Array Search Problem

Array Search Problem

am 10.03.2010 15:12:10 von Alice Wei

--_5ab2fc44-2ab0-44fb-857a-4d96c5b3344b_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable


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 p=
roblem here appears to be that no matter what value I have in the $distance=
_to_destination variable=2C 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=2C but I have to match up =
the colors based on the values of the correct states.=20

I figured that I may have problems with=20

$key=3D array_search($location2=2C$from)=3B //Find out the position of =
the index in the array

$colors_style =3D "=3Bfill:" . $state_colors[$key]=3B //Use the index =
from array_search to apply to the color index

Obviously=2C 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 variabl=
e. Could someone please give me some hints on how I could do the array_sear=
ch here based on the "value" of the values in the $distance_to_distance and=
apply the color to the states?


header("Content-type: image/svg+xml")=3B //Outputting an SVG

$from =3D $_GET['from']=3B
$state_colors=3D $_GET['state_colors']=3B
$distance_to_destination=3D $_GET['distance_to_destination']=3B

$from =3D explode("-"=2C $from)=3B
$state_colors=3D explode("-"=2C $state_colors)=3B
$change =3D explode("-"=2C$change)=3B

#Load the Map
$ourFileName=3D "USA_Counties_with_FIPS_and_names.svg"=3B
$fh =3D fopen($ourFileName=2C "r") or die("Can't open file")=3B
$contents =3D fread($fh=2Cfilesize($ourFileName))=3B
$lines2=3D file($ourFileName)=3B

foreach ($lines2 as $line_num =3D> $line2) {

$style_line_num =3D $line_num+3=3B
$line2 =3D trim($line2)=3B

if(preg_match("/^style/"=2C$line2)) {

$rest =3D substr($line2=2C0=2C-1)=3B =20

for ($j=3D$line_num=3B$j<=3D$style_line_num=3B$j++){
if(preg_match("/inkscape:label/"=2C$lines2[$j])) {=20
$location =3D explode("=3D"=2C$lines2[$j])=3B
$location2 =3D substr($location[1]=2C1=2C-6);
=20
if(in_array($location2=2C $from)) {
=20
$key=3D array_search($location2=2C$from)=3B //Find out the pos=
ition of the index in the array
$colors_style =3D "=3Bfill:" . $state_colors[$key]=3B //Use t=
he index from array_search to apply to the color index
$rest2 =3D substr($line2=2C0=2C-1). $colors_style . "\""=3B =
=20
echo $rest2 . "\n"=3B
}
else echo $line2 . "\n"=3B =20

} //end preg_match inkscape =20
} //end for loop
} //If preg_match style

else echo $line2 . "\n"=3B //else if preg_match style =20
} //end for each =20

fclose($fh)=3B
?>

Thanks for your help.

Alice
=20
____________________________________________________________ _____
Hotmail: Trusted email with Microsoft=92s powerful SPAM protection.
http://clk.atdmt.com/GBL/go/201469226/direct/01/=

--_5ab2fc44-2ab0-44fb-857a-4d96c5b3344b_--

Re: Array Search Problem

am 10.03.2010 18:50:23 von Rene Veerman

did you read the help for those functions on php.net?

On Wed, Mar 10, 2010 at 4:12 PM, Alice Wei wrote:
>
> Hi,
>
> =A0I have the code as shown in the following that I am trying to create t=
he image of based on the file loaded into the file and additional edits. Th=
e problem here appears to be that no matter what value I have in the $dista=
nce_to_destination variable, it does not affect any changes on the map. Wha=
t 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 t=
he colors based on the values of the correct states.
>
> =A0I figured that I may have problems with
>
> =A0 =A0$key=3D array_search($location2,$from); //Find out the position of=
the index in the array
>
> =A0 =A0$colors_style =3D ";fill:" . $state_colors[$key]; =A0//Use the ind=
ex 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 variabl=
e. Could someone please give me some hints on how I could do the array_sear=
ch here based on the "value" of the values in the $distance_to_distance and=
apply the color to the states?
>
> >
> header("Content-type: image/svg+xml"); //Outputting an SVG
>
> $from =3D $_GET['from'];
> $state_colors=3D $_GET['state_colors'];
> $distance_to_destination=3D $_GET['distance_to_destination'];
>
> $from =3D explode("-", $from);
> $state_colors=3D explode("-", $state_colors);
> $change =3D explode("-",$change);
>
> #Load the Map
> $ourFileName=3D "USA_Counties_with_FIPS_and_names.svg";
> $fh =3D fopen($ourFileName, "r") or die("Can't open file");
> $contents =3D fread($fh,filesize($ourFileName));
> $lines2=3D file($ourFileName);
>
> foreach ($lines2 as $line_num =3D> $line2) {
>
> $style_line_num =3D $line_num+3;
> $line2 =3D trim($line2);
>
> =A0 =A0 =A0if(preg_match("/^style/",$line2)) {
>
> =A0 =A0 =A0 $rest =3D substr($line2,0,-1);
>
> =A0 =A0 =A0 for ($j=3D$line_num;$j<=3D$style_line_num;$j++){
> =A0 =A0 =A0 =A0 if(preg_match("/inkscape:label/",$lines2[$j])) {
> =A0 =A0 =A0 =A0 =A0 =A0$location =3D explode("=3D",$lines2[$j]);
> =A0 =A0 =A0 =A0 =A0 =A0$location2 =3D substr($location[1],1,-6);
>
> =A0 =A0 =A0 =A0 =A0 =A0if(in_array($location2, $from)) {
>
> =A0 =A0 =A0 =A0 =A0 =A0 $key=3D array_search($location2,$from); //Find ou=
t the position of the index in the array
> =A0 =A0 =A0 =A0 =A0 =A0 $colors_style =3D ";fill:" . $state_colors[$key];=
=A0//Use the index from array_search to apply to the color index
> =A0 =A0 =A0 =A0 =A0 =A0 $rest2 =3D substr($line2,0,-1). $colors_style . "=
\"";
> =A0 =A0 =A0 =A0 =A0 =A0 echo $rest2 . "\n";
> =A0 =A0 =A0 =A0 =A0 =A0}
> =A0 =A0 =A0 =A0 =A0 =A0else echo $line2 . "\n";
>
> =A0 =A0 =A0 =A0 } //end preg_match inkscape
> =A0 =A0 } //end for loop
> =A0 } =A0//If preg_match style
>
> =A0 =A0 else echo $line2 . "\n"; //else if preg_match style
> =A0} //end for each
>
> fclose($fh);
> ?>
>
> Thanks for your help.
>
> Alice
>
> ____________________________________________________________ _____
> Hotmail: Trusted email with Microsoft=92s powerful SPAM protection.
> http://clk.atdmt.com/GBL/go/201469226/direct/01/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Array Search Problem

am 10.03.2010 18:57:33 von Alice Wei

--_f4c42d33-69f9-40c7-95ad-4eb07a702e46_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable


>=20
> did you read the help for those functions on php.net?

Yes=2C I found a "recursive" way to find out the "index" like I wanted=2C b=
y doing something like=20

$from =3D explode("-"=2C $from)=3B
$state_colors=3D explode("-"=2C $state_colors)=3B
$change =3D explode("-"=2C$change)=3B

$count =3D count($new_array)=3B
$i=3D0=3B
foreach ($new_array as $key =3D> $value){
echo $i . " " . $key . " is " . $value . " miles away
"=3B
$i++=3B
}

You can see it is not very elegant=2C and plus=2C I created the $new_array =
so I could do the ordering according to the values of the change array. I c=
an tell that since this is not a single array=2C which is probably why arra=
y_search does not work.=20
Since I don't need the "value" of my "new_array" here=2C 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=2C is there a trick in array_s=
earch that I could use to find the index without having to strip off anythi=
ng?

Thanks for your help.

Alice

>=20
> On Wed=2C Mar 10=2C 2010 at 4:12 PM=2C Alice Wei wr=
ote:
> >
> > Hi=2C
> >
> > I have the code as shown in the following that I am trying to create t=
he image of based on the file loaded into the file and additional edits. Th=
e problem here appears to be that no matter what value I have in the $dista=
nce_to_destination variable=2C it does not affect any changes on the map. W=
hat I am trying to do here is to create a map based on the pre-passed throu=
gh colors of individual states from another program=2C 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=3D array_search($location2=2C$from)=3B //Find out the position =
of the index in the array
> >
> > $colors_style =3D "=3Bfill:" . $state_colors[$key]=3B //Use the ind=
ex from array_search to apply to the color index
> >
> > Obviously=2C it is not applying the colors to the states that I would l=
ike other than doing it one by one as the order of what is in the $from var=
iable. 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?
> >
> > > >
> > header("Content-type: image/svg+xml")=3B //Outputting an SVG
> >
> > $from =3D $_GET['from']=3B
> > $state_colors=3D $_GET['state_colors']=3B
> > $distance_to_destination=3D $_GET['distance_to_destination']=3B
> >
> > $from =3D explode("-"=2C $from)=3B
> > $state_colors=3D explode("-"=2C $state_colors)=3B
> > $change =3D explode("-"=2C$change)=3B
> >
> > #Load the Map
> > $ourFileName=3D "USA_Counties_with_FIPS_and_names.svg"=3B
> > $fh =3D fopen($ourFileName=2C "r") or die("Can't open file")=3B
> > $contents =3D fread($fh=2Cfilesize($ourFileName))=3B
> > $lines2=3D file($ourFileName)=3B
> >
> > foreach ($lines2 as $line_num =3D> $line2) {
> >
> > $style_line_num =3D $line_num+3=3B
> > $line2 =3D trim($line2)=3B
> >
> > if(preg_match("/^style/"=2C$line2)) {
> >
> > $rest =3D substr($line2=2C0=2C-1)=3B
> >
> > for ($j=3D$line_num=3B$j<=3D$style_line_num=3B$j++){
> > if(preg_match("/inkscape:label/"=2C$lines2[$j])) {
> > $location =3D explode("=3D"=2C$lines2[$j])=3B
> > $location2 =3D substr($location[1]=2C1=2C-6)=3B
> >
> > if(in_array($location2=2C $from)) {
> >
> > $key=3D array_search($location2=2C$from)=3B //Find out the =
position of the index in the array
> > $colors_style =3D "=3Bfill:" . $state_colors[$key]=3B //Us=
e the index from array_search to apply to the color index
> > $rest2 =3D substr($line2=2C0=2C-1). $colors_style . "\""=3B
> > echo $rest2 . "\n"=3B
> > }
> > else echo $line2 . "\n"=3B
> >
> > } //end preg_match inkscape
> > } //end for loop
> > } //If preg_match style
> >
> > else echo $line2 . "\n"=3B //else if preg_match style
> > } //end for each
> >
> > fclose($fh)=3B
> > ?>
> >
> > Thanks for your help.
> >
> > Alice
> >
> > ____________________________________________________________ _____
> > Hotmail: Trusted email with Microsoft=92s powerful SPAM protection.
> > http://clk.atdmt.com/GBL/go/201469226/direct/01/
=20
____________________________________________________________ _____
Hotmail: Powerful Free email with security by Microsoft.
http://clk.atdmt.com/GBL/go/201469230/direct/01/=

--_f4c42d33-69f9-40c7-95ad-4eb07a702e46_--

Re: Array Search Problem

am 11.03.2010 07:12:15 von Rene Veerman

(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 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 =3D explode("-", $from);
> $state_colors=3D explode("-", $state_colors);
> $change =3D explode("-",$change);
>
> $count =3D count($new_array);
> $i=3D0;
> foreach ($new_array as $key =3D> $value){
> =A0echo $i . " " . $key . " is " . $value . " miles away
";
> =A0$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 o=
ff
> anything?
>
> Thanks for your help.
>
> Alice
>
>>
>> On Wed, Mar 10, 2010 at 4:12 PM, Alice Wei wrote:
>> >
>> > Hi,
>> >
>> > =A0I have the code as shown in the following that I am trying to creat=
e
>> > the image of based on the file loaded into the file and additional edi=
ts.
>> > 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 t=
he
>> > map. What I am trying to do here is to create a map based on the pre-p=
assed
>> > through colors of individual states from another program, but I have t=
o
>> > match up the colors based on the values of the correct states.
>> >
>> > =A0I figured that I may have problems with
>> >
>> > =A0 =A0$key=3D array_search($location2,$from); //Find out the position=
of the
>> > index in the array
>> >
>> > =A0 =A0$colors_style =3D ";fill:" . $state_colors[$key]; =A0//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 li=
ke
>> > 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 th=
e
>> > array_search here based on the "value" of the values in the
>> > $distance_to_distance and apply the color to the states?
>> >
>> > >> >
>> > header("Content-type: image/svg+xml"); //Outputting an SVG
>> >
>> > $from =3D $_GET['from'];
>> > $state_colors=3D $_GET['state_colors'];
>> > $distance_to_destination=3D $_GET['distance_to_destination'];
>> >
>> > $from =3D explode("-", $from);
>> > $state_colors=3D explode("-", $state_colors);
>> > $change =3D explode("-",$change);
>> >
>> > #Load the Map
>> > $ourFileName=3D "USA_Counties_with_FIPS_and_names.svg";
>> > $fh =3D fopen($ourFileName, "r") or die("Can't open file");
>> > $contents =3D fread($fh,filesize($ourFileName));
>> > $lines2=3D file($ourFileName);
>> >
>> > foreach ($lines2 as $line_num =3D> $line2) {
>> >
>> > $style_line_num =3D $line_num+3;
>> > $line2 =3D trim($line2);
>> >
>> > =A0 =A0 =A0if(preg_match("/^style/",$line2)) {
>> >
>> > =A0 =A0 =A0 $rest =3D substr($line2,0,-1);
>> >
>> > =A0 =A0 =A0 for ($j=3D$line_num;$j<=3D$style_line_num;$j++){
>> > =A0 =A0 =A0 =A0 if(preg_match("/inkscape:label/",$lines2[$j])) {
>> > =A0 =A0 =A0 =A0 =A0 =A0$location =3D explode("=3D",$lines2[$j]);
>> > =A0 =A0 =A0 =A0 =A0 =A0$location2 =3D substr($location[1],1,-6);
>> >
>> > =A0 =A0 =A0 =A0 =A0 =A0if(in_array($location2, $from)) {
>> >
>> > =A0 =A0 =A0 =A0 =A0 =A0 $key=3D array_search($location2,$from); //Find=
out the
>> > position of the index in the array
>> > =A0 =A0 =A0 =A0 =A0 =A0 $colors_style =3D ";fill:" . $state_colors[$ke=
y]; =A0//Use the
>> > index from array_search to apply to the color index
>> > =A0 =A0 =A0 =A0 =A0 =A0 $rest2 =3D substr($line2,0,-1). $colors_style =
.. "\"";
>> > =A0 =A0 =A0 =A0 =A0 =A0 echo $rest2 . "\n";
>> > =A0 =A0 =A0 =A0 =A0 =A0}
>> > =A0 =A0 =A0 =A0 =A0 =A0else echo $line2 . "\n";
>> >
>> > =A0 =A0 =A0 =A0 } //end preg_match inkscape
>> > =A0 =A0 } //end for loop
>> > =A0 } =A0//If preg_match style
>> >
>> > =A0 =A0 else echo $line2 . "\n"; //else if preg_match style
>> > =A0} //end for each
>> >
>> > fclose($fh);
>> > ?>
>> >
>> > Thanks for your help.
>> >
>> > Alice
>> >
>> > ____________________________________________________________ _____
>> > Hotmail: Trusted email with Microsoft=92s powerful SPAM protection.
>> > http://clk.atdmt.com/GBL/go/201469226/direct/01/
>
> ________________________________
> Hotmail: Powerful Free email with security by Microsoft. Get it now.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Array Search Problem

am 11.03.2010 13:13:12 von Alice Wei

--_f11d9819-ee9c-43f9-afe6-b320ec893a2c_
Content-Type: text/plain; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable


Hi,

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

Thanks for pointing me towards the right direction.=20

Alice


> From: rene7705@gmail.com
> Date: Thu=2C 11 Mar 2010 07:12:15 +0100
> Subject: Re: [PHP] Array Search Problem
> To: ajwei@alumni.iu.edu
> CC: php-general@lists.php.net
>=20
> (almost) all the tricks are in the comments of the help page for a
> function=2C on php.net
>=20
> but all functions accept only a given (and usually documented) set of
> parameter(type)s=2C so you'll probably have to prepare the var=2C or even
> call the function in a loop=2C outputting to yet another descriptively
> named array that'll be used as "wanted list" later in the code.
>=20
> On Wed=2C Mar 10=2C 2010 at 6:57 PM=2C Alice Wei wr=
ote:
> >>
> >> did you read the help for those functions on php.net?
> >
> > Yes=2C I found a "recursive" way to find out the "index" like I wanted=
=2C by
> > doing something like
> >
> > $from =3D explode("-"=2C $from)=3B
> > $state_colors=3D explode("-"=2C $state_colors)=3B
> > $change =3D explode("-"=2C$change)=3B
> >
> > $count =3D count($new_array)=3B
> > $i=3D0=3B
> > foreach ($new_array as $key =3D> $value){
> > echo $i . " " . $key . " is " . $value . " miles away
"=3B
> > $i++=3B
> > }
> >
> > You can see it is not very elegant=2C and plus=2C I created the $new_ar=
ray so I
> > could do the ordering according to the values of the change array. I ca=
n
> > tell that since this is not a single array=2C which is probably why
> > array_search does not work.
> > Since I don't need the "value" of my "new_array" here=2C I am still fin=
ding
> > out how to "strip off" the values here without having to flatten my arr=
ay.
> > Is what I am trying to do here possible? Or=2C 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=2C Mar 10=2C 2010 at 4:12 PM=2C Alice Wei =
wrote:
> >> >
> >> > Hi=2C
> >> >
> >> > I have the code as shown in the following that I am trying to creat=
e
> >> > the image of based on the file loaded into the file and additional e=
dits.
> >> > The problem here appears to be that no matter what value I have in t=
he
> >> > $distance_to_destination variable=2C 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=2C but I ha=
ve to
> >> > match up the colors based on the values of the correct states.
> >> >
> >> > I figured that I may have problems with
> >> >
> >> > $key=3D array_search($location2=2C$from)=3B //Find out the positi=
on of the
> >> > index in the array
> >> >
> >> > $colors_style =3D "=3Bfill:" . $state_colors[$key]=3B //Use the =
index from
> >> > array_search to apply to the color index
> >> >
> >> > Obviously=2C it is not applying the colors to the states that I woul=
d 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?
> >> >
> >> > > >> >
> >> > header("Content-type: image/svg+xml")=3B //Outputting an SVG
> >> >
> >> > $from =3D $_GET['from']=3B
> >> > $state_colors=3D $_GET['state_colors']=3B
> >> > $distance_to_destination=3D $_GET['distance_to_destination']=3B
> >> >
> >> > $from =3D explode("-"=2C $from)=3B
> >> > $state_colors=3D explode("-"=2C $state_colors)=3B
> >> > $change =3D explode("-"=2C$change)=3B
> >> >
> >> > #Load the Map
> >> > $ourFileName=3D "USA_Counties_with_FIPS_and_names.svg"=3B
> >> > $fh =3D fopen($ourFileName=2C "r") or die("Can't open file")=3B
> >> > $contents =3D fread($fh=2Cfilesize($ourFileName))=3B
> >> > $lines2=3D file($ourFileName)=3B
> >> >
> >> > foreach ($lines2 as $line_num =3D> $line2) {
> >> >
> >> > $style_line_num =3D $line_num+3=3B
> >> > $line2 =3D trim($line2)=3B
> >> >
> >> > if(preg_match("/^style/"=2C$line2)) {
> >> >
> >> > $rest =3D substr($line2=2C0=2C-1)=3B
> >> >
> >> > for ($j=3D$line_num=3B$j<=3D$style_line_num=3B$j++){
> >> > if(preg_match("/inkscape:label/"=2C$lines2[$j])) {
> >> > $location =3D explode("=3D"=2C$lines2[$j])=3B
> >> > $location2 =3D substr($location[1]=2C1=2C-6)=3B
> >> >
> >> > if(in_array($location2=2C $from)) {
> >> >
> >> > $key=3D array_search($location2=2C$from)=3B //Find out t=
he
> >> > position of the index in the array
> >> > $colors_style =3D "=3Bfill:" . $state_colors[$key]=3B /=
/Use the
> >> > index from array_search to apply to the color index
> >> > $rest2 =3D substr($line2=2C0=2C-1). $colors_style . "\""=
=3B
> >> > echo $rest2 . "\n"=3B
> >> > }
> >> > else echo $line2 . "\n"=3B
> >> >
> >> > } //end preg_match inkscape
> >> > } //end for loop
> >> > } //If preg_match style
> >> >
> >> > else echo $line2 . "\n"=3B //else if preg_match style
> >> > } //end for each
> >> >
> >> > fclose($fh)=3B
> >> > ?>
> >> >
> >> > Thanks for your help.
> >> >
> >> > Alice
> >> >
> >> > ____________________________________________________________ _____
> >> > Hotmail: Trusted email with Microsoft=92s powerful SPAM protection.
> >> > http://clk.atdmt.com/GBL/go/201469226/direct/01/
> >
> > ________________________________
> > Hotmail: Powerful Free email with security by Microsoft. Get it now.
=20
____________________________________________________________ _____
Hotmail: Free=2C trusted and rich email service.
http://clk.atdmt.com/GBL/go/201469228/direct/01/=

--_f11d9819-ee9c-43f9-afe6-b320ec893a2c_--