newbie
newbie

Reputation: 2452

Filling the array value if it is missing in the assoc array?

Filling the array value if it is missing in the assoc array? I have an :

$A= array("A1"=>array("a"=>1,"b"=>2,"d"=>3),
          "A2"=>array("a"=>4,"b"=>3,"c"=>2,"d"=>1)
          );

base on the A["A2"] size is bigger than A["A1"] I want got the new $A look like this

$A= array("A1"=>array("a"=>1,"b"=>2,"c"=>"0.00","d"=>3),
          "A2"=>array("a"=>4,"b"=>3,"c"=>2,"d"=>1)
          );

Upvotes: 0

Views: 47

Answers (1)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76890

i'd do it this way:

if (count($A['A2']) > count($A['A1'])){
    foreach($A['A2'] as $key => $value){
        if (!array_key_exists($key, $A['A1'])){
             $A['A1'][$key] = '0.00';
        }
    }
}

Upvotes: 1

Related Questions