0

I have two arrays (one simple and one multidimensional) and I want to verify if certain keys from the multidimensional array have empty values and replace them with their correspondent values from the simple array.

Solution for both simple arrays and/or 2D arrays is here:

PHP Compare and change certain elements in multidimensional arrays

But what would be the solution in the situation above?

Example of simple array:

$superheroes_complete = array(
    1 => 'one',
    'two' => 'two',
    3 => 'three',
    'email' => '[email protected]',
    5 => 'cinco',
    6 => 'six',
    'name' => 'Clark Kent',
    8 => 'eight'
);

Example of multidimensional array:

$superheroes_empty = array(
    "spiderman" => array(
        "name" => "Peter Parker",
        "email" => "",
    ),
    "superman" => array(
        "name" => "",
        "email" => "[email protected]",
    ),
    "ironman" => array(
    "name" => "Harry Potter",
    "email" => "[email protected]",
    )
);

Expectation:

$superheroes_empty = array(
    "spiderman" => array(
        "name" => "Peter Parker",
        "email" => "[email protected]",
    ),
    "superman" => array(
        "name" => "Clark Kent",
        "email" => "[email protected]",
    ),
    "ironman" => array(
        "name" => "Harry Potter",
        "email" => "[email protected]",
    )
);

Thank you in advance.

2
  • So how is your simple array supposed to relate to the multidimensional array, to know which values to change? I can't see any relationship between the two, nor any explanation Commented Jul 9, 2016 at 15:19
  • The relation should be made according to their key names. Will compare which keys from multidimensional array are empty and will replace their values with correspondent values of keys from simple array. In the example above shoud replace the "name" and "email" keys' values. Commented Jul 9, 2016 at 15:23

2 Answers 2

1

Here is one way to do this using array_walk_recursive:

array_walk_recursive($superheroes_empty, function(&$v, $k) use ($superheroes_complete) {
    if ($v === '' && isset($superheroes_complete[$k])) {
        $v = $superheroes_complete[$k];
    }
});

This will fill in any empty values if a corresponding key is found in $superheroes_complete. Since this makes replacements by reference, it will directly change the $superheroes_empty array, so if you still need the one with empty values, make a copy before using this.

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

2 Comments

It's OK, I don't need the data from it. This is only an example. But is important that the $superheroes_empty to remain in initial structure after replacements are made. Is this possible though?
This won't change the structure; it will only full in empty values.
0

You can also use this

foreach($superheroes_empty as $key =>$array){
    foreach($array as $key1=>$data){        
        if(empty($data)){           
            $superheroes_empty[$key][$key1] = $superheroes_complete[$key1];         
            }
        }   
    }
    echo "<pre>";print_r($superheroes_empty);

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.