0

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

1 Answer 1

1

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';
        }
    }
}
Sign up to request clarification or add additional context in comments.

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.