PHP - 从对象数组中按对象属性查找条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4742903/
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
PHP - find entry by object property from an array of objects
提问by Alex
The array looks like:
该数组看起来像:
[0] => stdClass Object
(
[ID] => 420
[name] => Mary
)
[1] => stdClass Object
(
[ID] => 10957
[name] => Blah
)
...
And I have a integer variable called $v
.
我有一个名为$v
.
How could I select an array entry that has a object where the ID
property has the $v
value ?
我怎样才能选择一个数组条目,该条目具有ID
属性具有$v
值的对象?
回答by Phil
You either iterate the array, searching for the particular record (ok in a one time only search) or build a hashmap using another associative array.
您可以迭代数组,搜索特定记录(可以只搜索一次)或使用另一个关联数组构建哈希图。
For the former, something like this
对于前者,像这样
$item = null;
foreach($array as $struct) {
if ($v == $struct->ID) {
$item = $struct;
break;
}
}
See this question and subsequent answers for more information on the latter - Reference PHP array by multiple indexes
有关后者的更多信息,请参阅此问题和随后的答案 -按多个索引引用 PHP 数组
回答by Daniel Hardt
YurkamTimis right. It needs only a modification:
YurkamTim是对的。它只需要修改:
After function($) you need a pointer to the external variable by "use(&$searchedValue)" and then you can access the external variable. Also you can modify it.
在 function($) 之后,您需要通过“use(&$searchedValue)”指向外部变量,然后才能访问外部变量。你也可以修改它。
$neededObject = array_filter(
$arrayOfObjects,
function ($e) use (&$searchedValue) {
return $e->id == $searchedValue;
}
);
回答by Tim
$arr = [
[
'ID' => 1
]
];
echo array_search(1, array_column($arr, 'ID')); // prints 0 (!== false)
回答by YurkaTim
回答by Museful
Using array_columnto re-index will save time if you need to find multiple times:
如果您需要多次查找,使用array_column重新索引将节省时间:
$lookup = array_column($arr, NULL, 'id'); // re-index by 'id'
Then you can simply $lookup[$id]
at will.
然后你就可以随意$lookup[$id]
了。
回答by Pablo S G Pacheco
class ArrayUtils
{
public static function objArraySearch($array, $index, $value)
{
foreach($array as $arrayInf) {
if($arrayInf->{$index} == $value) {
return $arrayInf;
}
}
return null;
}
}
Using it the way you wanted would be something like:
以您想要的方式使用它是这样的:
ArrayUtils::objArraySearch($array,'ID',$v);
回答by Kamil Kie?czewski
回答by Jose Carlos Ramos Carmenates
Fixing a small mistake of the @YurkaTim, your solution work for me but adding use
:
修复@YurkaTim 的一个小错误,您的解决方案对我有用,但添加use
:
To use $searchedValue
, inside of the function, one solution can be use ($searchedValue)
after function parameters function ($e) HERE
.
要使用$searchedValue
,在函数内部,一种解决方案可以是use ($searchedValue)
在函数参数之后function ($e) HERE
。
the array_filter
function only return on $neededObject
the if the condition on return is true
该array_filter
函数仅$neededObject
在返回条件为true
If $searchedValue
is a string or integer:
如果$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
If $searchedValue
is array where we need check with a list:
如果$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
回答by yuvilio
I sometimes like using the array_reduce()function to carry out the search. It's similar to array_filter() but does not affect the searched array, allowing you to carry out multiplesearches on the same array of objects.
我有时喜欢使用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): ";
}
回答by Mart-Jan
I did this with some sort of Java keymap. If you do that, you do not need to loop over your objects array every time.
我用某种 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'];
output:
输出:
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