hi i have an array of about 20/30 items big.
i need to have it loop threw the array and echo out only the items with the text p1 in them.
the array looks like so
"lolly","lollyp1","top","topp1","bum","bump1","gee","geep1"
and so on
i have tried to use something like this
foreach ($arr as $value) {
$needle = htmlspecialchars($_GET["usr"]);
$ret = array_keys(array_filter($arr, function($var) use ($needle){
return strpos($var, $needle) !== false;
}));
but all this gives me is a blank page or 1s
how can i have it echo out the items with p1 in them ?
Try This:
$needle = htmlspecialchars($_GET["usr"]);
$rtnArray = array();
foreach ($arr as $value) {
$rtnArray = strpos($value,$needle);
};
return $rtnArray;
If your trying to write directly to the page the lose the $rtnarray and echo:
$needle = htmlspecialchars($_GET["usr"]);
foreach ($arr as $value) {
echo strpos($value,$needle);
};
To only show ones with 'p1' then filter:
$needle = htmlspecialchars($_GET["usr"]);
foreach ($arr as $value) {
$temp = strpos($value,$needle);
if($temp > 1){
echo $value;
}
};
Using a direct loop with string-comparison would be a simple way to go here:
$needle = $_GET['usr'];
$matches = array();
foreach ($arr as $key => $value) {
if (strpos($value, $needle) !== false) {
$matches[] = $key;
}
}
The use of array_filter() in your post should work, pending the version of PHP you're using. Try updating to use a separate / defined function:
function find_needle($var) {
global $needle;
return strpos($var, $needle) !== false;
}
$ret = array_keys(array_filter($arr, 'find_needle'));
Codepad Example of the second sample
Related
I have a multidimensional array like this:
$a['bla1']['blub1']="test123";
$a['bla1']['blub2']="test1234";
$a['bla1']['blub3']="test12345";
$a['bla2']['blub1']="test123456";
$a['bla2']['blub2']="test12344e45";
$a['bla2']['blub3']="test12345335";
How to search by value and get back bla1 or bla2? I don't need the subkey, only the key.
try this:
function searcharray($a, $value)
{
foreach($a as $key1 => $keyid)
{
foreach($keyid as $key => $keyid2)
{
if ( $keyid2 === $value )
return $key.','.$key1;
}
}
return false;
}
This function recursively searches an array to any depth and returns the main key under which $needle is found:
function get_main_key($arr, $needle) {
$out = FALSE;
foreach ($arr as $key => $value) {
if ($value == $needle) {
$out = $key;
} elseif (is_array($value)) {
$ret = get_main_key($value, $needle);
$out = ( ! empty($ret)) ? $key : $out;
}
}
return $out;
}
I have a array of val which has dynamic strings with underscores. Plus I have a variable $key which contains an integer. I need to match $key with each $val (values before underscore).
I did the following way:
<?php
$key = 2; //always a dynamic number
$val = array('3_33', '2_55'); //always a dynamic string with underscore
if(in_array($key, $val)) {
echo 'Yes';
}
else
{
echo 'No';
}
?>
Though this code works fine, I want to know if its a correct way or suggest some better alternative.
use this function for regex match from php.net
function in_array_match($regex, $array) {
if (!is_array($array))
trigger_error('Argument 2 must be array');
foreach ($array as $v) {
$match = preg_match($regex, $v);
if ($match === 1) {
return true;
}
}
return false;
}
and then change your code to use this function like this:
$key = 2; //always a dynamic number
$val = array('3_33', '2_55'); //always a dynamic string with underscore
if(in_array_match($key."_*", $val)) {
echo 'Yes';
}
else
{
echo 'No';
}
This should work :
foreach( $val as $v )
{
if( strpos( $v , $key .'_' ) === true )
{
echo 'yes';
}
else {
echo 'no';
}
}
you can use this
function arraySearch($find_me,$array){
$array2 =array();
foreach ($array as $value) {
$val = explode('_',$value);
$array2[] =$val[0];
}
$Key = array_search($find_me, $array2);
$Zero = in_array($find_me, $array2);
if($Key == NULL && !$Zero){
return false;
}
return $Key;
}
$key = 2; //always a dynamic number
$val = array('3_33', '2_55'); //always a dynamic string with underscore
$inarray = false;
foreach($val as $v){
$arr = explode("_", $val);
$inarray = $inarray || $arr[0] == $key
}
echo $inarray?"Yes":"No";
The given format is quite unpractically.
$array2 = array_reduce ($array, function (array $result, $item) {
list($key, $value) = explode('_', $item);
$result[$key] = $value;
return $result;
}, array());
Now you can the existence of your key just with isset($array2[$myKey]);. I assume you will find this format later in your execution useful too.
Is there an easier way to do so?
$array = array(1,57,5,84,21,8,4,2,8,3,4);
$remove = 21;
$i = 0;
foreach ($array as $value ){
if( $value == $remove)
unset($array[$i])
$i++;
}
//array: 1,57,5,84,8,4,2,8,3,4
The array_search answer is good. You could also arraydiff like this
$array = array(1,57,5,84,21,8,4,2,8,3,4);
$remove = array(21);
$result = array_diff($array, $remove);
If you want to delete the first occurrence of the item in the array, use array_search to find the index of the item in the array rather than rolling your own loop.
$array = array(1,57,5,84,21,8,4,2,8,3,4);
$remove = 21;
$index = array_search($remove, $array);
if (index !== false)
unset($array[$index]);
To remove all duplicates, rerun the search/delete so long as it finds a match:
while (false !== ($index = array_search($remove, $array))) {
unset($array[$index]);
}
or find all keys for matching values and remove them:
foreach (array_keys($array, $remove) as $key) {
unset($array[$key]);
}
This is a little cleaner:
foreach($array as $key => $value) {
if ($value == $remove) {
unset($array[$key]);
break;
}
}
UPDATE
Alternatively, you can place the non-matching values into a temp array, then reset the original.
$temp = array();
foreach($array as $key => $value) {
if ($value != $remove) {
$temp[$key] = $value;
}
}
$array = $temp;
I have an array like the following:
5-9-21,
5-10-22,
5-10-22,
5-11-23,
3-17-29,
3-19-31,
3-19-31,
1-25-31,
7-30-31
I wil get a value dynamically. Then I have to compare that value with the middle part of array.
9,
10,
10,
11,
17,
19,
19,
25,
30
If it's matching then I have to remove the whole part from array.
For example. If I am getting a value dynamically is 19, then I wil match with that array. And 3-19-31 is there two times. So it will remove all 3-19-31. After exploding with "-".
How can I do this?
foreach($array as $key=>$value){
$parts = explode('-', $value);
if($parts[1] == $search) {
unset($array[$key]);
}
}
Or if your search is an array
foreach($array as $key=>$value){
$parts = explode('-', $value);
if(in_array($parts[1], $search)) {
unset($array[$key]);
}
}
You could use array_filter to get a new array.
$new_arr = array_filter($old_arr, function($var) use ($input) {
$ret = explode('-', $var);
return !(isset($ret[1]) && $ret[1] === $input);
});
Or use a normal loop and then use unset to remove the values.
for ($arr as $key => $value) {
$ret = explode('-', $value);
if (isset($ret[1]) && $ret[1] === $input) {
unset($arr[$key]);
}
}
use this function, this will give you all the keys which are matched:
function custom_array_search($keyword,$array){
if(!is_array($array)){
return false;
}
$ret_keys = array();
foreach($array as $key=>$value){
if(strpos("-{$keyword}-",$value)!==false){
$ret_keys[] = $key;
}
}
return $ret_keys;
}
This function will give you all keys in an array.
Now you can delete those i.e. unset all keys from that array. :)
<?php
$arr = array('5-9-21', '5-10-22', '5-10-22', '5-11-23', '3-17-29', '3-19-31', '3-19-31', '1-25-31', '7-30-31');
$k = '10';
#print_r($arr);
foreach ($arr as $key => $value)
{
$t = explode('-',$value);
if($t[1] == $k)
{
unset($arr[$key]);
#echo "deleted<br>";
}
}
#print_r($arr);
?>
You can try this...
$arr; // your array
$value = 19;
foreach ($arr as $key=>$a)
{
if(strpos($a, "-".$value."-") !== false)
unset($arr[$key]);
}
There are few ways you can do this. If you are going to have only one digit always in the first eliment of your triplet, the following code should work;
$triplet_array = array(5-9-21, 5-10-22, 5-10-22, 5-11-23, 3-17-29, 3-19-31, 3-19-31, 1-25-31, 7-30-31);
$i = 0;
foreach($triplet_array as triplet){
$middle = substring($triplet,2,0);
if($middle == $my_dynamic_value) unset($triplet_array[$i]);
$i++
}
but, if the first part is not going to contain only one digit always;
foreach($triplet_array as triplet){
$this_triplet = explode('-',$triplet);
if($this_triplet[1] == $my_dynamic_value) unset($triplet_array[$i]);
$i++
}
hope this helps :-)
I'm trying to do a scrape with SimpleHTMLDom and seem to be running in to a problem.
My code is as follows :
$table = $html->find('table',0);
$theData = array();
foreach(($table->find('tr')) as $row) {
$rowData = array();
foreach($row->find('td') as $cell) {
$rowData[] = $cell->innertext;
}
$theData[] = $rowData;
}
function array_find($needle, array $haystack)
{
foreach ($haystack as $key => $value) {
if (false !== stripos($needle, $value)) {
return $key;
}
}
return false;
}
$searchString = "hospitalist";
$position = array_find($searchString, $theData);
echo ($position);
Which yields the following error:
Warning: stripos() [function.stripos]: needle is not a string or an integer in C:\xampp\htdocs\main.php on line 85
What am I doing wrong?
You have the order of the actual parameters reversed in your call to stripos. See http://us3.php.net/manual/en/function.stripos.php. Just reverse the order of the arguments and that error should be fixed.
Change:
if (false !== stripos($needle, $value)) {
to
if (false !== stripos($value, $needle)) {
From the docs, you should be passing in the needle second, not first. Try this:
function array_find($needle, array $haystack)
{
foreach ($haystack as $key => $value) {
if (false !== stripos($value, $needle)) {
return $key;
}
}
return false;
}
The message is referring to the function argument of stripos and not your variable named $needle.
int stripos ( string $haystack , string $needle [, int $offset = 0 ] )
It is actually complaining about the needle $value