SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2009
    0 Post(s)
    0 Thread(s)

    advanced loops with multi-dimensional array

    I'm fairly good in PHP so I understand the main concept of everything. One problem I cant seem to wrap my head around is random dimensions of multidimensional arrays with for loops using the key and value. Here is an example.

    for example lets say I have something I grabbed from a database and I looped through and placed each entry into an array which looks like this

    array[0]["id"] = something
    array[0]["name"] = name
    array[1]["id"] = something else...
    array[1]["name"] = another name

    I can loop through this by doing something similar to

    PHP Code:
    foreach($array as $key => $value){
    $value as $key => $value){
    $key." ".$value."<br>";

    ** the above will basically list something similar to

    id : something
    name : name

    id : something else...
    name : another name

    This is usually what I use to loop through it...

    But lets say I throw a wrench in, such as array[1][0][0] is another array and array[3][0][1][2] is another array
    So basically I may randomly find arrays hidden deep within and I want the script to continue searching through each section to find another dimension to the array
    However I do not want to have to keep typing a for loops because sometimes I don't know if the array is 4 levels deep or 5 or just 2.
    I also want to have the pleasure of being able to grab the key with the value.

    Does anyone have any idea how to do this or if its even possible? I am thinking it may be possible with a function which keeps calling itself to check until it cant call itself any more. But even if I can do that how do I display it on screen in an organized manner.

    Hopefully someone has the knowledge to do this.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Lancaster University, UK
    2 Post(s)
    0 Thread(s)
    Really and truthfully, if you believe you are completely comfortable at the level you're at, this problem tells me it's time for you to tackle object-oriented PHP. It would make this problem a non-problem, because each level would have methods defined to handle this kind of thing.

    However, that won't help you anytime soon as it'll take a while to learn, so I'll focus on what you've got so far.

    You have the right idea with a function which calls itself. But how would you display hierarchial data with HTML? With nested lists

    There are probably more efficient methods than below, but it's written so that you see what's going on:
    PHP Code:
    function makeNestedList(array $Array){
    $Output '<ul>';
    $Array as $Key => $Value){
    $Output .= "<li><strong>{$Key}: </strong>";
    $Output .= makeNestedList($Value);
    $Output .= $Value;
    $Output .= '</li>';
    $Output .= '</ul>';
    $Data = array("Some Info" => array("A" => "a""B" => array("B1" => "b1""B2" => "b2"), "C" => array("C1" => array("C11" => "c11""C12" => "c12""C13" => array("C131" => "c131""C132" => "c132")), "C2" => "c2")));
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    3 Post(s)
    0 Thread(s)

    How would OOP make this a non-problem versus a precedural solution? Both would be done via recursion, no?

  4. #4
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Burlington, Canada
    89 Post(s)
    6 Thread(s)

    Another way to approach this is to use the recursiveiteratoritorator class which I show an example off how to work with dynamic depth arrays in this post... It is part of the PHP SPL so it is one of the faster compiled classes to use in such a case.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts