doudu8291 2016-01-26 10:19
浏览 67

多维数组过滤[PHP] [重复]

This question is an exact duplicate of:

Im trying to filer out only parts of this array - by [channel] (like this):

Array
(
    [0] => Array
        (
            [268a9d2d25fc2b9765c7cd7b8a768d3e] => Array
                (
                    [dj_name] => Emilian
                    [channel] => techno
                  )

            [5d1838c3aa6344e3109ab4f0122492f7] => Array
                (
                    [dj_name] => Emilian
                    [channel] => trance
                )
        )

    [1] => Array
        (
            [e13268de7c56db42f8aeab2ab4c607f2] => Array
                (
                    [dj_name] => John Doe             
                    [channel] => trance

                )

            [1aa81b7b123321f79571828600db0f08] => Array
                (
                    [dj_name] => John Doe
                    [channel] => techno
                )

        )

So results should be (if channel is trance ) :

Array
(
    [0] => Array
        (

            [5d1838c3aa6344e3109ab4f0122492f7] => Array
                (
                    [dj_name] => Emilian
                    [channel] => trance
                )
        )

    [1] => Array
        (
            [e13268de7c56db42f8aeab2ab4c607f2] => Array
                (
                    [dj_name] => John Doe             
                    [channel] => trance

                )


        )

... i have tried with foreach loops ... to unset but didnt have much succes .. also with array_filter ...

with foreach i tried like this ( but i dont know how to update the $show_data array once the loop is closed ... hmm im pretty green at php:

foreach($show_data as $idindex => $codearray){
            foreach($codearray as $codestring => $paramarray) {       
                        foreach($paramarray as $param => $val) {    
                                if(preg_grep('/trance/', $paramarray)){
                                     unset($paramarray[$val]);
                                  }}}}
        print_r($show_data);

And I also tried with ( but if the first one is trance then it doesnt filter out the rest.. even if they are techno ... i need it to filter out at the [5d1838c3aa6344e3109ab4f0122492f7] level ) :

$genre = 'trance';
$data = array_filter($dataraw, function($fs) use ($genre) {
 return current($fs)['channel'] === $genre;
});

Thanks in advance!

</div>
  • 写回答

2条回答 默认 最新

  • duanlvxi8652 2016-01-26 10:25
    关注

    Try this: it will hepl you.

    $output_array();
    $i = 0;
    foreach ($show_data as $data) {
        foreach ($data as $subdata) {
            if ($subdata['channel'] == 'trance') {
                $output_array[$i] = $subdata;
            }
        }
        $i++;
    }
    
    echo '<pre>';
    print_r($output_array);
    die;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算