0

I try to echo a value from a multidimensional array but somehow I can't figure it out.

The structure of this array is as followed:

todos

  • remaining
    • id
    • todolist_id
    • position
    • content
    • completed
    • created_at
    • updated_at
    • comments_count
    • due_on
    • due_at
    • creator (array with: id, name, avatar url)
    • assignee (array with: id, type, name)
  • completed

My problem at the moment is, I am unable to echo/print the values from the due_at and due_on (which are the only 2 I need). As I figured out this is most likely caused by the 2 arrays (creator, assignee) at the end. Since every time I try to view the values it stops before the creator part and does not show the other todos, just the first one. here is the piece of code I used to echo the values:

foreach($todo_itemphp->todos as $key => $value) {   
        echo "<ul>";
        echo "<li>".$key. ":". "&nbsp". "</li>";
        foreach ($value as $key2 => $value2) {
            echo "<ul>";
            echo "<li>".$key2. ":". "&nbsp". "</li>";
            foreach ($value2 as $key3 => $value3) {
                echo "<ul>";
                echo "<li>".$key3. ":". "&nbsp". $value3."</li>";
                foreach ($value3 as $key4 => $value4) {
                    echo "<ul>";
                    echo "<li>".$key4. ":". "&nbsp". $value4."</li>";
                    echo "</ul>";
                }
                echo "</ul>";
            }
            echo "</ul>";
        }
        echo "</ul>";   
    }

If you would like to know it's concerning Basecamp, I'm trying to get all the relevant project info to be view on screen. If there are any questions or if I was a little bit vague with my question please ask me to clarify, I hope I gave enough information for you guys to see if I'm doing it right. And if not what I'm doing wrong.

Taryn
  • 242,637
  • 56
  • 362
  • 405
J.I.N Kleiss
  • 187
  • 1
  • 2
  • 12
  • Have you tried by adding `IF` condition to check `$value2` against `due_at` and `due_on` arrays since you need values only for this? – Yogesh Jul 15 '13 at 12:19
  • Is my understanding correct? or do you want to create `UL LI` for all the array element? – Yogesh Jul 15 '13 at 12:20
  • I was just putting them into an UL LI for clearing it out for myself, in the future I need to convert the values to a date and pick the latest date, but that's for later concern. All I am trying to achieve now is echo-ing the values for due_at and due_on. – J.I.N Kleiss Jul 15 '13 at 12:38
  • Have you checked my first comment? ... if you want to echo only for `due_at` and `due_on` then you can add `IF` condition for `$value2` – Yogesh Jul 15 '13 at 12:48
  • Yes I've tried it a minute ago, still the same result though... I have no idea what is going wrong tbh :S – J.I.N Kleiss Jul 15 '13 at 12:58
  • Can you share the actual array (may be with duplicate data) object? ... I tried to create an array object with above structure and your code is working fine – Yogesh Jul 15 '13 at 13:04
  • That's the problem it's an array exported from Basecamp itself, I have no access to the structure I guess Basecamp protected this. you can view it on a temporarily webpage if you like. – J.I.N Kleiss Jul 15 '13 at 13:39

1 Answers1

1

You can use recursive function.

function print_array($array) {
    echo "<ul>";
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            print_array($value);
        } else {
            echo "<li>" .$key .' : ' .$value ."</li>";
        }
    }
    echo "</ul>";
}
NemanjaLazic
  • 604
  • 4
  • 12
  • thanks for the reply, unfortunately this didn't do the trick. It's not showing anything. – J.I.N Kleiss Jul 15 '13 at 12:39
  • You need to call the function: `print_array($todo_itemphp->todos);` – rr- Jul 15 '13 at 13:15
  • I tried this but now nothing is printed, when I replace print_array($todo_itemphp->todos); with print_array($todo_itemphp->creator); The values of the creator of only the first todo item is shown. – J.I.N Kleiss Jul 15 '13 at 13:54