简体   繁体   English

打印符合条件的行中的一个值,或者打印特定列中的所有值

[英]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 回显它?

Code: ( Demo )代码:(演示

$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.

相关问题 搜索具有特定列值的数组行并从符合条件的行返回另一个值 - Search for array row with specific column value and return another value from qualifying row 获取从限定值到第二次出现限定值之前的所有键 - Get all keys from a qualifying value until before the second occurrence of a qualifying value 从一个表中选择所有行,并为另一表中的每一行选择一个特定值 - Select all rows from one table and one specific value for each row from another table 检查选定行中列的所有值 - Checking all the Values of the column from selected row PHP,HTML和MySqli-如何在一个表行中显示特定列的所有数据 - PHP, HTML and MySqli - how to show all data from specific column in one table row 从特定的mysql表列中检索数据值并在php中打印 - Retrieve data values from specific mysql table column and print in php 如何创建MYSQL查询以获取特定结果(从一栏中计算所有唯一值) - How to create MYSQL query to get specific results (count all unique values from one column) 如果值不为空,则从行中打印值PHP SQL - Print values from row if value is not empty PHP SQL 如何从表中获取所有数据,如果在同一列中重复相同的值,则该行应计数一次? - How to get get all the data from table and if same value repeated in one column that row should count once? 打印数组中的特定值 - Print the specific values from the array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM