我有一个这样的数组:-
$str = array(
array(
'amount' => 1.87,
'user' => 'hello',
),
array(
'amount' => 0.9,
'user' => 'test' ,
),
array(
'amount' => 9,
'user' => 'hello',
),
array(
'amount' => 1.4,
'user' => 'test1',
)
);
现在我想在用户“hello”拥有的 html 表中显示这两个金额。我尝试了以下搜索方法:-
$ac = array_search("hello", $str);
echo $str["$ac"];
但它不起作用。无论如何要为用户“你好”显示这样的结果:-
1.87
9
以便我稍后可以在 html 表格中显示。
最佳答案
您可以使用 array_column()
$users = array_column($str,'user'); // get all the user list from array
$search = "hello"; // user you want to search
foreach($users as $key=>$value){ // iterate over user array
if($value == $search){ // compare user name with search value
echo $str[$key]['amount'] .PHP_EOL; // if matched print the corresponding amount
}
}
关于php - 在php中显示多个键和数组列的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52125058/