1

I'm trying to add key value pairs to an array with php.

When I echo out the array, i get the values

echo "Key = " . $key . "| Value = " . $value;

Key = area_id| Value = 4000001Key = area_title| Value = Region

All good.

But when I try and add those key value pairs to an array 'main', the array is empty?

Like below:

                $main = array();

                function recursive($array){
                    foreach($array as $key => $value){
                        //If $value is an array.
                        if(is_array($value)){
                            //We need to loop through it.
                            recursive($value);
                        } else{
                            //It is not an array, so print it out.
                            //$main[$key] = array (
                            //  $key = $value
                            //);

                            //echo "Key = " . $key . "| Value = " . $value;

                            $main[$key] = $value;
                        }
                    }
                }   

If the key and values are there, as I can echo them, why are they not adding to the array?

frobak
  • 557
  • 2
  • 11
  • 30

3 Answers3

4

As your function is written, the variable $main in the function is local to that function. Changes made to that local variable won't affect the $main that is outside the function.

Add this as the first line in your function:

global $main;

This will allow your function to modify the global variable.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Your function doesn't work because $main variable is defined in global scope, not in a function scope. One approach could be to introduce $main to your function using global $main.

Another, better, approach is to modify your function so it will return result array.

$array = array(
    'Batman' => 'Robin',
    'Fruits' => array(
        'Apple', 'Strawberry'
    ),
    'Sports' => array(
        'collective' => 'Basketball',
        'one man show' => 'Running'
    )
);

$main = array();

function recursive($source){
    $result = array();
    foreach($source as $key => $value){
        //If $value is an array.
        if(is_array($value)){
            //We need to loop through it.
            $result = array_merge($result, recursive($value));
        } else{
            $result[$key] = $value;
        }
    }

    return $result;
}   

var_dump(recursive($array));
Community
  • 1
  • 1
Buksy
  • 11,571
  • 9
  • 62
  • 69
0

Thanks for your help. I think I was WAY over complicating it.

I just needed to print a couple of values from each array, and if a value was another array, get the values from that.

I found and amended the below function, which is very simple, and works a treat

            // recursive function to print areas grouped with their children
            function RecursiveWrite($array) {
                foreach ($array as $vals) {

                    echo "<div class='row area_level_rows area_level_" . $vals['area_level'] . "'>";
                            echo "<div class='col-md-12'>" . $vals['area_name'] . "</div>";
                    echo "</div>";                  

                    if(!empty($vals['children'])) {
                    RecursiveWrite($vals['children']);
                    }
                }
            }

            RecursiveWrite($area_tree);
frobak
  • 557
  • 2
  • 11
  • 30