Reputation: 181
Can't seem to figure this out. I struggle so badly with arrays. I am trying to get the data out of this array and keep getting errors.
I tried using this code below and several other attempts and failed miserably. What's the best way to remember how to do this? Everytime I don't code Php for a couple months I seem to forget everything...
foreach( $data as $key) {
foreach( $key as $value => $sum) {
echo $sum;
}
}
Array
(
[result] => OK
[data] => Array
(
[destination] =>
[tracking] => Array
(
[0] => Array
(
[loc] => Array
(
[city] =>
[territory] => ME
[country] => US
)
[desc] => Delivered
[stamp] => 1384977300
[time] => 11/20/13 11:55 am
[locStr] => ME, US
[geo] => Array
(
[lat] => 45.253783
[lon] => -69.4454689
)
)
Upvotes: 0
Views: 48
Reputation: 53
I think your code is not wrong. you have three dimensional array. you have only two foreach. you have to loop within a loop within a loop. and if you try to echo an array Ofcourse you will have an error so you should check first if that output is an array or not.
Upvotes: 1
Reputation: 1837
In php there are basically two different type of arrays. key/value based array and element based array. An element based array is
$arr = array("a", "b", "c");
echo $arr[0]; // prints a
echo $arr[2]; // prints c
// hash - k/v array
$arr = array("monkey" => "banana", "chicken" => "egg");
echo $arr["monkey"]; // prints banana
// combination
$arr = array( array("monkey" => array("banana", "water")));
echo $arr[0]["monkey"][1]; // prints water
hope this helps.
Upvotes: 1
Reputation: 1837
iterate through tracking?
if ($arr['result'] == "OK") {
for ($i=0; $i < count( $arr['data']['tracking'] ); $i++) {
// do stuff with $arr['data']['tracking'][$i]
}
}
Upvotes: 1