Santosh Pillai
Santosh Pillai

Reputation: 1391

remove outer array by key

My array is like the one below

 Array
 (
     [0] => Array
         (
             [cwgetOptionsResponse] => Array
                 (
                     [cwdetails] => Array
                         (
                             [cwNameDetail] => Array
                                 (
                                     [cwName] => Array
                                         (
                                        [cwNameId] => 1
                                         )

                                     [cwPostCode] => PDP/E225
                                     [cwPrints] => Array
                                         (
                                             [cwSurname] => 1088138401
                                             [cwColourStatus] => passed
                                         )

                                 )

                         )

                 )

         )

 )

I am looking to remove cwgetOptionsResponse, cwdetails and cwNameDetail to get an array like the one below. I have tried array_shift but this removes the outer elements. Is there any way to remove the arrays by keys?

 Array
 (
     [0] => Array
         (
             [cwName] => Array
                 (
                     [cwNameId] => 1
                 )

             [cwPostCode] => PDP/E225
             [cwPrints] => Array
                 (
                     [cwSurname] => 1088138401
                     [cwColourStatus] => passed
                 )

         )


 )

Upvotes: 0

Views: 95

Answers (1)

Azhar Khan
Azhar Khan

Reputation: 54

Solution 1:

You can use array_shift()

Example:

$oldarray = array(array('cwgetOptionsResponse' => array("cwdetails" => array("cwNameDetail" => array("cwName" => array("cwNameId" => 1))))));

print_r(($a));

$removezero = array_shift($oldarray );
$removecwgetOptionsResponse = array_shift($oldarray);
$removecwdetails = array_shift($oldarray);
$cwNameDetail = array_shift($oldarray);

and $cwNameDetail will contain the array as you want, or you can combine it in single variable if needed.

Solution 2:

Insert the values in new array:

$newarray = $oldarray[0]['cwgetOptionsResponse']['cwdetails']['cwNameDetail'];

Upvotes: 2

Related Questions