简体   繁体   English

PHP数组:通过索引获取值,其中

[英]PHP arrays: get value by index where

I have this PHP array: 我有这个PHP数组:

$statuses = array(
  'delivery-ongoing' => array("status" => "at-10", "traffic" => "FCL", "type" => "export"),
  'delivered' => array("status" => "at-13", "traffic" => "FCL", "type" => "export"),
  'delivery-ongoing' => array("status" => "00--00", "traffic" => "FCL", "type" => "import"),
  'return-to-ongoing' => array("status" => "to-04", "traffic" => "FCL", "type" => "import"),
  'delivered' => array("status" => "at-13", "traffic" => "FCL", "type" => "import")
);

I have to select status by key "delivery-ongoing" where type = "import" 我必须通过类型为=“ import”的键“进行中”选择状态

I can play with the array structure since is a constant within my class. 我可以使用数组结构,因为这是我班上的一个常数。

I tried 我试过了

$statuses['delivery-ongoing']['status']

How do I get the right status for type = "import" 如何获得type =“ import”的正确状态

Is there a sort of loop I have to do or there is another way to do this? 我是否需要执行某种循环,或者有另一种方法可以执行此操作?

You can use array_filter 您可以使用array_filter

$filtered = array_filter($statuses, function($value, $key) {
    return ($key == 'delivery-ongoing' && $value['type'] == 'import');
}, ARRAY_FILTER_USE_BOTH);

print_r($filtered);

Also, as it has been suggested in the comments, you can re-name your keys, maybe by appending an ID after the status. 另外,正如注释中所建议的那样,您可以重命名密钥,也许是在状态后附加一个ID。

Eq: 式:

'delivery-ongoing-101' => array("status" => "at-10", "traffic" => "FCL", "type" => "export"),

There are several problems with this array: 这个数组有几个问题:

1- There's an error in a parenthesis that is closed too early at this line: 1-括号中的错误在此行关闭的太早了:

'return-to-ongoing' => array("status" => "to-04", "traffic" => "FCL"), "type" => "import",

2- If you define the same key two times on the same array, you won't be able to access the first element that was defined with this key. 2-如果在同一数组上两次定义相同的键,则将无法访问用该键定义的第一个元素。 If you use a debugger, you will see that only 3 elements are available in your array, cause there are more than one that share the same key and only the last one is saved. 如果使用调试器,您将看到数组中只有3个元素可用,因为有多个共享同一密钥,并且仅保存了最后一个。

But to get the value that you are looking for, you can use this loop: 但是要获得所需的值,可以使用以下循环:

foreach ($statuses as $key => $value) {

    if($key == 'delivery-ongoing' && $value['type'] == 'import'){

        $result = $value['status'];

        break;
    }
}

The status for the type import is available at $result after the loop ends. 循环结束后,类型导入的状态在$ result可用。

Your $statuses must have the following structure: 您的$ statuses必须具有以下结构:

$statuses = array(
  'delivery-ongoing' => array(
    array("status" => "at-10", "traffic" => "FCL", "type" => "export"), 
    array("status" => "00--00", "traffic" => "FCL", "type" => "import")
  ),
  'delivered' => array(
    array("status" => "at-13", "traffic" => "FCL", "type" => "export"), 
    array("status" => "at-13", "traffic" => "FCL", "type" => "import")
    ),
  'return-to-ongoing' => array(array("status" => "to-04", "traffic" => "FCL", "type" => "import")),
);

Now, you can do what you want to do by doing: 现在,您可以通过执行以下操作来完成您想做的事情:

 function filter_by_value ($array, $index, $value){ 
    if(is_array($array) && count($array)>0)  
    { 
        foreach(array_keys($array) as $key){ 
            $temp[$key] = $array[$key][$index]; 

            if ($temp[$key] == $value){ 
                $newarray[$key] = $array[$key]; 
            } 
        } 
      } 
  return $newarray; 
} 

$imported = filter_by_value($statuses['delivery-ongoing'], 'type', 'import');

print_r($imported);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM