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 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?
  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 Multisim红外倒车雷达仿真中距离问题
  • ¥15 请上面代码做什么处理或什么混淆