1

I have this json encoded array

requestparser.php

 $array = array("ph" => array("phweb" => "yes", "phemail" => "yesss"));
 echo json_encode($array);

and ajax post type request for sending and processing the return response.

$.ajax({
    type: 'POST',
    url: 'requestparser.php',
    data: { "request" : "pull" },
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    cache: false,
    success: function(result) {
    alert(result["ph"]["phweb"]);
    alert(result["ph"]["phemail"]);
    }
});

what I'm trying to do is to get the array key and filter it with if statement, like (refer below)

var thearraykey = array key
if (thearraykey === "ph"){
    alert(array key)
}

how to get the array key that was in the json encoded response from requestparser.php? any help, ideas and clues would be greatly appreciated.

Juliver Galleto
  • 8,831
  • 27
  • 86
  • 164
  • Is this what you want to do?: http://stackoverflow.com/questions/558981/iterating-through-list-of-keys-for-associative-array-in-json – Millie Smith Mar 15 '15 at 15:46
  • If you had a debugger like Firebug and used `console.log(result)` you'd be able to see how the object is formatted. – Devon Bessemer Mar 15 '15 at 15:47
  • Anyone like that I technically used a tertiary operator that is telling him to do nothing if he says `yes`, and view the SO question if he says `no`, which is sort of the opposite of what I want? No? Ok :(. – Millie Smith Mar 15 '15 at 15:47

1 Answers1

0

Use the Object.keys function. Like so:

var keys = Object.keys(jsonResponse);

That returns an array of keys. Then it's up to you to iterate over the array and do what you will with the keys. Recursive calls to the function with subsequent JSON objects will allow you to retrieve all the keys.

Zarathuztra
  • 3,215
  • 1
  • 20
  • 34