Re: Array Search Not Working?

From: Date: Wed, 10 Mar 2010 16:09:54 +0000
Subject: Re: Array Search Not Working?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-302667@lists.php.net to get a copy of this message
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 http://www.spidean.com

« previous php.general (#302667) next »