数组看起来像:

[0] => stdClass Object
        (
            [ID] => 420
            [name] => Mary
         )

[1] => stdClass Object
        (
            [ID] => 10957
            [name] => Blah
         )
...

我有一个整数变量$v。

我如何选择一个数组条目,其中有一个对象的ID属性有$v值?


当前回答

修复了@YurkaTim的一个小错误,你的解决方案为我工作,但增加了使用:

要在函数内部使用$searchedValue,可以在函数参数function ($e)后面使用($searchedValue)。

如果返回条件为真,array_filter函数只在$ needdobject时返回

如果$searchedValue是一个字符串或整数:

$searchedValue = 123456; // Value to search.
$neededObject = array_filter(
    $arrayOfObjects,
    function ($e) use ($searchedValue) {
        return $e->id == $searchedValue;
    }
);
var_dump($neededObject); // To see the output

如果$searchedValue是一个数组,我们需要检查一个列表:

$searchedValue = array( 1, 5 ); // Value to search.
$neededObject  = array_filter(
    $arrayOfObjects,
    function ( $e ) use ( $searchedValue ) {
        return in_array( $e->term_id, $searchedValue );
    }
);
var_dump($neededObject); // To see the output

其他回答

修复了@YurkaTim的一个小错误,你的解决方案为我工作,但增加了使用:

要在函数内部使用$searchedValue,可以在函数参数function ($e)后面使用($searchedValue)。

如果返回条件为真,array_filter函数只在$ needdobject时返回

如果$searchedValue是一个字符串或整数:

$searchedValue = 123456; // Value to search.
$neededObject = array_filter(
    $arrayOfObjects,
    function ($e) use ($searchedValue) {
        return $e->id == $searchedValue;
    }
);
var_dump($neededObject); // To see the output

如果$searchedValue是一个数组,我们需要检查一个列表:

$searchedValue = array( 1, 5 ); // Value to search.
$neededObject  = array_filter(
    $arrayOfObjects,
    function ( $e ) use ( $searchedValue ) {
        return in_array( $e->term_id, $searchedValue );
    }
);
var_dump($neededObject); // To see the output

我使用某种Java键映射来实现这一点。 如果你这样做,你不需要每次都循环遍历你的对象数组。

<?php

//This is your array with objects
$object1 = (object) array('id'=>123,'name'=>'Henk','age'=>65);
$object2 = (object) array('id'=>273,'name'=>'Koos','age'=>25);
$object3 = (object) array('id'=>685,'name'=>'Bram','age'=>75);
$firstArray = Array($object1,$object2);
var_dump($firstArray);

//create a new array
$secondArray = Array();
//loop over all objects
foreach($firstArray as $value){
    //fill second        key          value
    $secondArray[$value->id] = $value->name;
}

var_dump($secondArray);

echo $secondArray['123'];

输出:

array (size=2)
  0 => 
    object(stdClass)[1]
      public 'id' => int 123
      public 'name' => string 'Henk' (length=4)
      public 'age' => int 65
  1 => 
    object(stdClass)[2]
      public 'id' => int 273
      public 'name' => string 'Koos' (length=4)
      public 'age' => int 25
array (size=2)
  123 => string 'Henk' (length=4)
  273 => string 'Koos' (length=4)
Henk
class ArrayUtils
{
    public static function objArraySearch($array, $index, $value)
    {
        foreach($array as $arrayInf) {
            if($arrayInf->{$index} == $value) {
                return $arrayInf;
            }
        }
        return null;
    }
}

按照你想要的方式使用它是这样的:

ArrayUtils::objArraySearch($array,'ID',$v);
      $keyToLookFor = $term->name;
      $foundField = array_filter($categories, function($field) use($keyToLookFor){
      return $field -> name === $keyToLookFor; });

      if(!empty($foundField)){
      $ke = array_keys($foundField);
      $ke = $ke[0];
      $v = $foundField[$ke]->id;}

我在Wordpress主题上工作,从类别得到总评论,这有助于我为数组

我有时喜欢使用array_reduce()函数来执行搜索。它类似于array_filter(),但不会影响所搜索的数组,允许您对同一对象数组执行多次搜索。

$haystack = array($obj1, $obj2, ...); //some array of objects
$needle = 'looking for me?'; //the value of the object's property we want to find

//carry out the search
$search_results_array = array_reduce(
  $haystack,

  function($result_array, $current_item) use ($needle){
      //Found the an object that meets criteria? Add it to the the result array 
      if ($current_item->someProperty == $needle){
          $result_array[] = $current_item;
      }
      return $result_array;
  },
  array() //initially the array is empty (i.e.: item not found)
);

//report whether objects found
if (count($search_results_array) > 0){
  echo "found object(s): ";
  print_r($search_results_array[0]); //sample object found
} else {
  echo "did not find object(s): ";
}