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 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答