RE: Array Search Not Working?
| From: | Alice Wei | Date: | Wed, 10 Mar 2010 16:21:26 +0000 |
| Subject: | RE: Array Search Not Working? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-302669@lists.php.net to get a copy of this message | ||
> Date: Wed, 10 Mar 2010 10:09:54 -0600
> From: nospam@mckenzies.net
> To: ajwei@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
Here is what I wanted to do:
$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);
print_r($new_array);
Although array_combine does put the two arrays together, I cannot really "sort" with the
array, because there is text and numeric in there. Plus, array_search seems to be only able to
search through one dimension. I would really like to know how I can first sort through the
$new_array according to the "numeric" and NOT the "text".
I then would like to use the "index" after the sort to use as the "key" to find
out what "color" I am supposed to use according to the position.
Is this possible? And, are there some directions on where I can start this?
Thanks for your help.
Alice
_________________________________________________________________
Your E-mail and More On-the-Go. Get Windows Live Hotmail Free.
http://clk.atdmt.com/GBL/go/201469229/direct/01/