From: aj...@alumni.iu.edu
To: nos...@mckenzies.net
CC: php-general@lists.php.net
Subject: RE: Array Search Not Working?
Date: Wed, 10 Mar 2010 11:21:26 -0500








> Date: Wed, 10 Mar 2010 10:09:54 -0600
> From: nos...@mckenzies.net
> To: aj...@alumni.iu.edu
> CC: php-general@lists.php.net
> Subject: Re: Array Search Not Working?
> 
> Alice Wei wrote:
> > Hi,
> > 
> >   I have two arrays here that I have combined into a new array, as shown 
> > here:
> > 
> > $from = explode("-", $from);
> > $change = explode("-",$change);
> > $new_array = array_combine($from,$change);
> > 
> > I then tried reading it from a file and do string matches, trying to find 
> > out the "key" using the array_search of the individual array elements. I 
> > seem to have no such luck, even when I copied one of the elements after I 
> > do a print_r($new_array); 
> > 
> > Here is the code,
> > 
> > foreach ($lines2 as $line_num => $line2) { 
> > $style_line_num = $line_num+3;
> > 
> >       if(preg_match("/^style/",$line2)) {
> >    
> >             if(preg_match("/inkscape:label/",$lines2[$style_line_num])) {  
> >             $location = explode("=",$lines2[$style_line_num]);
> >             $location2 = substr($patient_location[1],1,-6);  
> >                  
> >              if(in_array($location2, $from)) {                  
> >              $key= array_search($location2,$new_array); //Find out the 
> > position of the index in the array        
> >              echo "Key " . $key . "<br>";  //This only gives me a blank 
> > space after the word Key
> >                  
> >             }     
> > 
> >          } //end preg_match inkscape           
> >    }  //If preg_match style
> > 
> > I looked at the example from 
> > http://php.net/manual/en/function.array-search.php, and looks like what I 
> > am trying to do here is possible, and yet, why am I not getting a proper 
> > key return?
> > 
> > Thanks for your help.
> > 
> > Alice
> >                                       
> > _________________________________________________________________
> > Hotmail: Powerful Free email with security by Microsoft.
> > http://clk.atdmt.com/GBL/go/201469230/direct/01/
> 
> It's not clear what you are doing, but here is the problem that you are
> asking about.  array_combine() builds an array using the values from
> $from as the keys of the new array and the values from change as the
> values of the new array.
> 
> So here, if $locations2 is a value in the $from array:
> if(in_array($location2, $from)) {
> 
> But here you are searching the values of $new_array which are copies of
> the values from $to.
> $key= array_search($location2,$new_array);
> 
> 
> -- 
> Thanks!
> -Shawn

Hi, 

Looks like I have figured out how to get it to sort numerically after the print 
out , but still I cannot get it to do the array_search to find out the index of 
the "element" in this "new_array". 

Here is the code, 

$from = "Adair, OK-Alfalfa, OK-Atoka, OK-Beaver, OK-Beckham, OK-Blaine, 
OK-Bryan, OK-Caddo, OK-Canadian, OK-Carter, OK-Cherokee, OK-Choctaw, 
OK-Cimarron, OK-Cleveland, OK-Coal, OK-Comanche, OK-Cotton, OK-Craig, OK-Creek, 
OK-Custer, OK-Delaware, OK-Dewey, OK-Ellis, OK-Garfield, OK-Garvin, OK-Grady, 
OK-Grant, OK-Greer, OK-Harmon, OK-Harper, OK-Haskell, OK-Hughes, OK-Jackson, 
OK-Jefferson, OK-Johnston, OK-Kay, OK-Kingfisher, OK-Kiowa, OK-Latimer, OK-Le 
Flore, OK-Lincoln, OK-Logan, OK-Love, OK-Major, OK-Marshall, OK-Mayes, 
OK-McClain, OK-McCurtain, OK-McIntosh, OK-Murray, OK-Muskogee, OK-Noble, 
OK-Nowata, OK-Okfuskee, OK-Oklahoma, OK-Okmulgee, OK-Osage, OK-Ottawa, 
OK-Pawnee, OK-Payne, OK-Pittsburg, OK-Pontotoc, OK-Pottawatomie, OK-Pushmataha, 
OK-Roger Mills, OK-Rogers, OK-Seminole, OK-Sequoyah, OK-Stephens, OK-Texas, 
OK-Tillman, OK-Tulsa, OK-Wagoner, OK-Washington, OK-Washita, OK-Woods, 
OK-Woodward, OK
";
$state_colors= 
"#FFFFCC-#FFFFCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FFCCCC-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#FF9999-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF-#0DCCFF";
$change= 
"207-158-189-220-134-74.9-174-75.1-38-125-173-215-390-39-141-92.1-121-175-80.1-95.4-191-124-169-101-77-52-117-155-188-176-169-114-158-122-141-107-43-122-181-223-53.7-41.5-131-112-144-147-48-249-141-101-155-81.4-158-92.3-16.3-105-153-186-108-69.2-146-103-53.4-225-140-136-80.4-179-93.3-277-136-110-136-143-103-183-141";;

$from = explode("-", $from);
$state_colors= explode("-", $state_colors);
$change = explode("-",$change);

$new_array = array_combine($from,$change);
asort($new_array);

foreach ($new_array as $key => $value) echo $key . " is " . $value . " miles 
away<br />"; //this does sort numerically. 

The question you asked me earlier in the email, location2 is supposed to be one 
of the elements in the $from array, which is why I thought I could just do a 

  $key= array_keys($new_array,$location2); //Find out the position of the index 
in the array                                     

and find its index. In my case now, could you please give me some tips on how I 
could find the "index" of the $new_array array? 

Thanks. 
                                          
Alice
Your E-mail and More On-the-Go. Get Windows Live Hotmail Free. Sign up now.     
                                  
_________________________________________________________________
Hotmail: Powerful Free email with security by Microsoft.
http://clk.atdmt.com/GBL/go/201469230/direct/01/

Reply via email to