0

I am new to php and am not sure why this isn't working. Could someone help me? Thanks! My code is below:

if (!$this->_in_multinested_array($over_time, $request_year)) {
            $temp = array('name'=>$request_year, 'children'=>array());
            array_push($over_time, $temp);
        }

        if (!$this->_in_multinested_array($over_time, $request_month)) {

            $child = array('name'=>$request_month, 'children'=>array());

            foreach ($over_time as $temp) {

                if ($temp['name'] == $request_year) {
                   array_push($temp['children'], $child);
                }
            }
        }

Whenever I check the result of this code the temp['children'] array is always empty even though it shouldn't be.

Mars J
  • 902
  • 3
  • 15
  • 23

1 Answers1

2

Each $temp in this loop is a copy:

    foreach ($over_time as $temp) {

        if ($temp['name'] == $request_year) {
           array_push($temp['children'], $child);
        }
    }

You want to change the array instead of making a copy, so you have to use a reference:

    foreach ($over_time as &$temp) {

        if ($temp['name'] == $request_year) {
           array_push($temp['children'], $child);
        }
    }
Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194