1

Let me explain my problem, I am supposed to call a web service before the contact form is shown, the return of the web service is:

$items = json_decode('[{"location":[{"building":["Building1"],"name":"Location1"}],"name":"Organization1"},{"location":[{"building":["Building2"],"name":"location2"}],"name":"Organisation2"},{"location":[{"building":["Building3"],"name":"Location3"}],"name":"Organization3"}]');

Here I have extracted only the organisations,locations and building using the following code:

foreach( $items as $each ){
    echo $each->location[0]->building[0];
    echo $each->location[0]->name;
    echo $each->name;
}

I would like to get the values of organisations, buildings and locations in different arrays in this format:

("building1", "building2", "building3")
("organisation1", "organisation2", "organisation3")
("location1", "location2", "location3")
Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
user2201395
  • 179
  • 1
  • 2
  • 8
  • 1
    Well then what do you do a organization has multiple locations? I ask because the format chosen for the JSON specifically allows for multiple locations for each organization. Or are you not planning to cross reference on the numeric keys? – prodigitalson Mar 23 '13 at 03:30
  • well i really wanted to do some thing like that,you have read my mind,do you have any idea on doing that – user2201395 Mar 23 '13 at 03:37
  • http://stackoverflow.com/questions/8695572/how-to-convert-this-json-to-php-array-and-also-how-to-get-specific-values-only – Dipesh Parmar Mar 23 '13 at 03:41
  • @user2201395: Why do you want to access them from dissociated arrays when you can access them as a single structure for each item returned? That doesnt make any sense. If you just want to access them as arrays then youre better off forcing the JSON conversion to an array like `$items = json_decode($theJsonString, true)` then you could access like `$items[0]['location'][0]['name']` – prodigitalson Mar 23 '13 at 06:03

2 Answers2

1

To take your existing code and modify it a tiny bit;

$buildings = array( );
$organisations = array( );
$locations = array( );
foreach( $items as $each ){
    $buildings[] = $each->location[0]->building[0];
    $organisations[] = $each->location[0]->name;
    $locations[] = $each->name;
}

The results are now in the variables defined at the top. [] simply tells it to append a value to the end of the array.

Dave
  • 44,275
  • 12
  • 65
  • 105
  • location[0]->building[0] ); organisations.push( $each->location[0]->name ); locations.push( $each->name ); } there is showing some error at the line var buildings – user2201395 Mar 23 '13 at 03:34
  • @user2201395 try it now. I mistakenly thought you were using JavaScript. – Dave Mar 23 '13 at 03:36
  • @dave i have included echo $buildings; to the end of your code i am getting the result as ArrayArrayArray ,is it not possible to print as ("building1", "building2", "building3") – user2201395 Mar 23 '13 at 03:44
  • @user2201395 to print arrays nicely in PHP you need to use `print_r(myArray)` instead of `echo myArray` – Dave Mar 23 '13 at 03:45
  • @anyone how can i retrieve the array values to come as options in the drop down menu – user2201395 Mar 23 '13 at 04:17
  • @user2201395 that is a different question, and likely one which has been asked before. Try searching. – Dave Mar 23 '13 at 04:18
0

Here's how you do it:

$arrays = array();
$arrays['buildings'] = array(); // buildings
$arrays['organisations'] = array();
$arrays['locations'] = array();
foreach( $items as $each ){
    $arrays['buildings'][] = $each->location[0]->building[0];
    $arrays['organisations'][] = $each->location[0]->name;
    $arrays['locations'][] = $each->name;
}
Shoe
  • 74,840
  • 36
  • 166
  • 272