gpt4 book ai didi

PHP通过值删除数组索引

转载 作者:搜寻专家 更新时间:2023-10-31 21:27:11 24 4
gpt4 key购买 nike

我正在尝试取消设置包含两个值的两个特定数组位置。

我填充数组的实际代码。

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 个我必须删除的数组索引。以下是哪些 id:21 和 20076231

我已经尝试过以下方法:

Array search code没有成功。有谁知道我可以尝试什么?

最佳答案

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

关于PHP通过值删除数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34356798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com