1

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():

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

2
  • You guys have me thinking about this differently; right now I've just creating an array and then trying to extract the information using a seperate php file. I'll try accomplishing this without using an array, and going to a function instead. Commented Mar 7, 2013 at 21:23
  • Got it! Used a variation of Prashank's method. Thank you everyone for taking the time to help me out! Commented Mar 7, 2013 at 22:05

3 Answers 3

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Prashank! This ended up working out great for me; I ended up not using it as a function, but as a loop within the Wordpress loop that I'm working in.
0

Maybe this?

foreach ($points as $point){
   if ($point->Postcode === $codeIamLookingFor) {
       echo "Distance: " . number_format($point->D, 1);
   }
}

Comments

0
function arrayseek($array, $zip){
   foreach($array as $k => $v){
       if($v[0] == $zip){
           return $v[1];
       }
   }
   return false;
}

2 Comments

If possible its often nice to give a little narrative to your answer to explain what's going on.
Yeah - sorry - I was out of time :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.