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

时间:2014-01-07 09:09:01

标签: php

我有一个数组变量 $ data ,它具有以下数组结构:

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
        )

)
从上面的数组结构

我想获取子数组,如虚线所示。

期望输出:

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

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

我尝试了什么:我尝试了array_slice()array_chunks(),但我的情况都没有。

提前致谢

3 个答案:

答案 0 :(得分:1)

你的模式在所有情况下是相同的,然后这个功能很好

function getMyArray($array){
  $rowdata = $array[0]['rowdata'];

  $resultArray = array(); 
  foreach($rowdata as $val){
    $resultArray[] = $val[0];
  }

  return $resultArray;
}

用法: -

$array = "YOUR ARRAY HERE";
$result = getMyArray($array);

答案 1 :(得分:1)

你也可以这样做(简而言之):

演示: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); 

尝试这个:
演示: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);

<强>输出

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

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

)

答案 2 :(得分:1)

foreach($data[0]['rowdata'] as $value){
    print_r($value[0]);
}