1

Ok, so I have this in a JSON array (the output of retrievedData())

{
    "device": {
        "01AA02AB141208VV": {
            "$version": -148598935,
            "$timestamp": 1344382016000,
            "serial_number": "01AA02AB141208VV"
        }
    }
}

However, I can't seem to select the serial number. I have tried what usually works, but I can't select it. This is assuming we don't already know the "01AA02AB141208VV".

$retrieved_data = retrieveData($array['userid'], $array['urls']['transport_url'], $array['access_token']);
$retrieved_data_array = json_decode($retrieved_data, true);
$serial_number = $retrieved_data_array['device'][0]['serial_number'];

Am I doing something wrong? I bet it's really simple, but I just cannot figure it out!

Thanks!

14
  • 1
    where is the code for retrieveData function? Show us the result of var_dump($retrieved_data_array). Commented Aug 8, 2012 at 0:05
  • what is $array? , does it matter? Commented Aug 8, 2012 at 0:05
  • 1
    If you don't already know the serial number, then you have no way of accessing the data without looping through each device using a foreach and examining the contents. Commented Aug 8, 2012 at 0:07
  • retrieveData is a huge script that just outputs the JSON array. Really not needed, since I gave you its output. $array is also sperate from this and has no effect on it. Commented Aug 8, 2012 at 0:07
  • 1
    @jprofitt: actually it is an array since true was passed as the 2nd argument. Commented Aug 8, 2012 at 0:08

3 Answers 3

4

Unfortunately, it's not really simple. You're trying to find 42 Main St. when you don't know the city Main St. is located in. The only way to find the serial_number would be to loop over $retrieved_data_array with foreach until you find the value you want.

Sign up to request clarification or add additional context in comments.

Comments

3

you can use key() function to access the keys of the associative array (in this case the '$retrieved_data_array['device']' array:

<?php

$data = '{"device": {"01AA02AB141208VV": {"$version": -148598935,"$timestamp": 1344382016000,"serial_number": "01AA02AB141208VV"}}}';

$retrieved_data_array = json_decode($data, true);
$serial_number = key($retrieved_data_array['device']);
reset($retrieved_data_array['device']);

print_r($serial_number);
print_r($retrieved_data_array['device'][$serial_number]);

?>

Comments

1

This will grab the serial numbers, assuming you have multiple items in the devices array:

$keys = array();

foreach( $retrieved_data_array['device'] as $key => $val ){
  $keys[] = $key;
}

$serial = $keys[0];

This can be reduced if you know you only have one item in the array...

1 Comment

Now, if there were two, would $keys[1] grab the next?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.