[英]Print one value from qualifying row or else all values from a specific column
I am using a foreach loop to echo names from my multi-dimensional array.我正在使用 foreach 循环从我的多维数组中回显名称。
Sample Array:样本数组:
$readJson = [
[
'id' => 78,
'name' => 'audi',
'sscat_id' => 59,
'date' => '0000-00-00 00:00:00'
],
[
'id' => 106,
'name' => 'apache',
'sscat_id' => 86,
'date' => '0000-00-00 00:00:00'
],
[
'id' => 16,
'name' => 'asia',
'sscat_id' => 62,
'date' => '0000-00-00 00:00:00'
]
];
I need to implement a condition whereby if the value of $_GET['b']
exists in the id
column of my array, I want to echo that subarray's name
value.我需要实现一个条件,如果$_GET['b']
的值存在于我的数组的id
列中,我想回显该子数组的name
值。
If $_GET['b']
does not exist in my array, I want to echo all of the name
values in the array.如果我的数组中不存在$_GET['b']
,我想回显数组中的所有name
值。
The is my failing code:这是我失败的代码:
foreach ($readJson as $key => $value){
if($_GET["b"] === $value["id"]){ // here is statement
echo $value["name"]; // I want to echo just this item not others
// maybe break; ?
} else {
echo $value["name"]; // echo all items
}
}
I guess I need something like break
but I know break
won't echo items after it.我想我需要像break
之类的东西,但我知道break
不会在它之后回显项目。
Or if I get item index maybe I could echo it by index or id?或者,如果我得到项目索引,也许我可以通过索引或 ID 回显它?
$readJson=[
['id'=>78,'name'=>'audi','sscat_id'=>59,'date'=>'0000-00-00 00:00:00'],
['id'=>106,'name'=>'apache','sscat_id'=>86,'date'=>'0000-00-00 00:00:00'],
['id'=>16,'name'=>'asia','sscat_id'=>62,'date'=>'0000-00-00 00:00:00']
];
$_GET['b']=16; // test case for echoing one
//$_GET['b']=99; // test case for echoing all
if(($index=array_search($_GET["b"],array_column($readJson,'id')))!==false){ // id exists in multi-dim array
echo $readJson[$index]['name'];
}else{ // id not found, show all
echo implode(', ',array_column($readJson,'name'));
}
Output:输出:
asia
When $_GET['b']=99
, then output is:当$_GET['b']=99
,输出为:
audi, apache, asia
Filter the values by matches, if none matched, use all values instead, and output them:按匹配过滤值,如果没有匹配,则使用所有值,并输出它们:
$values = array_filter($readJson, function ($i) { return $i['id'] == $_GET['b']; });
foreach ($values ?: $readJson as $value) {
echo $value['name'];
}
If $values
is empty ( == false
), $values ?: $readJson
falls back to $readJson
, otherwise uses $values
.如果$values
为空( == false
), $values ?: $readJson
回退到$readJson
,否则使用$values
。
Alternatives might include echo join(', ', array_column($values ?: $readJson, 'name'))
;替代方法可能包括echo join(', ', array_column($values ?: $readJson, 'name'))
; depending on what exactly you need to do with those values in the loop.取决于您究竟需要对循环中的这些值做什么。
if(array_search($_GET["b"],array_column($readJson, 'id')) !== False) {
foreach ($readJson as $key => $value){
if($_GET["b"]==$value["id"]){
echo $value["name"];
}
}
}else{
foreach ($readJson as $key => $value){
echo $value["name"];
}
}
We want to first set as an array of all the values and then check using array_search
after that print that value using the key of that searched value.我们想首先将所有值设置为一个数组,然后使用array_search
进行检查,然后使用该搜索值的键打印该值。 if the get value is empty means print all the array values.如果 get 值为空,则表示打印所有数组值。
Check with like this i added sample array and printed:像这样检查我添加了示例数组并打印:
$readJson = array(1,2,3,4,5);
$arr = array();
foreach ($readJson as $key => $value){
$arr[] = $value;
}
if($key = array_search($_GET["b"], $arr)){
echo $arr[$key];
}else{
print_r($arr);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.