通过值删除PHP数组索引

3
我正在尝试取消设置包含两个值的两个特定数组位置。
我的实际代码来填充数组。
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();

如果使用print_r函数输出,该数组的样子如下:
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
    )
)

现在$store包含我需要删除的2个数组索引。它们是21和20076231。

我已经尝试了以下方法:

使用数组搜索代码,但没有成功。有人有什么想法我该尝试什么?


在创建数组时,请忽略此ID 20076231,只需使用if($store['id'] == '20076231') continue; - devpro
1
工作得非常完美,谢谢! - pr0b
你应该使用这个答案中的searchForId函数:https://dev59.com/b2w15IYBdhLWcg3wWqj- - Gino Pane
@d4ne:这是很好的知识,我也在答案中分享了。 - devpro
5个回答

1

对于这个简单的问题,有几种不同的方法。其中之一可以使用函数 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));

0
如果你需要在创建数组后取消其中的项目,可以查看array_map
首先,将数组映射为每个ID的索引值。
$map = array_map(function($item){ return $item['id']; }, $store);

然后从映射中获取您ID的索引(例如21)。

$index = array_search(21, $map);

然后使用array_splice进行删除。

array_splice($store, $index, 1);

0
在你的循环中尝试这个,它不会包含在你的数组中,所以不需要像这样取消设置:
foreach($data as $store) {
    if($store['id'] == '20076231') 
        continue; 

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

0
为什么不直接在您的$storeArray中使用ID呢?而且,既然它似乎是整数,为什么要强制将它转换为(字符串)呢?
试试这个:
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);
}

0

一行代码很酷,但有时候更喜欢显式的代码。

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

您可以一次按一个ID进行过滤

$store = filter_by_id($store, 21);

或者您可以同时过滤多个ID:

$store = filter_by_id($store, [21, 20076231]);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接