用另一个数组中的Value替换数组的键

时间:2022-06-29 21:29:08

I have an array structure like below

我有一个如下所示的数组结构

Array
(
    [1] => Dept1
    [2] => Dept2
    [3] => Dept3
)

And I have another Array as below

我有另一个数组如下

Array
(
    [1] => Array
        (
            [user1] => 58
            [user3] => 75
        )

    [2] => Array
        (
            [user6] => 162
        )

    [3] => Array
        (
            [user7] => 2
            [user8] => 126
            [user9] => 148

        )
)

I want

Array
    (
        [Dept1] => Array
            (
                [user1] => 58
                [user3] => 75
            )

        [Dept2] => Array
            (
                [user6] => 162
            )

        [Dept3] => Array
            (
                [user7] => 2
                [user8] => 126
                [user9] => 148

            )
    )

The numbers in 2nd array are the department numbers. And their values are present in the first array. I want to replace the department number in second array with value from first array.

第二个数组中的数字是部门编号。它们的值存在于第一个数组中。我想用第一个数组中的值替换第二个数组中的部门号。

I have tried with array_replace() but don't get successful.

我尝试过使用array_replace()但是没有成功。

Please help

Thanks in advance

提前致谢

3 个解决方案

#1


4  

If the second array has less elements than your lookup array or if the keys are not in the same order, you need to map the key values first and then combine the arrays using array_combine().

如果第二个数组的元素少于查找数组,或者键的顺序不同,则需要先映射键值,然后使用array_combine()组合数组。

array_combine(array_map(function($key) use ($depts) {
    return $depts[$key]; // translate key to name
}, array_keys($dept_values)), $dept_values));

Otherwise, you can combine them immediately:

否则,您可以立即合并它们:

array_combine($depts, $dept_values);

See also: array_map()

另请参见:array_map()

#2


-1  

Try this. Basically join and explode will do your work.

试试这个。基本上加入和爆炸将完成你的工作。

<html>
<head>
<title>Copy to new array</title>
</head>
<body>
<?php
$dept= array('Dept1', 'Dept2', 'Dept3', 'Dept4');
$temp= join(",",$dept);
$department=explode(",",$temp);
echo "The first element in new array is: " . $department[0];
echo " and the second element in new array is: " . $department[1];
?>
</body>
</html>

#3


-1  

This code is working as expected for me:

这段代码正如我所料:

PHP Code:

<?php
print_r(array_combine($firstArray, $secondArray));
?>

Array Output:

Array
(
    [Dept1] => Array
        (
            [user1] => 58
            [user3] => 75
        )

    [Dept2] => Array
        (
            [user6] => 162
        )

    [Dept3] => Array
        (
            [user7] => 2
            [user8] => 126
            [user9] => 148
        )

)

#1


4  

If the second array has less elements than your lookup array or if the keys are not in the same order, you need to map the key values first and then combine the arrays using array_combine().

如果第二个数组的元素少于查找数组,或者键的顺序不同,则需要先映射键值,然后使用array_combine()组合数组。

array_combine(array_map(function($key) use ($depts) {
    return $depts[$key]; // translate key to name
}, array_keys($dept_values)), $dept_values));

Otherwise, you can combine them immediately:

否则,您可以立即合并它们:

array_combine($depts, $dept_values);

See also: array_map()

另请参见:array_map()

#2


-1  

Try this. Basically join and explode will do your work.

试试这个。基本上加入和爆炸将完成你的工作。

<html>
<head>
<title>Copy to new array</title>
</head>
<body>
<?php
$dept= array('Dept1', 'Dept2', 'Dept3', 'Dept4');
$temp= join(",",$dept);
$department=explode(",",$temp);
echo "The first element in new array is: " . $department[0];
echo " and the second element in new array is: " . $department[1];
?>
</body>
</html>

#3


-1  

This code is working as expected for me:

这段代码正如我所料:

PHP Code:

<?php
print_r(array_combine($firstArray, $secondArray));
?>

Array Output:

Array
(
    [Dept1] => Array
        (
            [user1] => 58
            [user3] => 75
        )

    [Dept2] => Array
        (
            [user6] => 162
        )

    [Dept3] => Array
        (
            [user7] => 2
            [user8] => 126
            [user9] => 148
        )

)

相关文章