R R
R R

Reputation: 2956

getting sub array from the given array in 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

Upvotes: 0

Views: 80

Answers (3)

Awlad Liton
Awlad Liton

Reputation: 9351

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
        )

)

Upvotes: 1

Bruce
Bruce

Reputation: 63

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

Upvotes: 1

Sibiraj PR
Sibiraj PR

Reputation: 1481

Your pattern is same in all case then this function is fine

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

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

  return $resultArray;
}

Usage:-

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

Upvotes: 1

Related Questions