这是我的数组的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
)
)
如何找到[id]=>4的对象的索引,以便将其从数组中删除?
$found = false;
foreach($values as $key => $value) {
if ($value->id == 4) {
$found = true;
break;
}
}
if ($found) unset($values[$key]);
这被认为比任何其他解决方案都快,因为我们只将array
迭代到,直到找到要删除的对象。
注意:迭代时不应该删除数组的元素,所以我们稍后会在这里删除。
foreach($parentObj AS $key=>$element){
if ($element->id == THE_ID_YOU_ARE_LOOKING_FOR){
echo "Gottcha! The index is - ". $key;
}
}
$parentObj
显然是您的根数组,它包含所有其他数组。
我们使用foreach
循环来迭代每个项,然后根据您想要的值来测试它的id
属性。一旦我们有了它,我们所在的$key
就是您要查找的索引。
使用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
尝试这个
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);
另一种实现结果的方法是使用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);
这是我的解决方案。考虑到这一点,它有点粗鲁,但它会完成任务的。
搜索(数组$items,混合$id[,&$key])
返回$id
找到的项目。如果您添加变量$key
,它将为您提供找到的项目的密钥。
function search($items, $id, &$key = null) {
foreach( $items as $item ) {
if( $item->id == $id ) {
$key = key($item);
return $item;
break;
}
}
return null;
}
使用
$item = search($items, 4, $key);
unset($items[$key]);
注意:可以对其进行修改,以允许自定义密钥并返回共享相同值的多个项。
我创建了一个示例,以便您可以看到它的实际操作。
一个有趣的替代
$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]);
}
目前php还没有任何支持的函数。
因此,参考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;
}
您可以将此函数保存为核心函数,以备以后使用。
在我的例子中,这是我的数组$array
我对我的项目的这个问题感到困惑,但这里的一些答案帮助了我
array(3) {
[0]=> float(-0.12459619130796)
[1]=> float(-0.64018439966448)
[2]=> float(0)
}
然后使用if condition
停止循环
foreach($array as $key => $val){
if($key == 0){ //the key is 0
echo $key; //find the key
echo $val; //get the value
}
}
我知道,这么多年过去了,这可能是一个无用的答案,但为什么不呢?
这是我个人实现的一个可能的index_of
,使用与其他答案相同的代码,但让程序员选择何时以及如何进行检查,还支持复杂的检查。
if (!function_exists('index_of'))
{
/**
* @param iterable $haystack
* @param callable $callback
* @param mixed|null &$item
* @return false|int|string
*/
function index_of($haystack, $callback, &$item = null)
{
foreach($haystack as $_key => $_item) {
if ($callback($_item, $_key) === true) {
$item = $_item;
return $_key;
}
}
return false;
}
}
foreach( $arr as $k=>&$a) {
if( $a['id'] == 4 )
unset($arr[$k]);
}