This question already has answers here:
PHP in_array isn't finding a value that is there
(3 answers)
Closed 9 years ago.
I am using a simple php script to look for an element in an array like
$restricted = array('root/base', 'root2' );
print_r($restricted);
if( array_search('root/base', $restricted) ){
echo "1";
} else {
echo "0";
}
But I am always getting the following output
Array ( [0] => root/base [1] => root2 ) 0
This means that the array_search is failing to find the element in the given array. Can anybody show some light on whats happening?
I tried to replace array_search() with in_array() also. But that too returned the same error.
From PHP DOC
array_search — Searches the array for a given value and returns the corresponding key if successful
The index is 0 that is why you think its fails
Use
array_search('root/base', $restricted) !== false
Related
This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 1 year ago.
I'm fairly new to PHP. I'm trying to filter out an array of items that contain a specific string. In order to then push those items to array, I need to pass the array to the function as a reference (or so my research has told me) however I can't seem to get it to work, any ideas? Thanks all.
$filteredS3Results = array();
function filterResults($var, &$filteredS3Results) {
if (strpos($var, '008-20160916') !== false) {
array_push($filteredS3Results, $var);
};
};
array_filter($s3Results,"filterResults");
The callback function just receives one argument, the array element being tested. It should return a boolean value that indicates whether the element should be included in the filtered result.
$filteredS3Results = array_filter($s3Results, "filterResults");
function filterResults($var) {
return strpos($var, '008-21060916') !== false;
}
This question already has answers here:
php is null when empty? [duplicate]
(10 answers)
Closed 5 years ago.
The following code gives TRUE,FALSE,FALSE,FALSE,
I dont understand the TRUE response on empty arrays.
Someone has an explanation?
$results=array();
// Case 1 : Empty array
$myArray=array();
array_push($results, ($myArray==null));
array_push($results, ($myArray===null));
// Case 2 : Non Empty array
$myArray=array(1);
array_push($results,($myArray==null));
array_push($results,($myArray===null));
//
foreach ($results as $result) {
if ($result) echo("TRUE,"); else echo ("FALSE,");
}
Response here : PHP treats NULL, false, 0, and the empty string as equal, see stackoverflow here php is null or empty?
... and empty arrays
Need to be very careful so
This question already has answers here:
PHP in_array() / array_search() odd behaviour
(2 answers)
Closed 6 years ago.
It is known that in_array() function can be used to check if an array contains a certain value. However, when an array contains the value 0, an empty or null values pass the test, too.
For example
<?php
$testing = null;
$another_testing = 0;
if ( in_array($testing, [0,1,5,6,7]) )
echo "Found";
else
echo "Not Found";
echo "<br>";
if ( in_array($another_testing, [0,1,5,6,7]) )
echo "Found";
else
echo "Not Found";
?>
In both cases "Found" is printed. But I would like the first case to print "Not Found", and the second case - "Found".
I know I can solve the problem by adding an extra if statement, or by writing my own function, but I want to know if there are any built-ins in PHP that can perform the checks.
This behavior is explained by the fact that null == 0. But null !== 0. In other words, you should check for the types as well.
You don't need another function. Simply pass true as the third parameter:
in_array($testing, [0,1,5,6,7], true)
In this case in_array() will also check the types.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to check if an array element exists?
apologize if i am wrong,I am new to PHP, Is there any way to find out whether key exists in Json string after decoding using json_decode function in PHP.
$json = {"user_id":"51","password":"abc123fo"};
Brief:
$json = {"password":"abc123fo"};
$mydata = json_decode($json,true);
user_id = $mydata['user_id'];
If json string doesn't consist of user_id,it throws an exception like Undefined index user_id,so is there any way to check whether key exists in Json string,Please help me,I am using PHP 5.3 and Codeigniter 2.1 MVC Thanks in advance
IF you want to also check if the value is not null you can use isset
if( isset( $mydata['user_id'] ) ){
// do something
}
i.e. the difference between array_key_exists and isset is that with
$json = {"user_id": null}
array_key_exists will return true whereas isset will return false
You can try array_key_exists.
It returns a boolean value, so you could search for it something like:
if(array_key_exists('user_id', $mydata)) {
//key exists, do stuff
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Checking if array is multidimensional or not?
How do I check whether an array is multidimensional or not in PHP?
Use count twice, one with single parameter, and one with recursive mode
if (count($myarray) == count($myarray, COUNT_RECURSIVE))
{
echo 'MyArray is not multidimensional';
}
else
{
echo 'MyArray is multidimensional';
}
count(array,mode)
array ---Required. Specifies the array or object to count.
mode ---Optional. Specifies the mode of the function. Possible values:
0 - Default. Does not detect multidimensional arrays (arrays within arrays)
1 - Detects multidimensional arrays
Note: This parameter was added in PHP 4.2
Multi-dimensional arrays in PHP are simply arrays containing arrays. So a simple function for this could be written as
function is_multidim_array($arr) {
if (!is_array($arr))
return false;
foreach ($arr as $elm) {
if (!is_array($elm))
return false;
}
return true;
}
This will run through every element of $arr and check whether it's an array. Should it encounter an element that is not an array, it will return false. Otherwise, return true.