1

The flowing array is stored in var $items (plus more).

"c74a0dba-5407-4f90-b04e-fdc88c4dd434": {
"0": {
"value": "Suffolk"
}
},
"91866e91-70d7-405f-900c-475d0c027399": {
"country": {
"0": "GB"
}
},

To view the array i can use:

$county = json_decode($items); 
var_dump($county);

This prints

[c74a0dba-5407-4f90-b04e-fdc88c4dd434"]=> object(stdClass)#24 (1) { ["0"]=> object(stdClass)#25 (1) { ["value"]=> string(15) "Suffolk" } } ["91866e91-70d7-405f-900c-475d0c027399"]=> object(stdClass)#26 (1) { ["country"]=> object(stdClass)#27 (1) { ["0"]=> string(2) "GB" } }

I need to extract the county info 'Suffolk' using its unique identifier.

I attempted the below but it produces a server error

$result_county = $county->getElement('c74a0dba-5408-4f90-b04e-fdc88c4dd434')->getElementData()->get('value');

The below is a var_dump of the whole array. The identifier c74a0dba-5408-4f90-b04e-fdc88c4dd434 is the same for each item array.

object(stdClass)#2 (18) { ["4d612549-f4cd-4487-ba42-f091ece35391"]=> object(stdClass)#3 (1) { ["0"]=> object(stdClass)#4 (1) { ["value"]=> string(13) "East Newlem" } } ["5eb77708-72e8-4587-b65e-ee50eb0f9e6d"]=> object(stdClass)#5 (1) { ["0"]=> object(stdClass)#6 (1) { ["value"]=> string(24) "Hillside Estate" } } ["520720dc-c480-405a-ac56-bf317f48d860"]=> object(stdClass)#7 (1) { ["0"]=> object(stdClass)#8 (1) { ["value"]=> string(17) "11 Rees Drive" } } [ECT...

Any ideas?

4

2 回答 2

2

If you treat this as an array in the following manner

$array = json_decode($items,true);
echo "<pre>",print_r($array,true),"</pre>"; 
$key ="c74a0dba-5408-4f90-b04e-fdc88c4dd434";
echo $array[$key][0]['value'];

It will produce and array structure like below:

Array
(
    [4d612549-f4cd-4345-ba42-f091ece35391] => Array
        (
        [0] => Array
            (
            [value] => East Newlem
            )
        )
    [5eb77708-72e8-4676-b65e-ee50eb0f9e6d] => Array
        (
        [0] => Array
            (
            [value] =>Hillside Estate
            )
        )
)

If you set the json_decoded 2nd argument to true it will return an array rather than the earlier objects. I do prefer objects over arrays but all about context and what the data represents (array or object).

Looks like you have composite objects after the extraction. The process has generated a lot of objects(stdClass) which is typical when a non-object is converted to an object. For instsnce if an example array is converted to an object the object(stdClass) that is generated will have properties that are named by keys and their corresponding values. I think this is what you are seeing here.

Initially there is a container object holding x objects that mostly consist of 3 keys, the first 2 of which have object values and the third a string value. Accessing the info like a standard object:

echo $county->{4d612549-f4cd-4487-ba42-f091ece35391}->{0}->value;

'East Newlem' should be returned (if not try placing quotes around the values in the curly braces).

Broken into objects the data looks like below:

object(stdClass)#2 (18) {
["4d612549-f4cd-4487-ba42-f091ece35391"]=>
    object(stdClass)#3 (1) { ["0"]=>
        object(stdClass)#4 (1) { ["value"]=> string(13) "East Newlem" }
}
["5eb77708-72e8-4587-b65e-ee50eb0f9e6d"]=>
    object(stdClass)#5 (1) { ["0"]=>
        object(stdClass)#6 (1) { ["value"]=> string(24) "Hillside Estate" }
}

Hope this helps

于 2013-01-29T09:19:02.290 回答
1
$county = json_decode($items, true); /// which will give you array


foreach($county as $val){
   echo $val[0]['value'];
}
于 2013-01-29T09:19:58.470 回答