mckinnej
mckinnej

Reputation: 33

Unable to get unset to work with array of arrays

I have a database function that returns an array of arrays that looks like this:

    Array
    (
        [4896] => Array
            (
                [0] => 4896
                [id] => 4896
                [1] => Party
                [event_name] => Party
                [2] => 2018-02-19 08:00:00
                [start_date] => 2018-02-19 08:00:00
                [3] => 2018-02-23 16:00:00
                [end_date] => 2018-02-23 16:00:00
                [4] => 4896
                [parent_event] => 4896
                [5] => 0
                [event_order] => 0
            )

        [4897] => Array
            (
                [0] => 4897
                [id] => 4897
                [1] => Ball
                [event_name] => Ball
                [2] => 2018-02-19 08:00:00
                [start_date] => 2018-02-19 08:00:00
                [3] => 2018-02-21 16:00:00
                [end_date] => 2018-02-21 16:00:00
                [4] => 4896
                [parent_event] => 4896
                [5] => 0
                [event_order] => 0
            )
)

As you can see, it duplicates values in a combination of indexed and associative arrays. This complicates processing later on, so I'd like to get rid of the indexed items and leave just the associative items. I wrote the following code to do this. ($list is the array)

print('count= ' . count($list)); print('<br >');
foreach ($list as $list_key => $item) {
    foreach ($item as $item_key => $sub_item) {
        if (is_int($item_key)) {
            unset($list[$list_key][$item_key]);
        }
    }
}
print('count= ' . count($list)); print('<br >');

Problem is that it doesn't do anything. It correctly identifies the indexed array items, but nothing changes after the unset. The count is exactly the same before and after.

I feel that it is related to how the array is referenced, but I'd tried every combination I could think of without success. I either get errors or the same result. Any help? TIA.

Upvotes: 0

Views: 113

Answers (3)

Xorifelse
Xorifelse

Reputation: 7911

This looks like a fetch result returned from a database using the fetching style BOTH. Don't try to patch the situation, instead you should you should use a fetch style called assoc which stands for associative which means named indexes.

In PDO:

$stmt->fetch(\PDO::FETCH_ASSOC)

In MySQLi:

mysqli_fetch_array($result, \MYSQLI_ASSOC)
mysqli_fetch_assoc($result)

Usually, this is the default setting so you might want to check you're using some sort of modifier that changes the default database settings.

Upvotes: 0

abr
abr

Reputation: 2129

You can edit a value by adding an & at the value marker like this. However, I recommend you creating an external array and just array_push whatever results you'd like.

//the & below is only intended if you plan on altering the values, if you follow
//the example to pass onto the out-cycle array, you don't need it
$externalList = [];
print('count= ' . count($list)); print('<br >');
foreach ($list as $list_key => &$item) {
    // array_push ($list_key => &$item) to that list
    foreach ($item as $item_key => &$sub_item) {
        if (!is_int($item_key)) {
            // array_push ($item_key => &$sub_item) to that $item
            unset($list[$list_key][$item_key]);
        }
    }
}
print('count= ' . count($list)); print('<br >');

http://php.net/manual/en/language.references.pass.php

http://php.net/manual/en/function.array-push.php

Upvotes: 1

Barmar
Barmar

Reputation: 780851

The array you're counting is not the ones you're unsetting from.

Use:

print_r(array_map('count', $list));

to see the number of elements in the row arrays. This should be reduced after the loop.

Upvotes: 0

Related Questions