0

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

9
  • Does the large array follow a specific pattern? Commented Jan 7, 2014 at 9:10
  • @DainisAbols no it doesnot.that is the main problem. Commented Jan 7, 2014 at 9:11
  • Well, is it at least always in depth 3 ? Commented Jan 7, 2014 at 9:12
  • @DainisAbols no,only the structure will always remain the same Commented Jan 7, 2014 at 9:12
  • just functions won't tell us much, what is the concrete code that you tried (i.e. show how you accessed that array, which part you sliced/chunked/derefenced) and what you did to that part to get the final data out; what did that code give you, and why do you think it gave you that? Commented Jan 7, 2014 at 9:13

3 Answers 3

1

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
        )

)
Sign up to request clarification or add additional context in comments.

9 Comments

thanks a lot.+1 will mark this as answer after few minutes if i wont get other better solution.thanks again.
awlad thanks again.need one more help.how to delete the last 2 elemnt from the above array?i tried unset ($data_failures[count($data_failures)-2]); but its not working
you want to delete from output array?
awlad: no.i want to delete from the original array i.e. $data in your case $arr
This array: "0" => array ( "1" => "npsorg", "3" => "npsdetbe", ) ?
|
1

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);

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.