3

I'm trying to unset two specific array positions which contain two values.

My actual code to fill the array.

function get_store_list(){
    $data = @file_get_contents('http://www.zwinky.com/xml/clothingList.xml');
    $data = @simplexml_load_string($data);
    $data = $data->xpath('//stores/store');

    foreach($data as $store) {
        $storeArray[] = array(
           "name" => (string)$store['name'],
           "id"   => (string)$store['id']
        );
     }

    return $storeArray;
}

$store = get_store_list();

The array looks like the following incase ill echo it out using print_r function:

Array
(
[0] => Array
    (
        [name] => Artizans
        [id] => 20037336
    )

[1] => Array
    (
        [name] => Bwabies!
        [id] => 20080134
    )

[2] => Array
    (
        [name] => Crave Mart
        [id] => 20097365
    )

[3] => Array
    (
        [name] => David & Goliath
        [id] => 20099998
    )

[4] => Array
    (
        [name] => Domo
        [id] => 20098166
    )

[5] => Array
    (
        [name] => Emily the Strange
        [id] => 20101926
    )

[6] => Array
    (
        [name] => Garfield
        [id] => 20098167
    )

[7] => Array
    (
        [name] => Jetsetter
        [id] => 26
    )

[8] => Array
    (
        [name] => Like Dat
        [id] => 3
    )

[9] => Array
    (
        [name] => Paris Hilton
        [id] => 21
    )

[10] => Array
    (
        [name] => Peppermint Place
        [id] => 12
    )

[11] => Array
    (
        [name] => Rocawear
        [id] => 19
    )

[12] => Array
    (
        [name] => ShoeBuy
        [id] => 10
    )

[13] => Array
    (
        [name] => Skelanimals
        [id] => 20100198
    )

[14] => Array
    (
        [name] => Snoop Dogg
        [id] => 20
    )

[15] => Array
    (
        [name] => SW&TH
        [id] => 20096121
    )

[16] => Array
    (
        [name] => The Castle
        [id] => 1
    )

[17] => Array
    (
        [name] => The Lair
        [id] => 4
    )

[18] => Array
    (
        [name] => The Mix
        [id] => 923
    )

[19] => Array
    (
        [name] => The Powerpuff Girls
        [id] => 20098121
    )

[20] => Array
    (
        [name] => The Surf Shop
        [id] => 5
    )

[21] => Array
    (
        [name] => Tie The Knot
        [id] => 20076231
    )

[22] => Array
    (
        [name] => tokidoki
        [id] => 20099224
    )

[23] => Array
    (
        [name] => University Club
        [id] => 2
    )

[24] => Array
    (
        [name] => Z Avenue
        [id] => 6
    )

[25] => Array
    (
        [name] => Z's Greetings
        [id] => 20099506
    )
)

Now $store does contain 2 array indexes which I have to delete. Which are the following ids: 21 and 20076231

I've been trying the following already:

Array search code without beeing success. Does anyone have a idea what I could try?

Community
  • 1
  • 1
pr0b
  • 377
  • 2
  • 3
  • 14

5 Answers5

1

There are a handful different approaches for this simple issue. One of them could be using function array_filter():

/**
 * @param array $list        the list to process
 * @param array $IDsToRemove the IDs of elements to remove from $list
 * @return array a subset of $list that does not contain elements having 'id' in $IDsToRemove
 */
function removeFromArray(array $list, array $IDsToRemove)
{
    return array_filter(
        // Filter the input list...
        $list,
        // ... using a function...
        function (array $item) use ($IDsToRemove) {
            // ... that accepts an element if its "id" is not in $IDsToRemove
            return ! in_array($item['id'], $IDsToRemove);
        }
    );
}

// Usage
$filteredStore = removeFromArray($store, array(21, 20076231));
axiac
  • 68,258
  • 9
  • 99
  • 134
0

If you need to unset an item from your array after it's been created, take a look at array_map.

First map your array to retrieve the index of each ID.

$map = array_map(function($item){ return $item['id']; }, $store);

Then get the index of your ID from the map (e.g. 21).

$index = array_search(21, $map);

Then remove with array_splice.

array_splice($store, $index, 1);
Matt McDonald
  • 4,791
  • 2
  • 34
  • 55
0

Try this in your loop, this will not include in your array than no need to unset like this:

foreach($data as $store) {
    if($store['id'] == '20076231') 
        continue; 

    $storeArray[] = array(
       "name" => (string)$store['name'],
       "id"   => (string)$store['id']
    );
 }
devpro
  • 16,184
  • 3
  • 27
  • 38
0

why not use directly the id in your $storeArray? And as it seems to be integer why do you force it to (string)?

Try this:

function get_store_list(){
    $data = @file_get_contents('http://www.zwinky.com/xml/clothingList.xml');
    $data = @simplexml_load_string($data);
    $data = $data->xpath('//stores/store');

    foreach($data as $store) {
        $storeArray[(int)$store['id']] = array(
           "name" => (string)$store['name']
        );
     }

    return $storeArray;
}
// delete the keys you want
unset ($storeArray[21], $storeArray[20076231]);
// or if you have more ids to delete you can create a deleteArray
$deleteArray = array(2, 20076231);
foreach ($deleteArray as $toDelete){
    unset($storeArray($toDelete);
}
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
0

One line code is cool but sometimes explicit code is preferable.

function filter_by_id(array $data, $id)
{
        foreach ($data as $k => &$v) {
                foreach ((array) $id as $i) {
                        if ($v['id'] === $i) {
                                $v = null;
                        }
                }
        }

        // 'array_filter()' produces a new array without the null entries.
        // 'array_values()' produces a new array with indexes without gaps.
        return array_values(array_filter($data));
}

You can filter by one id at time

$store = filter_by_id($store, 21);

Or you can filter multiple ids at the same time:

$store = filter_by_id($store, [21, 20076231]);
Daniele Orlando
  • 2,692
  • 3
  • 23
  • 26