jquery looping issue

hi
i have this array returns from an ajax call. at the moment it is hard coded, it will be dynamic.
        $multilinevalues = array(
            array( 'x' => "2011 Q1" , 'y' => 3, 'z' => 6  ) ,
            array( 'x' => "2011 Q2" , 'y' => 10, 'z' => 1 ) ,
            array( 'x' => "2011 Q3" , 'y' => 2 , 'z' => 4) ,
            array( 'x' => "2011 Q4" , 'y' => 3 , 'z' => 3) ,
        );
       
        echo json_encode($multilinevalues);

Open in new window


i need to store the "x" key (not its value) in a variable. ( NOT 4 times, but just one).
i need to store the "y" and "z" keys (not the values) in an array ( again NOT 4 times, but only the one iteration). obviously in real data after y and z can be more.

how do i do that? tried many loops and get the keys but i need the x in a variable of its own and the y and z within an array, and just once not all the inner arrays.


best regards
LVL 1
derridaAsked:
Who is Participating?
 
Julian HansenCommented:
This would depend on whether your json array will always contain consistent arrays i.e arrays with the same number of items with the same key names. If so you could do something like this
$(function() {
    $.ajax({
      url: 'yourdatascript.php',
      type: 'POST',
      dataType: 'JSON',
      success: function(result) {
        var keys = Object.keys(result[0]);
      }
    });
});

Open in new window

If the returned arrays are not consistent then this might be what you want
$(function() {
  $.ajax({
    url: 'yourdatascript.php',
    type: 'POST',
    dataType: 'JSON',
    success: function(result) {
      var keyvalues = Array();
      for (var r in result) {
        keys = Object.keys(result[r]);
        var i;
        for(i =0; i< keys.length;i++) {
          keyvalues[keys[i]] = keys[i];
        }
      }
      console.log(keyvalues);
    }
  });
});

Open in new window

0
 
derridaAuthor Commented:
hey, first thanks for the help. but i needed not just the keys, i need the first key stored in a seperate veriable, and all the other keys in a separate array.
0
 
Julian HansenCommented:
Can you post an example (Based on your sample input) of what you want out the other end
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
derridaAuthor Commented:
i need something like this:

loop this array and from it:
var firstKey = "x" (or whatever it will be once it will work dynamically.)notice, this one it the first key in the array but need to be transmitted as a string.
ver restKeys = ['y','z']; or whatever will be the reat of the keys array.
0
 
derridaAuthor Commented:
ok did it.
used this:
            var thefirst = thekeys[0].shift();
            var therest = thekeys[0];

thank you for the help. i was lost.
0
 
Julian HansenCommented:
You are welcome, thanks for the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.