2

I'm trying to access a multidimensional array, and append to its sub-keys, but am having a hard time coming up with a function to do so.

Currently my array looks like this, with the key being the parent_id of the folder.

array (size=3)
  0 => 
    object(stdClass)[25]
      public 'id' => string '18' (length=2)
  18 => 
    array (size=3)
      19 => 
        object(stdClass)[28]
          public 'id' => string '19' (length=2)
      20 => 
        object(stdClass)[29]
          public 'id' => string '20' (length=2)
      21 => 
        object(stdClass)[30]
          public 'id' => string '21' (length=2)
  19 => 
    array (size=1)
      24 => 
        object(stdClass)[31]
          public 'id' => string '24' (length=2)

What I've tried:

function getChildren($folder_id)
    {
        $folder_cursor = $this->db->get_where("folder", array("id" => $folder_id));
        if ($folder_cursor->num_rows() > 0) {
            $row = $folder_cursor->row();
            array_push($this->temp, $row);
            $this->recursiveGetChildren($row->id);
        }
    }

    function recursiveGetChildren($parent_id)
    {
        $q = $this->db->get_where("folder", array("parent" => $parent_id));
        if ($q->num_rows() > 0) {
            $this->temp[$parent_id] = array();
            foreach($q->result() as $q) {
                $this->temp[$parent_id][$q->id] = $q;
                $this->recursiveGetChildren($q->id);
            }
        }
    }

I would like for the array to look like this:

array (size=3)
  0 => 
    object(stdClass)[25]
      public 'id' => string '18' (length=2)
  18 => 
    array (size=3)
      19 => 
        array (size=2)
         0=> 
          object(stdClass)[28]
              public 'id' => string '19' (length=2)
         24 => 
            object(stdClass)[31]
             public 'id' => string '24' (length=2)
      20 => 
        object(stdClass)[29]
          public 'id' => string '20' (length=2)
      21 => 
        object(stdClass)[30]
          public 'id' => string '21' (length=2)

Edited for clarity.

4
  • Can you please be clearer about the logic behind these? If there is any logic, you can use many array prototypes to accomplish such, you can even reference to the values of the arrays if you need to, but you cannot reference the keys. A possible recursive function that you could use is array_walk_recursive php.net/manual/en/function.array-walk-recursive.php, but it largely depends on the logic. In any case your case looks quite hard to do in a single line of code (or in a single function), but array_walk_recursive can be a good starting point. Commented May 15, 2015 at 16:16
  • I've updated the OP for clarity. Commented May 15, 2015 at 16:26
  • 1
    for your previous case, a possible solution (that will only work on that case though) is this one: sandbox.onlinephpfunctions.com/code/… . It is as simple as that, as long as that structure is respected. You can edit that code and regulate the preg_match according to your own keys and values, but this might be a good example to start from ;) Commented May 15, 2015 at 16:28
  • I like that train of thought @briosheje, thank you! Commented May 15, 2015 at 16:31

1 Answer 1

1

Your structure is as follows:

array(
    "child_id" => array(
        "sub_child_id" => array();
    ),
    "child_id_2" => array(
        "sub_child_id_2" => array(
            sub_sub_child_id => array();
        );
    ),
);

which implies the following:

$array[ $a[ $aa[ ] ], $b[ $bb[ $bbb[ ] ] ] ]

Then, it seems like for each array, the element itself is an array of arrays.. nth depth

Try array_walk_recursive()):

<?php
$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');

function test_print($item, $key)
{
    echo "$key holds $item\n";
}

array_walk_recursive($fruits, 'test_print');
?>

Implementation:

Sign up to request clarification or add additional context in comments.

3 Comments

You're looking at the structure I wanted, not the structure I currently have
ahh, ok. ill update as soon as ive tested in my playground. apologies.
ok, i tried reading it again. perhaps, you could structure firstly the question, the issue, then your attempt on solution and that would help the community understand it better. hope we are able resolve it but i am only guessing, from your pseudocode, you're close. but its missing a context/question.

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.