-1

I want to remove a specific array key from all the inner arrays. I know there is a function called unset() which uses index or object name to slice it out. But I am not able to implement in my case in the below code.

[answers] => Array
            (
                [0] => Array
                    (
                        [title] => First Answer 1
                        [image] => 
                        [isCorrect] => 1
                    )

                [1] => Array
                    (
                        [title] => Second Answer
                        [image] => 
                        [isCorrect] => 0
                    )

                [2] => Array
                    (
                        [title] => Third Answer
                        [image] => 
                        [isCorrect] => 0
                    )

                [3] => Array
                    (
                        [title] => Fourth Answer
                        [image] => 
                        [isCorrect] => 0
                    )

            )

How can I remove [image] from each of the arrays in [answers]?

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
topper1309
  • 151
  • 3
  • 16
  • 1
    Yes, it's possible. Just iterate through the array and use `unset()` in the loop. You should make an attempt and come back if you can't get it working. Remember that you then need to show us what you tried and where you're stuck. – M. Eriksson Aug 30 '18 at 18:51
  • Show us what you already tried! – Thiago Natanael Aug 30 '18 at 18:56
  • @topper1309, I adjusted the terminology in your question a bit based on the example array you showed. The items in the array are arrays, and I thought referring to them as objects was potentially misleading. If you really do have an array of objects, you should edit your question to show that instead. – Don't Panic Aug 30 '18 at 19:15

3 Answers3

1

There's really no reason for unset to not work (assuming that's what you meant by "I am not able to implement in my case") unless you did it like this:

foreach ($array['answers'] as $answer) {
     unset($answer['image'];
}

It can't work that way, because the $answer you have there does not refer to the original array. It's a copy of the item, because the foreach loop makes a copy of each value in the array as it goes.

You need to get the key of the outer array and use that as well in your unset expression.

foreach ($array['answers'] as $key => $answer) {
     unset($array['answers'][$key]['image'];
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

This code works, by looking up the values in the array and removing the value.

foreach($answers as $key => $answer) {
  unset($answers[$key]['image']);
}

Observe that $answer variable here, is just a temporary copy of the array element inside the foreach loop...

0

You can just use the unset E.g.: If you want to remove the index 1, with the second answer, you could just do

unset($answers[1]);
Guildsmac
  • 11
  • 2
  • 1
    That's not what the OP asks for. The OP wants to remove `image` from all sub arrays. This would simply remove the complete array on index 1. – M. Eriksson Aug 30 '18 at 19:01