I have an array of arrays as such below and I want to check if the [avs_id] contains a substring "a_b_c". How to do this in php? Array
(
[id] => 10003
[avs_id] => a_b_c_3248
)
Array
(
[id] => 10003
[avs_id] => d_e_f_3248
)
asked Mar 14, 2016 at 2:30 coffeeakcoffeeak 2,9065 gold badges41 silver badges83 bronze badges 2 You can use array_filter() : $src = 'a_b_c';
$result = array_filter
(
$array,
function( $row ) use( $src )
{
return (strpos( $row['avs_id'], $src ) !== False);
}
);
eval.in demo The
result maintain original keys, so you can directly retrieve item(s) matching substring. If you want only check if substring exists, or the number of items having substring, use this: $totalMatches = count( $result );
answered Mar 14, 2016 at 2:50 fusion3kfusion3k 11.4k4 gold badges22 silver badges43 bronze badges Loop through your array and test for the string in the specific element of your array with strpos as in the example code below. foreach($yourMainArray as $arrayItem){
if (strpos($arrayItem['avs_id'], 'a_b_c') !== false) {
echo 'true';
}
}
answered Mar 14, 2016 at 2:39 ThrowBackDewdThrowBackDewd 1,7072 gold badges13 silver badges16 bronze badges
A loop may be more ideal but if you know what array index the string is in that you are after: $arr = array('id'=>'10003', 'avs_id'=>'a_b_c_3248');
if (strpos($arr['avs_id'], 'a_b_c') !== false) {
echo 'string is in avs_id';
}
answered Mar 14, 2016 at 2:41
CountachCountach 5979 silver badges20 bronze
badges You can use : foreach($yourArray as $arrayItem){
if (strpos($arrayItem['avs_id'], 'a_b_c') !== false) {
//return true : code here
}
}
answered Mar 14, 2016 at 3:10
AlexAlex 3,2061 gold badge26 silver badges24
bronze badges (PHP 4, PHP 5, PHP 7, PHP 8) in_array — Checks if a value exists in an array Descriptionin_array(mixed $needle , array $haystack ,
bool $strict = false ): bool Parameters needle The searched value. Note: If needle is a string, the comparison is done in a case-sensitive manner. haystack The array. strict If the third parameter strict is set to
true then the in_array() function will also check the types of the needle in the haystack . Note: Prior to PHP 8.0.0, a string needle will match an array value of 0 in non-strict mode, and vice versa. That may lead to undesireable results. Similar edge cases exist for other types, as well. If not
absolutely certain of the types of values involved, always use the strict flag to avoid unexpected behavior.
Return Values Returns true if needle is found in the array, false otherwise. ExamplesExample #1 in_array()
example
<?php $os = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $os)) { echo "Got Irix"; } if (in_array("mac", $os)) { echo "Got mac"; } ?>
The second condition fails because in_array() is case-sensitive, so the program above will display: Example #2 in_array() with strict example
<?php $a = array('1.10', 12.4, 1.13);
if (
in_array('12.4', $a, true)) { echo "'12.4' found with strict check\n"; }if ( in_array(1.13, $a, true)) { echo "1.13 found with strict check\n"; } ?>
The above example will output: 1.13 found with strict check
Example #3 in_array() with an array as
needle
<?php $a = array(array('p', 'h'), array('p', 'r'), 'o');
if (
in_array(array('p', 'h'), $a)) { echo "'ph' was found\n"; }if ( in_array(array('f', 'i'), $a)) { echo "'fi' was found\n"; }if ( in_array('o', $a)) { echo "'o' was found\n"; } ?>
The above example will output: 'ph' was found
'o' was found
See Also- array_search() - Searches the array for a given value and returns the first corresponding key if successful
- isset() - Determine if a variable is declared and is different than null
- array_key_exists() - Checks if the given key or index exists in the array
beingmrkenny at gmail dot com ¶ 10 years ago
Loose checking returns some crazy, counter-intuitive results when used with certain arrays. It is completely correct behaviour, due to PHP's leniency on variable types, but in "real-life" is almost useless.
The solution is to use the strict checking option.
<?php// Example array$array = array( 'egg' => true, 'cheese' => false, 'hair' => 765, 'goblins' => null, 'ogres' => 'no ogres allowed in this array' );// Loose checking -- return values are in comments
// First three make sense, last four do not
in_array(null, $array); // true in_array(false, $array); // true in_array(765, $array); // true in_array(763, $array); // true in_array('egg', $array); // true in_array('hhh', $array); // true in_array(array(), $array); // true// Strict checking in_array(null, $array, true); // true in_array(false, $array, true); // true in_array(765, $array, true); // true in_array(763, $array, true); // false in_array('egg', $array, true); // false in_array('hhh', $array, true); // false in_array(array(), $array, true); // false?>
rhill at xenu-directory dot
net ¶ 13 years ago
I found out that in_array will *not* find an associative array within a haystack of associative arrays in strict mode if the keys were not generated in the *same order*:
<?php
$needle
= array( 'fruit'=>'banana', 'vegetable'=>'carrot' );$haystack = array( array('vegetable'=>'carrot', 'fruit'=>'banana'), array('fruit'=>'apple', 'vegetable'=>'celery') );echo in_array($needle, $haystack, true) ? 'true' : 'false'; // Output is 'false'echo in_array($needle, $haystack) ? 'true' : 'false'; // Output is 'true'?>
I had wrongly assumed the order of the items in an associative array were irrelevant, regardless of whether 'strict' is TRUE or FALSE: The order is irrelevant *only* if not in strict mode.
thomas dot sahlin at gmail dot com ¶ 12 years ago
If you're creating an array yourself and then using in_array to search it, consider setting the keys of the array and using isset instead since it's much faster.
<?php
$slow
= array('apple', 'banana', 'orange');if ( in_array('banana', $slow)) print('Found it!');$fast = array('apple' => 'apple', 'banana' => 'banana', 'orange' => 'orange');if (isset( $fast['banana'])) print('Found it!');?>
How do you find if an array contains a specific string in PHP?
Approach: In order to search an array for a specific value, we will be using the in_array() function where the parameter for the search is of string type & its value is set to true. Otherwise, this function returns a false value if the specified value is not found in an array.
How do you check if a array contains a specific word in PHP?
Answer: Use the PHP strpos() Function
You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string.
How do you check if an array contains a value?
The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.
How do you check whether a string contains any words from an array?
To check if a string contains a substring from an array:. Use the Array. some() method to iterate over the array.. Check if the string contains each substring.. If the condition is met, the string contains a substring from the array..
|