Problem With array_search()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Lance705
Forum Newbie
Posts: 17
Joined: Mon Jul 01, 2002 2:15 am

Post by Lance705 »

So like "C:\Epsilon\Epsilon.exe *"?
Lance705
Forum Newbie
Posts: 17
Joined: Mon Jul 01, 2002 2:15 am

Post by Lance705 »

Warning: Invalid argument supplied for foreach() in c:\apache\htdocs\uomindblast\servercontrols\kill.php on line 2
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

well * only subsitutes for one character.. so.. like i said, im not the person to ask on this... i never understood all of php's wildcards
Lance705
Forum Newbie
Posts: 17
Joined: Mon Jul 01, 2002 2:15 am

Post by Lance705 »

YES! GO MAC U RULE THX!
Last edited by Lance705 on Mon Jul 01, 2002 3:01 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

When you did foreach does your code look like this:

Code: Select all

$proccessname = "Epsilon"; 

exec("C:/kill/PS", $poslist) or die('failed executing PS'); 

foreach ($poslist as $id => $value) {
	if (preg_match('/epsilon/i', $value)) {
		echo $id;
	}
}
since using your posted output from $poslist and the foreach loop above I got the expected answer, 30.

Mac
Lance705
Forum Newbie
Posts: 17
Joined: Mon Jul 01, 2002 2:15 am

Post by Lance705 »

I got it. thx.
Lance705
Forum Newbie
Posts: 17
Joined: Mon Jul 01, 2002 2:15 am

Post by Lance705 »

whoa, wait. What is 30. Is it the array table with epsilon in it? It is showing the table that contains ps...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Nope it's the key that you're looking for so to get the value associated with that key you could have:

Code: Select all

$proccessname = "Epsilon"; 

exec("C:/kill/PS", $poslist) or die('failed executing PS'); 

foreach ($poslist as $id => $value) { 
   if (preg_match('/epsilon/i', $value)) { 
      echo $value; 
   } 
}
Mac
Lance705
Forum Newbie
Posts: 17
Joined: Mon Jul 01, 2002 2:15 am

Post by Lance705 »

now it is returning the wrong one

Code: Select all

<?
exec("C:/kill/PS", $poslist) 
or die('failed executing PS'); 

foreach ($poslist as $id => $value) &#123; 
   if (preg_match('/epsilon/i', $value)) &#123; 
      echo $id; 
  &#125; 
&#125;
print("\n$poslist&#1111;$id]\n");
print("$poslist&#1111;$value]\n");
?>
Output:

30 C:\kill\ps.exe 0x94c
Lance705
Forum Newbie
Posts: 17
Joined: Mon Jul 01, 2002 2:15 am

Post by Lance705 »

It is still returning 30, although my proccess list has changed andd Epsilon no longer resides on 30.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It's not getting the wrong one it's just remembering the last value of $id which is the last key in the array because of the foreach loop...

This is what you were trying to do (but will return the correct value corresponding to the key found):

Code: Select all

<?php
exec("C:/kill/PS", $poslist) or die('failed executing PS'); 

foreach ($poslist as $id => $value) &#123; 
   if (preg_match('/epsilon/i', $value)) &#123; 
      $search_key = $id; 
  &#125; 
&#125; 
echo $search_key.' '.$postlist&#1111;$search_key];
?>
Mac
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It is still returning 30, although my proccess list has changed and Epsilon no longer resides on 30.
Have you checked this using print_r() on the output from PS again?

Mac
Lance705
Forum Newbie
Posts: 17
Joined: Mon Jul 01, 2002 2:15 am

Post by Lance705 »

Ok, I get it now. Thank you for you time =)
Post Reply