dtye7921 2014-01-07 09:09
浏览 73
已采纳

从php中的给定数组获取子数组

I have a array variable $data which has the following array structure:

Array
(
    [0] => Array
        (
            [rowdata] => Array
                (
                    [0] => Array
                        (

                            [wiecont_03] => 
                            [tevrc_04] => 
                            [opmerkTXT] => Overall NPS
                            [0] => Array-----------------------
                                (                              |
                                    [0] => rapport             |
                                    [1] => npsorg              |
                                    [3] => npsdetbe            | i want this
                                                                  part of the array
                                )-------------------------------

                        )

                    [1] => Array
                        (
                            [DOELGROEP] => 2
                            [KANTOOR] => 2
                            [OBLIGOCATEGORIE] => 3
                            [NAAM] => dfdf
                            [WEIGHT] => 0.95
                            [opmerkTXT] => Overall NPS
                            [0] => Array
                                (
                                    [1] => npsorg
                                    [3] => npsdetbe
                                )

                        )

                )

            [reason] => column values are not correct
        )

)

from the above array structure i want to fetch the sub array if any as depicted by the dotted line.

Desired Output:

                                 [0] => Array
                                (                              
                                    [0] => rapport             
                                    [1] => npsorg              
                                    [3] => npsdetbe            
                                )
                                 and 

                                  [0] => Array
                                (
                                    [1] => npsorg
                                    [3] => npsdetbe
                                )

What i have tried: i tried array_slice() and array_chunks() but none of them worked in my case.

Thanks in advance

  • 写回答

3条回答 默认 最新

  • douluogu8713 2014-01-07 09:20
    关注

    You also can do it(in short) like this:

    Demo: https://eval.in/86588

    <?php 
    $arr = array
    (
        0 => array
        (
            "rowdata" => array
            (
                "0" => array
                (
    
                    "wiecont_03" => "",
                    "tevrc_04" => "",
                    "opmerkTXT" => "Overall NPS",
                    "0" => array
                    (
                        "0" => "rapport",
                        "1" => "npsorg",
                        "3" => "npsdetbe"
                    )
    
                ),
    
                "1" => array
                (
                    "DOELGROEP" => 2,
                    "KANTOOR" => 2,
                    "OBLIGOCATEGORIE" => 3,
                    "NAAM" => "dfdf",
                    "WEIGHT" => 0.95,
                    "opmerkTXT" => "Overall NPS",
                    "0" => array
                    (
                        "1" => "npsorg",
                        "3" => "npsdetbe",
                    )
    
                )
    
            )
    
    
        )
    
    );
    $re = array();
    //print_r($arr[0]['row_data']);
    $i=0;
    foreach($arr[$i]['rowdata'] as $a){
    if(is_array($a[0])){
      array_push($re,$a[0]);
      }
              $i =$i+1;
    }
    print_r($re); 
    

    TRY THIS:
    Demo : https://eval.in/86560

    $arr = array
    (
        "0" => array
        (
            "rowdata" => array
            (
                "0" => array
                (
    
                    "wiecont_03" => "",
                    "tevrc_04" => "",
                    "opmerkTXT" => "Overall NPS",
                    "0" => array
                    (
                        "0" => "rapport",
                        "1" => "npsorg",
                        "3" => "npsdetbe"
                    )
    
                ),
    
                "1" => array
                (
                    "DOELGROEP" => 2,
                    "KANTOOR" => 2,
                    "OBLIGOCATEGORIE" => 3,
                    "NAAM" => "dfdf",
                    "WEIGHT" => 0.95,
                    "opmerkTXT" => "Overall NPS",
                    "0" => array
                    (
                        "1" => "npsorg",
                        "3" => "npsdetbe",
                    )
    
                )
    
            )
    
    
        )
    
    );
    $re = array();
    foreach($arr as $a){
        foreach($a as $b){
           foreach($b as $c){
              array_push($re,$c[0]);
           }
        }
    }
    print_r($re);
    

    OUTPUT

    Array
    (
        [0] => Array
            (
                [0] => rapport
                [1] => npsorg
                [3] => npsdetbe
            )
    
        [1] => Array
            (
                [1] => npsorg
                [3] => npsdetbe
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境