My goal is to merge 2 different arrays.
I have table "a" & "b". Data from table "a" are more prioritar.
PROBLEM: if a key from "a" contains an empty value, I would like to take the one from table "b".
Here is my code:
<?php
$a = array('key1'=> "key1 from prioritar", 'my_problem'=> "");
$b = array('key1'=> "key1 from LESS prioritar", 'key2'=>"key2 from LESS prioritar", 'my_problem'=> "I REACHED MY GOAL!");
$merge = array_merge($b, $a);
var_dump($merge);
Is there a way to do this in one function without doing something like below?
foreach($b as $key => $value)
{
if(!array_key_exists($key, $a) || empty($a[$key]) ) {
$a[$key] = $value;
}
}
$b
has 2key2
indexes? – Radices!array_key_exists || empty
is nonsense. Using either one will do just fine, depending on whether you're interested in a comparison tofalse
or not. Using both together is the same as just usingempty
. – Playwritingempty
. The Definitive Guide To PHP's isset And empty – Playwriting