Re: Array Search Not Working?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 10 Mar 2010 09:52:30 -0500, ajwei@xxxxxxxxxxxxx (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

I have a very handy utility for problems like this:

// Expand string array, & list all terms
function larec($array, $name) // List array recursive
	{
	if (is_array($array))
		{
		$j = count ($array);
		$temp = array_keys($array);
		$i = 0; while ($i < $j)
			{
			if(isset($array[$temp[$i]]))
				{
				$new_line = $name."['".$temp[$i]."']";
				larec ($array[$temp[$i]], $new_line);
				}
			$i++;
			}
		}
	else
		{
		echo '<p>'.$name.' = '.$array.'</p>';
		}
	}

If you have some array $foo then larec($foo,'Foo'); will list all the elements of $foo
recursively, without any obvious limits.  This makes it very easy to see what you have
actually got, as opposed to what you thought you would get.  The following is an abridged
example of the result of listing an array $wkg_sys of mine, using: 

larec  ($wkg_sys,'Sys');

Sys['class'] = W

Sys['style']['0']['wkg_style'] = basic_tab
Sys['style']['0']['pad'] = 
Sys['style']['0']['stripe'] = 0
Sys['style']['1']['wkg_style'] = nrml_style
Sys['style']['1']['pad'] = 1
Sys['style']['1']['stripe'] = 0

Sys['valid'] = 1
Sys['entries'] = 15
Sys['f_title'] = Developmental Web Page
Sys['version'] = IF1.4
Sys['ident'] = 0800
Sys['directory_id'] = 0000
Sys['index'] = 2
Sys['date'] = CCY2N

Clancy

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux