Problem With array_search()
Moderator: General Moderators
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
When you did foreach does your code look like this:
since using your posted output from $poslist and the foreach loop above I got the expected answer, 30.
Mac
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;
}
}Mac
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Nope it's the key that you're looking for so to get the value associated with that key you could have:
Mac
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;
}
}now it is returning the wrong one
Output:
30 C:\kill\ps.exe 0x94c
Code: Select all
<?
exec("C:/kill/PS", $poslist)
or die('failed executing PS');
foreach ($poslist as $id => $value) {
if (preg_match('/epsilon/i', $value)) {
echo $id;
}
}
print("\n$poslistї$id]\n");
print("$poslistї$value]\n");
?>30 C:\kill\ps.exe 0x94c
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
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):
Mac
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) {
if (preg_match('/epsilon/i', $value)) {
$search_key = $id;
}
}
echo $search_key.' '.$postlistї$search_key];
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK