0

I have

Array_A ( [0] => Array ( [DATE] => 2012-11-28 , [totalCount] => )
          [1] => Array ( [DATE] => 2012-11-29 , [totalCount] => )
          [2] => Array ( [DATE] => 2012-11-30 , [totalCount] => ) )

Array_B ( [10] => Array ( [DATE] => 2012-11-28 , [totalCount] => 30 )
          [11] => Array ( [DATE] => 2012-11-30 , [totalCount] => 40 )
          [12] => Array ( [DATE] => 2012-12-05 , [totalCount] => 50 ) )

How to I do if I need to replace the Array_A with the values from the Array_B and the output should be

Array_A ( [0] => Array ( [DATE] => 2012-11-28 , [totalCount] => 30 )
          [1] => Array ( [DATE] => 2012-11-29 , [totalCount] =>    )
          [2] => Array ( [DATE] => 2012-11-30 , [totalCount] => 40 ) )
3
  • 3
    What you have tried so far ? Commented Dec 27, 2012 at 8:40
  • 3
    This problem is very specific, you will not likely find a built-in API for this, you will have to write your own loop. Commented Dec 27, 2012 at 8:42
  • I have read the doc from php and tried standard function but none is desired Commented Dec 27, 2012 at 8:42

3 Answers 3

3

I just noticed the part where you only want the keys from array_a to be used, this loop wil do.

foreach($array_a as $key => $value){
    if (array_key_exists($key, $array_b){
        $result[$key] = $array_b[$key]; 
    } else {
        $result[$key] = null;
    }
}

Or, if you want the value of array_a being used if the key in array_b does not exist, simply replace the statement in the else-clause to: $result[$key] = $value;.


Edit (because of the comment of mickmackusa)

I think I misread the question back in the day and was expecting the key of the second array to be unique (like an identiefier in a table). So I was expecting array_b to be something like:

$array_b = [
    0 => ['DATE' => '2012-11-28', 'totalCount' => 30],
    2 => ['DATE' => '2012-11-30', 'totalCount' => 40],
    12 => ['DATE' => '2012-12-05', 'totalCount' => 50],
];

In that case the earlier mentioned code will work. Demo

See the answer from mickmackusa as that actually solves the issue of the OP.

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

2 Comments

Even after fixing the fatal typo in this snippet, it will stillnot provide the desired result from the sample input. Proof: 3v4l.org/ZBA0c
Thanks @mickmackusa you are absolutely right. Please see my edit.
2

Try

$result = array_merge_recursive($Array_A, $Array_B);
print_r($result);

1 Comment

This is inappropriate for the asked question. This unexplained code will produce unwanted results from the sample input data.
1

Your a and b arrays have identical structures, so rather than conditionally updating one column when another column is matched, just replace whole rows when an identifying column is found in the lookup array. Demo

$map = array_column($b, null, 'DATE');

var_export(
    array_map(
        fn($row) => $map[$row['DATE']] ?? $row,
        $a
    )
);

Output:

array (
  0 => 
  array (
    'DATE' => '2012-11-28',
    'totalCount' => 30,
  ),
  1 => 
  array (
    'DATE' => '2012-11-29',
    'totalCount' => NULL,
  ),
  2 => 
  array (
    'DATE' => '2012-11-30',
    'totalCount' => 40,
  ),
)

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.