dsrruefh12970 2016-01-14 18:46
浏览 45
已采纳

如何按键比较两个关联数组

I am trying to compare two associative arrays and get the difference based upon the value and also based upon the key. I have tried using an array_filter with a closure

The two arrays are like so:

Array 1

$newArr = [
    0 => [
        'id' => 'UT5',
        'qty' => '4'
    ],
    1 => [
        'id' => 'WRO',
        'qty' => '3'
    ],
    2 => [
        'id' => 'SHO',
        'qty' => '3' 
    ]
];

Array 2

$oldArr = [
    0 => [
        'id' => 'SHO',
        'qty' => '1'
    ],
    1 => [
        'id' => 'UT5',
        'qty' => '2'
    ],
];

My desired output is as follows:

array(3) 
{ 
  ["UT5"]=> int(2) 
  ["SHO"]=> int(2) 
  ["WRO"]=> int(3)
} 

I have gotten this far:

<?php

$newArr = [
    0 => [
        'id' => 'UT5',
        'qty' => '4'
         ],
    1 => [
        'id' => 'WRO',
        'qty' => '3'
    ],
    2 => [
        'id' => 'SHO',
        'qty' => '3'
    ]
];

$oldArr = [
    0 => [
        'id' => 'SHO',
        'qty' => '1'
    ],
    1 => [
        'id' => 'UT5',
        'qty' => '2'
    ],
];

$toAdd = [];
foreach ($newArr as $item) {
    $itemsToAdd = array_walk($oldArr, function ($k) use ($item, &$toAdd) {
        if ($k['id'] == $item['id']) {
            $toAdd[$k['id']] = max($k['qty'], $item['qty']) - min($k['qty'], $item['qty']);
        }
    });
}

var_dump($toAdd); die();

However with this function, my current output is:

array(2) { 
    ["UT5"]=> int(2) 
    ["SHO"]=> int(2) 
}

Note that WRO is missing. Is there a way that I can add a conditional to accurately check for this? I have tried a few solution such as !in_array and else but neither are giving me the desired output.

Any help is appreciated! Thanks!

  • 写回答

2条回答 默认 最新

  • duanhanzi8328 2016-01-14 18:53
    关注

    That's an easy one, your code saves a value ONLY if the key is present in both arrays. Just add a clause to check if the key DOESN'T exist in the old array. (also do the opposite in case the old array has a key the new one doesn't have)

    if (!isset(old array [ new array key ]){
    $newArray[new array key] = new array key and values;
    

    Your program structure is optimized for the computer and is too complex to follow as a human, I rewrote it entirely.

    <?php
    $newArr = [0 => ['id' => 'UT5', 'qty' => '4'], 1 => ['id' => 'WRO', 'qty' => '3'], 2 => ['id' => 'SHO', 'qty' => '3']];
    
    $oldArr = [0 => ['id' => 'SHO', 'qty' => '1'], 1 => ['id' => 'UT5', 'qty' => '2'], ];
    
    $newReset = [];
    foreach( $newArr as $item ) {
        $newReset[$item['id']] = $item['qty'];
    }
    
    $oldReset = [];
    foreach( $oldArr as $item ) {
        $oldReset[$item['id']] = $item['qty'];
    }
    
    foreach( $newReset as $key => $val ) {
        if( isset( $oldReset[$key] ) ) {
            $toAdd[$key] = max( $oldReset[$key], $val ) - min( $oldReset[$key], $val );
        }
        else $toAdd[$key] = intval($val);
    }
    
    var_dump( $toAdd );
    

    And here's the result.

    array(3) {
          ["UT5"]=>
          int(2)
          ["WRO"]=>
          int(3)
          ["SHO"]=>
          int(2)
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重