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?

6
  • 1
    Two questions - why is it there and why are you trying to remove it? Commented Mar 13, 2019 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 Commented Mar 13, 2019 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 Commented Mar 13, 2019 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 Commented Mar 13, 2019 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 Commented Mar 13, 2019 at 15:41

2 Answers 2

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

Comments

2

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);

1 Comment

A reason: it's being converted to json and passed down to the javascript client, and one doesn't want that property exposed.

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.