0

I have an array of 50000 arrays and i want to remove the "id" key-value pair from each of them.

I would rather not loop through 50k elements and was wondering if there was an efficient way to do it.

Array
(
    [0] => Array
        (
            [id] => 713061
            [market] => usd-btc
            [price] => 3893.69
        )

    [1] => Array
        (
            [id] => 713056
            [market] => usd-btc
            [price] => 3893.69
        )

    [2] => Array
        (
            [id] => 713051
            [market] => usd-btc
            [price] => 3893.69
        )

    [3] => Array
        (
            [id] => 713046
            [market] => usd-btc
            [price] => 3893.69
        )

    [4] => Array
        (
            [id] => 713041
            [market] => usd-btc
            [price] => 3892.95
        )

    [5] => Array
        (
            [id] => 713036
            [market] => usd-btc
            [price] => 3892.95
        )

I tried both the following but does not seem to be working:

// Remove ID
        foreach($server_data as $sd)
        {
            unset($sd['id']);
        }

        unset($server_data['id']);

        PRINT_R($server_data);

The $server_data is still returning the array with the $id element;

Any thoughts?

Sam.tuver
  • 679
  • 2
  • 9
  • 19
  • 1
    Two questions - why is it there and why are you trying to remove it? – Sindhara Mar 13 '19 at 15:36
  • _I would rather not loop through 50k elements_ well either you are going to have to or the PHP builtin function you might call is going to have to – RiggsFolly Mar 13 '19 at 15:38
  • 1
    It might be better to look at how you built this array and then just NOT add the `id` in the first place – RiggsFolly Mar 13 '19 at 15:39
  • @kuh-chan - the id comes from the online server where i'm fetching the data from - i need it to break the fetchData into multiple parts. I'm trying to remove it as my localDB already has an auto-increment id – Sam.tuver Mar 13 '19 at 15:40
  • 1
    Just because you are using it to seperate data does not mean you have to add it to the resultant data – RiggsFolly Mar 13 '19 at 15:41
  • How does the data look like you are fetching? – Sindhara Mar 13 '19 at 15:43

2 Answers2

3

This creates a copy of the subarray, so when you change it, the main array is not affected:

foreach ($server_data as $sd)
{
    unset($sd['id']);
}

You can unset from the original array:

foreach (array_keys($server_data) as $index)
{
    unset($server_data[$index]['id']);
}

Or pass the subarray a reference so that the original is changed:

foreach ($server_data as &$sd)
{
    unset($sd['id']);
}

Or, more tersely:

array_walk($server_data, function (&$item) { unset($item['id']); });
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
1

There's no reason I can think of to remove it (just ignore it), however you can run it through a callback that removes id and returns the rest:

$server_data = array_map(function($v) { unset($v['id']); return $v; }, $server_data);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87