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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.