0

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
                                    )

                            )
2
  • 1
    what is the question? Commented Nov 24, 2013 at 1:18
  • I'm just trying to loop through this Array. I know, newbie question but I'm not much of a developer but I'm trying to learn. I mostly do design work and some frontend JS. Commented Nov 24, 2013 at 1:20

3 Answers 3

1

iterate through tracking?

if ($arr['result'] == "OK") {

  for ($i=0; $i < count( $arr['data']['tracking'] ); $i++) {
    // do stuff with $arr['data']['tracking'][$i]
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Doesnt seem to work, not sure why... I also was trying this based on another thread but getting bad results: foreach($data['data']['tracking'][0] as $arr) { echo $arr['desc']; echo $arr['time']; }
Right on target, I just got it working. Thanks alot for the help, much appreciated, I'm on the right track now.
1

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.

1 Comment

I think I made this problem way harder than it was but thanks for the help.
1

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.

Comments

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.