如何在php数组中找到对象的索引?

时间:2012-07-17 19:14:16

标签: php arrays

这是我的数组的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为了从阵列中删除它?

11 个答案:

答案 0 :(得分:9)

$found = false;  
foreach($values as $key => $value) {
    if ($value->id == 4) {
        $found = true;
        break;
    }
}

if ($found) unset($values[$key]);

这被认为比任何其他解决方案更快,因为我们只迭代array直到我们找到要删除的对象。

注意:迭代时不应删除数组中的元素,所以我们之后再这样做。

答案 1 :(得分:6)

foreach($parentObj AS $key=>$element){
  if ($element->id == THE_ID_YOU_ARE_LOOKING_FOR){
    echo "Gottcha! The index is - ". $key;
  }
}

$parentObj显然是你的根数组 - 包含所有其他数组的数组。

我们使用foreach循环迭代每个项目,然后根据您想要的值来测试它的id属性。一旦我们拥有了 - 我们所在的$key就是您正在寻找的索引。

答案 2 :(得分:0)

试试这个

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

答案 3 :(得分:0)

实现结果的另一种方法是使用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);

答案 4 :(得分:0)

使用 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

答案 5 :(得分:0)

这是我的解决方案。鉴于,它有点苛刻,但它将完成工作。

search(array $ items,mixed $ 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]);

注意:可以修改此选项以允许自定义键并返回共享相同值的多个项目。

I've created an example so you can see it in action

答案 6 :(得分:0)

一个有趣的选择

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

答案 7 :(得分:0)

目前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;
}

您可以将此功能保存为核心功能以供日后使用。

答案 8 :(得分:0)

在我的情况下,我的数组为$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
           }
        }

答案 9 :(得分:0)

我知道,经过这么多年,这可能是一个无用的答案,但为什么不呢?

这是我个人对可能的 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;
    }
}

答案 10 :(得分:-1)

foreach( $arr as $k=>&$a) {
    if( $a['id'] == 4 )
        unset($arr[$k]);
}