【问题标题】:compare 2 multi-dimensional arrays比较2个多维数组
【发布时间】:2015-04-23 15:19:18
【问题描述】:

php:比较 php 中的 2 个多维数组 我有 02 个数组,我想比较一下。

    $first = array(
          0 => array(
                 "id" => 45,
                 "name" => "chicago"
               ),
          1 => array(
                 "id" => 78,
                 "name" => "LA"
               ),
    );

    $second = array(
          0 => array(
                 "id" => 45,
                 "name" => "chicago"
               ),
          1 => array(
                 "id" => 78,
                 "name" => "LA"
               ),
          2 => array(
                 "id" => 95,
                 "name" => "Washington"
               ),
    );

比较两个数组后,我想要这个

    $cityMissing =array (
                  0 => array(
                     "id" => 95,
                     "name" => "Washington"
                   );


**Please, I need your help.**

【问题讨论】:

  • 请先向我们展示您的尝试,以便我们帮助您解决问题。

标签: php arrays


【解决方案1】:

你可以使用array_diff_assoc:

$arrayFirst = array("0" => array("id", "45") );
$arraySecond = array("0" => array("id", "45"), "1" => array("id", "95"));

$result = array_diff_assoc($arraySecond, $arrayFirst);

print_r($result);

Array
(
    [1] => Array
    (
        [0] => id
        [1] => 95
    )
)

【讨论】:

  • 那是一条评论
  • 它不起作用,我有一个错误:注意:数组到字符串的转换
【解决方案2】:
public static function array_diff_assoc_recursive($a, $b){
        // Get all of the "compare against" arrays
        $b = array_slice(func_get_args(), 1);
        // Initial return value
        $ret = array();

        // Loop over the "to" array and compare with the others
        foreach($a as $key=>$val){
            // We should compare type first
            $aType = gettype($val);
            // If it's an array, we recurse, otherwise we just compare with "==="
            $args = $aType === 'array' ? array($val) : true;

            // Let's see what we have to compare to
            foreach($b as $x){
                // If the key doesn't exist or the type is different,
                // then it's different, and our work here is done
                if(!array_key_exists($key, $x) || $aType !== gettype($x[$key])){
                    $ret[$key] = $val;
                    continue 2;
                }

                // If we are working with arrays, then we recurse
                if($aType === 'array'){
                    $args[] = $x[$key];
                }
                // Otherwise we just compare
                else{
                    $args = $args && $val === $x[$key];
                }
            }

            // This is where we call ourselves with all of the arrays we got passed
            if($aType === 'array'){
                $comp = call_user_func_array(array(get_called_class(), 'array_diff_assoc_recursive'), $args);
                // An empty array means we are equal :-)
                if(count($comp) > 0){
                    $ret[$key] = $comp;
                }
            }
            // If the values don't match, then we found a difference
            elseif(!$args){
                $ret[$key] = $val;
            }
        }
        return $ret;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多