[PHP] Re: Array Search Not Working?

2010-03-10 Thread Shawn McKenzie
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

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



[PHP] Re: Array Search Not Working?

2010-03-10 Thread clancy_1
On Wed, 10 Mar 2010 09:52:30 -0500, aj...@alumni.iu.edu (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'] = 
Sys['index'] = 2
Sys['date'] = CCY2N

Clancy

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