RE: [PHP] Array Search Problem

[email protected] (Alice Wei)
Newsgroups php.general
Message-ID <[email protected]>
Hi, 

  At the time when I am writing this, looks like I already got the functions I needed. It turned out that I had to use some array_combine, sorting the items by keys instead of values as well as using array_keys to get the values I needed. 

Thanks for pointing me towards the right direction. 

Alice


> From: [email protected]
> Date: Thu, 11 Mar 2010 07:12:15 +0100
> Subject: Re: [PHP] Array Search Problem
> To: [email protected]
> CC: [email protected]
> 
> (almost) all the tricks are in the comments of the help page for a
> function, on php.net
> 
> but all functions accept only a given (and usually documented) set of
> parameter(type)s, so you'll probably have to prepare the var, or even
> call the function in a loop, outputting to yet another descriptively
> named array that'll be used as "wanted list" later in the code.
> 
> On Wed, Mar 10, 2010 at 6:57 PM, Alice Wei <[email protected]> wrote:
> >>
> >> did you read the help for those functions on php.net?
> >
> > Yes, I found a "recursive" way to find out the "index" like I wanted, by
> > doing something like
> >
> > $from = explode("-", $from);
> > $state_colors= explode("-", $state_colors);
> > $change = explode("-",$change);
> >
> > $count = count($new_array);
> > $i=0;
> > foreach ($new_array as $key => $value){
> >  echo $i . " " . $key . " is " . $value . " miles away<br />";
> >  $i++;
> > }
> >
> > You can see it is not very elegant, and plus, I created the $new_array so I
> > could do the ordering according to the values of the change array. I can
> > tell that since this is not a single array, which is probably why
> > array_search does not work.
> > Since I don't need the "value" of my "new_array" here, I am still finding
> > out how to "strip off" the values here without having to flatten my array.
> > Is what I am trying to do here possible? Or, is there a trick in
> > array_search that I could use to find the index without having to strip off
> > anything?
> >
> > Thanks for your help.
> >
> > Alice
> >
> >>
> >> On Wed, Mar 10, 2010 at 4:12 PM, Alice Wei <[email protected]> wrote:
> >> >
> >> > Hi,
> >> >
> >> >  I have the code as shown in the following that I am trying to create
> >> > the image of based on the file loaded into the file and additional edits.
> >> > The problem here appears to be that no matter what value I have in the
> >> > $distance_to_destination variable, it does not affect any changes on the
> >> > map. What I am trying to do here is to create a map based on the pre-passed
> >> > through colors of individual states from another program, but I have to
> >> > match up the colors based on the values of the correct states.
> >> >
> >> >  I figured that I may have problems with
> >> >
> >> >    $key= array_search($location2,$from); //Find out the position of the
> >> > index in the array
> >> >
> >> >    $colors_style = ";fill:" . $state_colors[$key];  //Use the index from
> >> > array_search to apply to the color index
> >> >
> >> > Obviously, it is not applying the colors to the states that I would like
> >> > other than doing it one by one as the order of what is in the $from
> >> > variable. Could someone please give me some hints on how I could do the
> >> > array_search here based on the "value" of the values in the
> >> > $distance_to_distance and apply the color to the states?
> >> >
> >> > <?php
> >> >
> >> > header("Content-type: image/svg+xml"); //Outputting an SVG
> >> >
> >> > $from = $_GET['from'];
> >> > $state_colors= $_GET['state_colors'];
> >> > $distance_to_destination= $_GET['distance_to_destination'];
> >> >
> >> > $from = explode("-", $from);
> >> > $state_colors= explode("-", $state_colors);
> >> > $change = explode("-",$change);
> >> >
> >> > #Load the Map
> >> > $ourFileName= "USA_Counties_with_FIPS_and_names.svg";
> >> > $fh = fopen($ourFileName, "r") or die("Can't open file");
> >> > $contents = fread($fh,filesize($ourFileName));
> >> > $lines2= file($ourFileName);
> >> >
> >> > foreach ($lines2 as $line_num => $line2) {
> >> >
> >> > $style_line_num = $line_num+3;
> >> > $line2 = trim($line2);
> >> >
> >> >      if(preg_match("/^style/",$line2)) {
> >> >
> >> >       $rest = substr($line2,0,-1);
> >> >
> >> >       for ($j=$line_num;$j<=$style_line_num;$j++){
> >> >         if(preg_match("/inkscape:label/",$lines2[$j])) {
> >> >            $location = explode("=",$lines2[$j]);
> >> >            $location2 = substr($location[1],1,-6);
> >> >
> >> >            if(in_array($location2, $from)) {
> >> >
> >> >             $key= array_search($location2,$from); //Find out the
> >> > position of the index in the array
> >> >             $colors_style = ";fill:" . $state_colors[$key];  //Use the
> >> > index from array_search to apply to the color index
> >> >             $rest2 = substr($line2,0,-1). $colors_style . "\"";
> >> >             echo $rest2 . "\n";
> >> >            }
> >> >            else echo $line2 . "\n";
> >> >
> >> >         } //end preg_match inkscape
> >> >     } //end for loop
> >> >   }  //If preg_match style
> >> >
> >> >     else echo $line2 . "\n"; //else if preg_match style
> >> >  } //end for each
> >> >
> >> > fclose($fh);
> >> > ?>
> >> >
> >> > Thanks for your help.
> >> >
> >> > Alice
> >> >
> >> > _________________________________________________________________
> >> > Hotmail: Trusted email with Microsoft’s powerful SPAM protection.
> >> > http://clk.atdmt.com/GBL/go/201469226/direct/01/
> >
> > ________________________________
> > Hotmail: Powerful Free email with security by Microsoft. Get it now.
 		 	   		  
_________________________________________________________________
Hotmail: Free, trusted and rich email service.
http://clk.atdmt.com/GBL/go/201469228/direct/01/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.