php 如何在php数组中找到对象的索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11529280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find index of object in php array?
提问by Milan
Here is print_r output of my array:
这是我的数组的 print_r 输出:
Array
(
[0] => stdClass Object
(
[itemId] => 560639000019
[name] => Item no1
[code] => 00001
[qty] => 5
[id] => 2
)
[1] => stdClass Object
(
[itemId] => 470639763471
[name] => Second item
[code] => 76347
[qty] => 9
[id] => 4
)
[2] => stdClass Object
(
[itemId] => 56939399632
[name] => Item no 3
[code] => 39963
[qty] => 6
[id] => 7
)
)
How can I find index of object with [id] => 4 in order to remove it from array?
如何找到 [id] => 4 的对象索引以将其从数组中删除?
采纳答案by fdomig
$found = false;
foreach($values as $key => $value) {
if ($value->id == 4) {
$found = true;
break;
}
}
if ($found) unset($values[$key]);
This is considered to be faster then any other solution since we only iterate the arrayto until we find the object we want to remove.
这被认为比任何其他解决方案都快,因为我们只迭代arrayto 直到找到要删除的对象。
Note:You should not remove an element of an array while iterating so we do it afterwards here.
注意:您不应该在迭代时删除数组的元素,因此我们在此之后进行。
回答by Lix
foreach($parentObj AS $key=>$element){
if ($element->id == THE_ID_YOU_ARE_LOOKING_FOR){
echo "Gottcha! The index is - ". $key;
}
}
$parentObjis obviously your root array - the one that holds all the others.
$parentObj显然是您的根数组 - 包含所有其他数组的数组。
We use the foreachloop to iterate over each item and then test it's idproperty against what ever value you desire. Once we have that - the $keythat we are on is the index you are looking for.
我们使用foreach循环来迭代每个项目,然后id根据您想要的值测试它的属性。一旦我们有了它 -$key我们正在寻找的索引就是您正在寻找的索引。
回答by Micha? Kluczka
use array_search:
使用array_search:
$a = new stdClass;
$b = new stdClass;
$a->id = 1;
$b->id = 2;
$arr = array($a, $b);
$index = array_search($b, $arr);
echo $index;
// prints out 1
回答by Baylor Rae'
Here's my solution. Given, it is a bit hackish, but it will get the job done.
这是我的解决方案。鉴于,它有点hackish,但它会完成工作。
search(array $items, mixed $id[, &$key]);
搜索(数组 $items,混合 $id[,&$key]);
Returns the item that was found by $id. If you add the variable $keyit will give you the key of the item found.
返回由 找到的项目$id。如果您添加变量$key,它将为您提供找到的项目的键。
function search($items, $id, &$key = null) {
foreach( $items as $item ) {
if( $item->id == $id ) {
$key = key($item);
return $item;
break;
}
}
return null;
}
Usage
用法
$item = search($items, 4, $key);
unset($items[$key]);
Note:This could be modified to allow a custom key and return multiple items that share the same value.
注意:这可以修改为允许自定义键并返回共享相同值的多个项目。
回答by Federkun
A funny alternative
一个有趣的选择
$getIdUnset = function($id) use ($myArray)
{
foreach($myArray as $key => $obj) {
if ($obj->id == $id) {
return $key;
}
}
return false;
};
if ($unset = $getIdUnset(4)) {
unset($myArray[$unset]);
}
回答by ThangTD
Currently php does not have any supported function for this yet.
目前 php 还没有任何支持的功能。
So refer to Java's Vector, or jQuery's $.inArray(), it would simply be:
因此,参考 Java 的 Vector,或 jQuery 的 $.inArray(),它只是:
public function indexOf($object, array $elementData) {
$elementCount = count($elementData);
for ($i = 0 ; $i < $elementCount ; $i++){
if ($object == $elementData[$i]) {
return $i;
}
}
return -1;
}
You can save this function as a core function for later.
您可以将此功能保存为核心功能供以后使用。
回答by half-a-nerd
try this
尝试这个
foreach($array AS $key=>$object){
if($object['id'] == 4){
$key_in_array = $key;
}
}
// chop it from the original array
array_slice($array, $key_in_array, 1);
回答by Marcin Necsord Szulc
Another way to achieve the result is to use array_filter.
实现结果的另一种方法是使用array_filter。
$array = array(
(object)array('id' => 5),
(object)array('id' => 4),
(object)array('id' => 3)
);
$array = array_filter($array, function($item) {
return $item->id != 4;
});
print_r($array);
回答by hjpotter92
foreach( $arr as $k=>&$a) {
if( $a['id'] == 4 )
unset($arr[$k]);
}

