$var
是一个数组:
Array (
[0] => stdClass Object ( [ID] => 113 [title] => text )
[1] => stdClass Object ( [ID] => 114 [title] => text text text )
[2] => stdClass Object ( [ID] => 115 [title] => text text )
[3] => stdClass Object ( [ID] => 116 [title] => text )
)
我们如何从某些[title]
致电[ID]
? (不接触[0], [1], [2], [3]
)
例如,如果我们致电$var['114']['title]
,则应提供text text text
答案 0 :(得分:6)
你不能。
您可以创建一个具有ID作为键的新数组,之后您可以快速访问该标题,或者每次需要搜索某个ID时都可以遍历该数组。
答案 1 :(得分:3)
为什么不把它构造成:
Array (
[113] => stdClass Object ( [title] => text )
[114] => stdClass Object ( [title] => text text text )
[115] => stdClass Object ( [title] => text text )
[116] => stdClass Object ( [title] => text )
)
问题解决了。
说出您的记录在$records
。您可以将其转换为:
$newArray = array();
foreach ($records as $record) {
$id = $record->id;
unset($record->id);
$newArray[$id] = $record;
}
print_r($newArray);
答案 2 :(得分:1)
如果我理解你的话,那就是我的榜样:
<?php
// This is your first Array
$var = array();
// And here some stdClass Objects in this Array
$var[] = (object) array( 'ID' => 113, 'title' => 'text' );
$var[] = (object) array( 'ID' => 114, 'title' => 'text text text' );
$var[] = (object) array( 'ID' => 115, 'title' => 'text text' );
$var[] = (object) array( 'ID' => 116, 'title' => 'text' );
// Now here the new Array
$new_var = array();
foreach( $var as $k => $v )
{
$new_var[$v->ID] = (array) $v;
}
// Now you can use $new_var['113']['title'] and so on
echo $new_var['113']['title'];
?>