user2509780
user2509780

Reputation: 121

Overwrite array element with its deepest nested value

I have an array like:

Array
(
   [name] => Linda
   [place] => stdClassObject
      {
        [country] => stdClassObject
           (
              [answer] => Spain
           )
       }
   [age] => 16
   [gender] => Array
      (
           [0] => female
      )
)

Now, I need to change the key "place" in the array in such a way that its either like:

[place] => Spain

or

[place] => Array
  (
       [0] => Spain
  )

Basically, I have to change it to the same format the other keys are in the array. Can anybody help me? I'm new to this.

Upvotes: 0

Views: 36

Answers (1)

Jeff Lambert
Jeff Lambert

Reputation: 24661

$arr['place'] = $arr['place']->country->answer

or your other way:

$arr['place'] = array( $arr['place']->country->answer )

Upvotes: 2

Related Questions