PHP通过值删除数组索引
Posted
技术标签:
【中文标题】PHP通过值删除数组索引【英文标题】:PHP delete array index through value 【发布时间】:2016-03-25 05:34:38 【问题描述】:我正在尝试取消设置两个包含两个值的特定数组位置。
我填充数组的实际代码。
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 没有成功。有人知道我可以尝试什么吗?
【问题讨论】:
在创建数组时忽略此 id 20076231 只需使用 if($store['id'] == '20076231') continue; 工作完美,谢谢! 您应该使用这个答案中的 searchForId:***.com/questions/6661530/… @d4ne:很高兴知道,我也分享了答案。 【参考方案1】:一行代码很酷,但有时显式代码更可取。
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]);
【讨论】:
【参考方案2】:为什么不直接使用 $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);
【讨论】:
【参考方案3】:对于这个简单的问题,有几种不同的方法。其中之一可能正在使用函数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));
【讨论】:
【参考方案4】:如果您需要在创建后取消设置数组中的项目,请查看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);
【讨论】:
【参考方案5】:在你的循环中试试这个,它不会包含在你的数组中,而不需要像这样取消设置:
foreach($data as $store)
if($store['id'] == '20076231')
continue;
$storeArray[] = array(
"name" => (string)$store['name'],
"id" => (string)$store['id']
);
【讨论】:
以上是关于PHP通过值删除数组索引的主要内容,如果未能解决你的问题,请参考以下文章