0

I have 2 arrays

$arr1 = Array
    (
        [REG1] => 94
        [REG3] => 45
    )

$arr2 =Array
    (
        [0] => REG1
        [1] => REG2
        [2] => REG3
        [3] => REG4
    )

I have to loop 2 arrays and I would like a result in one array like this:

Array(
    [0] => Array(
            [REG1] => 94
        )
    [1] => Array(
            [REG2] =>
        )
    [2] => Array(
            [REG3] => 45
        )
    [3] => Array(
            [REG4] => 
        )
)

But for the instand I can not do what I want, here is where I am:

private function mergeData($arr1, $arr2){

    $result = array_map(function($v1, $v2){
        $t[$v1] = $v2;
        return $t;
    }, $arr2, $arr1);
                
    return $result;
}

output:

Array(
    [0] => Array(
           [REG1] => 94
        )
    [1] => Array(
            [REG2] =>45
        )
    [2] => Array(
            [REG3] => 
        )
    [3] => Array(
            [REG4] => 
        )
)

I cannot correctly put the first array's values with the right keys. I tried with array_merge_recursive(), but also failed.

4 Answers 4

2

$arr3 = array_fill_keys( $arr2, '' );

creates an array containing the values of $arr2 as keys with as value an empty string.

$arr3 =Array
    (
        [REG1] => '' 
        [REG2] => ''
        [REG3] => ''
        [REG4] => ''
    )

After that just merge.

$result = array_merge ( $arr3, $arr1 );

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

1 Comment

This is not the desired result.
0

Another option would be to use the array_map function and map key with the corresponding values, if any:

$arr1 = [
    'REG1' => 94,
    'REG3' => 45
];

$arr2 = [
    'REG1',
    'REG2',
    'REG3',
    'REG4'
];

$result = array_map(function($item) use($arr1){
    return [$item => isset($arr1[$item]) ? $arr1[$item] : ""];
},$arr2);

This will return:

Array
(
    [0] => Array
        (
            [REG1] => 94
        )

    [1] => Array
        (
            [REG2] => 
        )

    [2] => Array
        (
            [REG3] => 45
        )

    [3] => Array
        (
            [REG4] => 
        )

)

Comments

0

Using a loop:

<?php

$arr1 = [
    'REG1' => 94,
    'REG3' => 45
];

$arr2 = [
    'REG1',
    'REG2',
    'REG3',
    'REG4'
];

$result = $arr2;
foreach($result as &$v)
    $v = [$v => $arr1[$v] ?? null];
unset($v);

var_export($result);

Output:

array (
  0 => 
  array (
    'REG1' => 94,
  ),
  1 => 
  array (
    'REG2' => NULL,
  ),
  2 => 
  array (
    'REG3' => 45,
  ),
  3 => 
  array (
    'REG4' => NULL,
  ),
)

Comments

0

To enjoy all of the modern conveniences of PHP, write a functional-style script with array_map(), arrow functions syntax for brevity, and a null coalescing operator. Demo

var_export(
    array_map(
        fn($v) => [$v => $arr1[$v] ?? ''],
        $arr2
    )
);

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.