droc60607 2014-06-19 11:54
浏览 18
已采纳

从数组中选择与php中另一个数组中的条件匹配的数据

i have 2 arrays i want to display the final array as what are the array element in $displayArray only be displayed from the $firstArray

$firstArray = Array
(
[0] => Array
    (
        [Dis_id] => Dl-Dis1
        [Dis_Desc] => Discount
        [Dis_Per] => 7.500
        [Dis_val] => 26.25
    )

[1] => Array
    (
        [Dis_id] => Dl-Dis2
        [Dis_Desc] => Discount
        [Dis_Per] => 2.500
        [Dis_val] => 8.13
    )

)

$displayArray = Array
(
[0] => Array
    (
        [0] => Dis_id
        [1] => Dis_val
    )

)

i want the final output will be

$resultArray = Array
(
[0] => Array
    (
        [Dis_id] => Dl-Dis1
        [Dis_val] => 26.25
    )

[1] => Array
    (
        [Dis_id] => Dl-Dis2
        [Dis_val] => 8.13
    )

)

Both the $firstArray and the $DisplayArray are dynamic but the $displayArray should be one.

i dont know how to do give me any suggestion

  • 写回答

1条回答 默认 最新

  • duan201444 2014-06-19 12:03
    关注

    First up, if $displayArray will never have more than one array, the answer is pretty simple. Start by popping the inner array, to get to the actual keys you will need:

    $displayArray = array_pop($displayArray);//get keys
    $resultArray = array();//this is the output array
    foreach ($firstArray as $data)
    {
        $item = array();
        foreach ($displayArray as $key)
            $item[$key] = isset($data[$key]) ? $data[$key] : null;//make sure the key exists!
        $resultArray[] = $item;
    }
    var_dump($resultArray);
    

    This gives you what you need.
    However, if $displayArray contains more than 1 sub-array, you'll need an additional loop

    $resultArray = array();
    foreach ($displayArray as $k => $keys)
    {
        $resultArray[$k] = array();//array for this particular sub-array
        foreach ($firstArray as $data)
        {
            $item = array();
            foreach ($keys as $key)
                $item[$key] = isset($data[$key]) ? $data[$key] : null;
            $resultArray[$k][] = $item;//add data-item
        }
    }
    var_dump($resultArray);
    

    the latter version can handle a display array like:

    $displayArray = array(
        array(
            'Dis_id',
            'Dis_val'
        ),
        array(
            'Dis_id',
            'Dis_desc'
        )
    );
    

    And it'll churn out a $resultArray that looks like this:

    array(
        array(
            array(
                'Dis_id'  => 'foo',
                'Dis_val' => 123
            )
        ),
        array(
            array(
                'Dis_id'   => 'foo',
                'Dis_desc' => 'foobar'
            )
        )
    )
    

    Job done

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵