这是print_r()
Array([0]=> stdCLass Object ([name] =>john
[surname] =>future
[group] =>one1
)
[1]=> stdCLass Object ([name] =>chris
[surname] =>past
[group] =>two2
)
)
我想要做的是按组搜索此数组。例。告诉它
group ="one1"
我的回复是name=john
,surname = future
我不知道如何尝试这个。
答案 0 :(得分:0)
假设数组被称为$groups
返回包含姓名和姓氏的数组
function get_name_from_groups($groups, $group_name)
{
foreach ( $groups as $group ) {
if ( $group->group == $group_name ) {
return array( 'name' => $group->name,
'surname' => $group->surname
);
}
}
return false;
}
$var = get_name_from_groups($groups, 'one1');
if ( $var === false ) {
echo 'The group one1 does not exist';
exit;
}
echo 'Name = ' . $var['name'];
echo 'Surname = ' . $var['surname'];
返回包含姓名和姓氏的对象
function get_name_from_groups($groups, $group_name)
{
foreach ( $groups as $group ) {
if ( $group->group == $group_name ) {
$ret = new stdClass();
$ret->name = $group->name;
$ret->surname = $group->surname;
}
}
return false;
}
$var = get_name_from_groups($groups, 'one1');
if ( $var === false ) {
echo 'The group one1 does not exist';
exit;
}
echo 'Name = ' . $var->name;
echo 'Surname = ' . $var->surname;