0

I'm trying to modify a multidimensional array inside a foreach loop using a function. However when I check my results with a print_r() the outcome is not what I want it to be and I'm not receiving any errors either.

foreach ($layoutData as $key => $data) {

            $method = $data[ 'acf_fc_layout' ];

            if (method_exists($this, $method)) {
                $data = $this->$method($data);
            }

            $this->parseOptions($data);
}

Note: The foreach is inside a function called layouts(). I thought this was irrelevant so I left that out.

public function parseOptions($data) {

        if (isset($data[ 'padded' ])) {
            if ($data[ 'padded' ] == 'true') {
                $data[ 'options' ][ 'padded' ] = true;
            } else {
                $data[ 'options' ][ 'padded' ] = false;
            }
            unset($data[ 'padded' ]);
        }

        if (isset($data[ 'background-color' ])) {
            $data[ 'options' ][ 'background-color' ] = $data[ 'background-color' ];
            if ($data[ 'options' ][ 'background-color' ] == 'white') {
                $data[ 'options' ][ 'background-color' ] = false;
            }
            unset($data[ 'background-color' ]);
        }

        return $data;
}

print_r() result:

Array
(
    [view] => signup
    [data] => Array
        (
            [acf_fc_layout] => signup
            [title] => Schrijf je snel in
            [text] => Kickstarten begint met aanmelden. Doe het snel, dan maak je kans op een Samsung Galaxy S9.
        )
   [padded] => 'true'
   [background-color] => 'dark-grey'
)

But what I want to achieve is:

Array
    (
        [view] => signup
        [data] => Array
            (
                [acf_fc_layout] => signup
                [title] => Schrijf je snel in
                [text] => Kickstarten begint met aanmelden. Doe het snel, dan maak je kans op een Samsung Galaxy S9.
            )
         [options] => Array
            (
                [padded] => 'true'
                [background-color] => 'dark-grey'
            )
Jessey Fransen
  • 135
  • 2
  • 11
  • Possible duplicate of [PHP foreach change original array values](https://stackoverflow.com/questions/15024616/php-foreach-change-original-array-values) – M. Eriksson Oct 04 '18 at 08:44
  • When running your foreach, the `$data` variable is only a _copy_ of the original value. Changing that won't change the original array. The above link should give you some more info about what you need to do. It's also explained [in the manual about foreach](http://php.net/manual/en/control-structures.foreach.php) – M. Eriksson Oct 04 '18 at 08:46

2 Answers2

0

I think this will solve your issue:

$layoutData[$key] = $this->$method($data);
sietse85
  • 1,488
  • 1
  • 10
  • 26
  • A good answer includes an explanation of _why_ it solves the issue. But since this has been asked and answered multiple times here before, the question should actually be closed as a duplicate. – M. Eriksson Oct 04 '18 at 08:54
0

Changing

$this->parseOptions($data);

to

 $data = $this->parseOptions($data);

Solved the problem. Rookie mistake.

Jessey Fransen
  • 135
  • 2
  • 11