Let's say I have a php array like this:
$compare = array("Testing", "Bar");
$my_array = array(
"Foo=7654", "Bar=4.4829", "Testing=123"
);
So it's safe to say below:
$compare[0] = "Testing"
So I wanna check if $my_array
contains $compare[0]
(in above case, yes it does) And if it contains returns that value from $my_array
which is Testing=123
How's that possible?
You may use foreach
loop:
$result = array();
foreach ($compare as $comp) {
foreach ($my_array as $my) {
if (strpos($my, $comp) !== false) {
$result[] = $comp;
}
}
}
in array $result
is result of search
or by other way:
$result = array();
foreach ($compare as $comp) {
foreach ($my_array as $my) {
if ($comp = explode('=', $my)[0]) {
$result[] = $comp;
}
}
}
To use a single loop...
$compare = array("Testing", "Bar");
$my_array = array(
"Foo=7654", "Bar=4.4829", "Testing=123"
);
$output = [];
foreach ( $my_array as $element ) {
if ( in_array(explode('=', $element)[0], $compare)){
$output[] = $element;
}
}
print_r($output);
This just checks that the first part of the value (before the =) is in the $compare
array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.