doudu2404 2013-09-16 19:36
浏览 276
已采纳

使用PHP中另一个数组的值替换数组中的值

I have two arrays. Array 1 is a mapping between DeptID and dept_number:

array(66) {
[0] => array(2) {
 'DeptID' → str•4 '1300'
 'dept_number' → str•3 '18'
}

[1] => array(2) {
 'DeptID' → str•4 '1000'
 'dept_number' → str•3 '129'
}

[2] => array(2) {
 'DeptID' → str•4 '2400'
 'dept_number' → str•3 '101'
}

...and so on

Array 2 has store records which include dept_number. I want to replace the dept_number value with the DeptID value from Array 1.

array(27) {
[0] => array(11) {
 'org_id' → str•2 '11'
 'org_number' → str•2 '11'
 'supplier_number' → str•4 '3806'
 'supplier_name' → str•17 'TEST1'
 'invoice_number' → str•8 'DXXX'
 'receive_date' → str•10 '2013-09-11'
 'inv_status' → str•7 'Pending'
 'final_cost' → str•6 '317.30'
 'final_qty' → str•5 '12.00'
 'dept_number' → str•2 '18'
 'dept_descr' → str•10 'BULK 1'
}

[1] => array(11) {
 'org_id' → str•2 '11'
 'org_number' → str•2 '11'
 'supplier_number' → str•4 '1070'
 'supplier_name' → str•24 'TEST2'
 'invoice_number' → str•7 'DXXY'
 'receive_date' → str•10 '2013-09-11'
 'inv_status' → str•7 'Pending'
 'final_cost' → str•6 '830.30'
 'final_qty' → str•5 '26.00'
 'dept_number' → str•2 '101'
 'dept_descr' → str•10 'BULK 2'
}

...and so on

How do I replace the dept_number in Array 2 with the DeptID from Array 1?

  • 写回答

3条回答 默认 最新

  • doudui6756 2013-09-16 19:44
    关注

    Hash tables.

    Start making a KVP (Key Value Pair) array of your mapping array with the key being whatever value you want to look up values for (in this case, dept_number) and set the value of the array key to DeptID:

    $deptKVP = array();
    foreach ($deptMappings as $deptMapping) {
        $deptKVP[$deptMapping['dept_number']] = $deptMapping['DeptID'];
    }
    
    /*
        $deptKVP = [
            18 => 1300,
            129 => 1000,
            101 => 2400
        ];
    */
    

    And then simply iterate through your regular array:

    foreach ($array2 as &$subArray) {
        $subArray['dept_number'] = $deptKVP[$subArray['dept_number']];
    }
    unset($subArray); //Unset the reference
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douxin20081125 2013-09-16 19:44
    关注

    If your dept_number is unique:

    foreach ($array1 as $a1)
        $array3[ $a1['dept_number'] ] = $a1['DeptId'];
    

    Now you have an associated array1 in array3. Next, iterate array2

    foreach($array2 as $key=>$value)
        $array2[$key]['DeptId'] = $array3[ $value['dept_number'] ];
    
    评论
  • dotxxh0998 2013-09-16 19:47
    关注

    You're basically going to have to loop through Array 2, and check each dept_number value against each dept_number in Array 1:

    foreach($array2 as $arr2_record){
        foreach($array1 as $arr1_record){
            if($arr2_record['dept_number'] == $arr1_record['dept_number']){
                $arr2_record['DeptID'] = $arr1_record['DeptID'];
                $arr2_record['dept_number'] = null;
            }
        }
    }
    

    EDIT: If you want to maintain the "dept_number" key, then you would just set the $arr2_record['dept_number'] to the "DeptID" of $arr1_record

    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 网络打印机Ip地址自动获取出现问题
  • ¥15 求局部放电案例库,用于预测局部放电类型
  • ¥100 QT Open62541
  • ¥15 stata合并季度数据和日度数据
  • ¥15 谁能提供rabbitmq,erlang,socat压缩包,记住版本要对应
  • ¥15 Vue3 中使用 `vue-router` 只能跳转到主页面?
  • ¥15 用QT,进行QGIS二次开发,如何在添加栅格图层时,将黑白的矢量图渲染成彩色
  • ¥50 监控摄像头 乐橙和家亲版 保存sd卡的文件怎么打开?视频怎么播放?
  • ¥15 Python的Py-QT扩展库开发GUI
  • ¥60 提问一下周期性信信号的问题