PHP—array_merge_recursive()-相同键没有数组 [英] PHP—array_merge_recursive() - no Array for same keys

查看:69
本文介绍了PHP—array_merge_recursive()-相同键没有数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$php -a
php > $data1 = ['tag' => 'div', 'classes' => [1,2,3]];
php > $data2 = ['tag' => 'section', 'classes' => [2,3,4,5,6]];
php > $result = array_merge_recursive($data1, $data2);
php > print_r($result);
Array
(
    [tag] => Array
        (
            [0] => div
            [1] => section
        )

    [classes] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 2
            [4] => 3
            [5] => 4
            [6] => 5
            [7] => 6
        )

 )

文档中所述:

So as describes in the Docs:

如果输入数组具有相同的字符串键,则这些键的值将合并到一个数组中[…]

If the input arrays have the same string keys, then the values for these keys are merged together into an array[…]

PHP中是否有一个基本功能相同的现有函数,但是没有将相同的键合并到一个数组中,从而覆盖了值并保留了键?

Is there a existing function within PHP that basically does the same, but without merging the same keys into an array, so that the values are overridden and the key kept?

在这种情况下,我希望得到该结果:

In this case I would like to have that result:

Array
(
    [tag] => section

    [classes] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [5] => 4
            [6] => 5
            [7] => 6
        )

 )

关于@JustOnUnderMillions的评论:

In regards to the comment of @JustOnUnderMillions:

是的,我想知道,这不是我期望的那样的功能,而是在寻找结果的时候.

Yes I wonder, that is not what I would expect of such a function, I would expect a result as I am looking for.

推荐答案

您可以处理此功能(递归和多数组):

You can deal with this function (recursive and multi-arrays):

<?php

$data1 = ['tag' => 'div', 'classes' => [1,2,3], 'foo' => ['bar' => [1,2], 'bar2' => 'foo']];
$data2 = ['tag' => 'section', 'classes' => [2,3,4,5,6], 'foo' => ['bar' => [2,3], 'bar3' => 'foo']];
$data3 = ['tag' => 'section', 'classes' => [7], 'foo' => ['bar' => [5], 'bar3' => 'foo2']];


print_r(custom_array_merge($data1, $data2, $data3));

function custom_array_merge() {
    $arguments = func_get_args();
    $datas = call_user_func_array('array_merge', $arguments);
    foreach($datas as $key => $value) {
        if(is_array($value)) {
            $values = [];
            foreach($arguments as $array) {
                $values[] = isset($array[$key]) ? $array[$key] : [];
            }

            if(array_depth($value) === 1) {
                $datas[$key] = array_unique(call_user_func_array('array_merge', $values));
            }else {
                $datas[$key] = call_user_func_array('custom_array_merge', $values);
            }
        }
    }

    return $datas;
}

function array_depth(array $array) {
    $max_depth = 1;

    foreach ($array as $value) {
        if (is_array($value)) {
            $depth = array_depth($value) + 1;

            if ($depth > $max_depth) {
                $max_depth = $depth;
            }
        }
    }

    return $max_depth;
}

这篇关于PHP—array_merge_recursive()-相同键没有数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆