我有一个像这样的数组:-
$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"];
但是它不起作用。无论如何,用户'hello'会显示这样的结果:-
1.87
9
以便以后可以在html表中显示。
答案 0 :(得分:4)
foreach($str as $new_str){
if($new_str['user']=="hello"){
echo $new_str['amount'];
echo "<br />";
}
}
答案 1 :(得分:2)
您可以使用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
}
}
答案 2 :(得分:0)
在这里您可以得到
$ac = array_column($record, ‘Hello’);
Print($ac);
答案 3 :(得分:0)
<?php
$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',
)
);
$key = array_keys(array_column($str, 'user'), 'hello');
foreach($key as $value){
echo $str[$value]["amount"]."<br/>";
}
?>