0

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
                 )

         )


 )
3
  • 2
    Rather than thinking in terms of removing things, just create a new array and populate it with only the structure and items you want from the original. Commented Apr 27, 2022 at 14:02
  • @ADyson The array is the result of a SOAP response, I am unable to create it Commented Apr 27, 2022 at 14:06
  • 2
    That doesn't stop you creating a new array from the parts of the original, as per my suggestion. Commented Apr 27, 2022 at 14:08

1 Answer 1

2

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'];
Sign up to request clarification or add additional context in comments.

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.