简体   繁体   English

从多维数组中的特定数组获取所有值

[英]Getting all values from a specific array within a multidimentional array

I've been searching around quite a bit for an answer for this, but I'm afraid that I've been unable to figure out a solution to this problem. 我一直在寻找有关此问题的答案,但是恐怕我一直无法找到解决该问题的方法。 I've created a multidimensional array which includes zip code information. 我创建了一个包含邮政编码信息的多维数组。 However, I've been unable to pull the values out of it in the way that I need to. 但是,我一直无法以所需的方式从中提取价值。 Here's an example of the print_r(): 这是print_r()的示例:

Array ( 
[0] => Array ( 
    [0] => 59101 
    [1] => 0.0 )
[1] => Array ( 
    [0] => 59102 
    [1] => 5.0 )
[2] => Array ( 
    [0] => 59105 
    [1] => 6.8 )
[3] => Array ( 
    [0] => 59106 
    [1] => 9.2 )
[4] => Array ( 
    [0] => 59037 
    [1] => 12.7 )
[5] => Array ( 
    [0] => 59044 
    [1] => 13.9 )
[6] => Array ( 
    [0] => 59002 
    [1] => 16.6 )
[7] => Array ( 
    [0] => 59079 
    [1] => 19.3 )

)

I need to look through the array for a specific zip code, and then get distance (the second value in each array) associated with that zip code. 我需要在数组中查找特定的邮政编码,然后获取与该邮政编码相关联的距离(每个数组中的第二个值)。 I'd considered restructuring the array, but I'm unsure of how to accomplish it. 我曾考虑过重组数组,但不确定如何实现。 Here's my current code: 这是我当前的代码:

EDIT## sorry, I may not have been clear. EDIT ##对不起,我可能还不清楚。 The below code is what I'm using to build the array, not to extract information from the array. 下面的代码是我用来构建数组而不是从数组中提取信息的代码。 I have not idea how to get the information out of the array. 我不知道如何从数组中获取信息。

$rArray = array();
foreach ($points as $point){
    $zips = $point->Postcode;
    $dists =  number_format($point->D,1);
    array_push($rArray,array($zips,$dists));
}

Any thoughts on the best way to accomplish this? 对实现此目标的最佳方法有何想法? Thanks! 谢谢!

This? 这个?

EDIT: After your question update. 编辑:您的问题更新后。

function getDistanceByZip($zip) {
   $array = //your array here;
   foreach($array as $value) {
      if($zip == $value[0]) {
         return $value[1];
      }
   }
   return false;
}

Maybe this? 也许这个吗?

foreach ($points as $point){
   if ($point->Postcode === $codeIamLookingFor) {
       echo "Distance: " . number_format($point->D, 1);
   }
}
function arrayseek($array, $zip){
   foreach($array as $k => $v){
       if($v[0] == $zip){
           return $v[1];
       }
   }
   return false;
}

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

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