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

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

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了