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?

1
  • show the $array content Commented May 18, 2016 at 19:47

3 Answers 3

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.

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

2 Comments

mmmm, added that, still returning an empty array?
@frobak: Works as expected. You are not defining $main inside another function are you? The indenting makes it look like you are in another function.
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));

Comments

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);

Comments

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.