Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a key in a subarray [closed]

Tags:

arrays

php

I have a multi-array like this:

 array(2) {
      [0]=>
      array(8) {
        [0]=>
        string(1) "3"
        ["Id"]=>
        string(1) "3"
        [1]=>
        string(8) "Portugal"
        ["Country"]=>
        string(8) "Portugal"
        [2]=>
        string(8) "sometext"
        ["Type"]=>
        string(8) "sometext"
        [3]=>
        string(1) "0"
        ["xptoabcdef"]=>
        string(1) "0"
      }
      [1]=>
      array(8) {
        [0]=>
        string(1) "4"
        ["Id"]=>
        string(1) "4"
        [1]=>
        string(8) "Portugal"
        ["Country"]=>
        string(8) "Portugal"
        [2]=>
        string(8) "sometext"
        ["Type"]=>
        string(8) "sometext"
        [3]=>
        string(2) "22"
        ["xptoabcdef"]=>
        string(2) "22"
      }

How can I delete the "Country" column from the array in the most simple way?

like image 309
2Noob2Good Avatar asked Dec 12 '22 09:12

2Noob2Good


2 Answers

Try something with array_map()

$new_array = array_map(function($v) {
    unset($v['Country']);
    return $v;
}, $old_array);

Demo.

like image 86
Glavić Avatar answered Jan 13 '23 14:01

Glavić


You could use array_map() with a callback function to achieve this:

$array = array_map(function($elem) {
   unset($elem['Country']); 
   return $elem;
}, $array);

Or use a foreach loop and pass the value by reference, like so:

foreach ($array as $key => & $value) {
    unset($value['Country']);
}

Demo!

like image 29
Amal Murali Avatar answered Jan 13 '23 15:01

Amal Murali