Check if specific array key exists in multidimensional array - PHP
I played with your code to get it working :
function findKey($array, $keySearch)
{
foreach ($array as $key => $item) {
if ($key == $keySearch) {
echo 'yes, it exists';
return true;
} elseif (is_array($item) && findKey($item, $keySearch)) {
return true;
}
}
return false;
}
Check if key exist in a multidimensional array
Attempt to extract the year
column and if it results in a non-empty array then there is a year
somewhere in the array:
if(array_column($array, 'year')) {
//yes year exists :-)
} else {
//no doesn't exist :-(
}
If you're wanting to check each array in the array and do something for each one, then just loop and check for year
:
foreach($array as $values) {
if(isset($values['year'])) {
//do something with $values
} else {
//do something else
}
}
how to check if a specific array key is exist in multidimensional array
Super hacky solution:
function array_key_exists_recursive($array, $key) {
return strpos(json_encode($array), "\"" . $key . "\":") !== false;
}
Better solution:
$array = ['a' => ['b' => 'c']];
function array_key_exists_recursive($key, $array) {
if (array_key_exists($key, $array)) {
return true;
}
foreach($array as $k => $value) {
if (is_array($value) && array_key_exists_recursive($key, $value)) {
return true;
}
}
return false;
}
var_dump(array_key_exists_recursive('b', $array));
How to check if value exists in multidimensional array with keys?
You have to use in_array()
along with array_column()
<?php
$array = array(
array('name' => 'number1', 'number' => '0612345675'),
array('name' => 'number2', 'number' => '0634345675'),
array('name' => 'number3', 'number' => '0634378675')
);
$valueToFind = '0634345675';
if (in_array($valueToFind, array_column($array, 'number'))){
echo 'found';
}else{
echo 'not found';
}
Output:- https://3v4l.org/TUtSL
If you want to show that array too, then use array_search()
$key = array_search($valueToFind, array_column($array, 'number'));
if($key){
echo 'value found';
echo PHP_EOL;
echo "matched array is";
echo PHP_EOL;
print_r($array[$key]);
}
Output:-https://3v4l.org/Mc2cC
In case multiple match found:
$valueToFind = '0634378675';
$matchedArray = array();
foreach($array as $arr){
if($valueToFind == $arr['number']){
$matchedArray[] = $arr;
}
}
if( count($matchedArray) > 0){
echo "match found";
echo PHP_EOL;
print_r($matchedArray);
}
Output:- https://3v4l.org/p439T
Multidimensional array: check if key exists
You can use array_key_exists to check if the key exists in the array, and then get the value. So you have:
<?php
$users = array(
"bill@microsoft.com" => array(
"Name" => "Bill Gates",
"Password" => "12345",),
"jim@apple.com" => array(
"Name" => "Jim Franklyn",
"Password" => "98765"),
);
$key = "bill@microsoft.com";
if(array_key_exists($key, $users)) {
echo "Password is: " . $users[$key]["Password"];
} else {
echo "Not Found!";
}
php checking if value exist in a 2 level multidimensional array
Use below code, it will find key to n-level depth and search for given key
function multiKeyExists(array $arr, $key) {
// is in base array?
if (array_key_exists($key, $arr)) {
return $arr[$key]['cat_id']; // returned cat_id
}
// check arrays contained in this array
foreach ($arr as $element) {
if (is_array($element)) {
if (multiKeyExists($element, $key)) {
return $element[$key]['cat_id']; // returned cat_id
}
}
}
return false;
}
How to check whether array key is present inside multidimensional array in php
simple use array_column
to get the specific column from multidimensional array
. if the count of array is more than zero
key exists
otherwise key not exists
so show the error message
.
if(count(array_column($array['ratio'],'assigned_to'))>0){
echo "key exist in the multi-dimensional array";
}else{
echo "key not present ";
}
check if value exists in multidimensional array
You can use array_search()
to search for a specific value in your array. This function returns the key corresponding to the value if found, false
otherwise.
So what you will want to do is loop each subarray:
$category = $_GET['cat'];
$allProjects = array(
'project1' => array('corporate'),
'project2' => array('corporate', 'print'),
'project3' => array('web')
);
foreach ($allProjects as $projectName => $categories) {
$categoryIndex = array_search($category, $categories);
if ($categoryIndex !== false) {
echo 'active: ' . $categoryIndex;
// Do something with $categoryIndex and $projectName here
}
}
Update:
Looks like this is your answer:
$project = $_GET('project');
$category = $_GET('cat');
if (isset($allProjects[$project]) && in_array($category, $allProjects[$project])) {
echo 'yes';
}
Multidimensional PHP array - Key exists
This is where a recursive function comes in handy.
function multi_array_key_exists($key, array $array): bool
{
if (array_key_exists($key, $array)) {
return true;
} else {
foreach ($array as $nested) {
if (is_array($nested) && multi_array_key_exists($key, $nested))
return true;
}
}
return false;
}
Note that this can take some time (in long nested arrays), it might be better to flatten first, since you are only interested in whether the key exists or not.
Related Topics
Selecting Multiple Array Elements
Jquery Mobile: How to Correctly Submit Form Data
What's the Best Practice to Set HTML Attribute via PHP
Best Practices to Test Protected Methods with PHPunit
Sqlsrv_Num_Rows Not Returning Any Value
PHP Exec $Path Variable Missing Elements
PHP Send Mail to Multiple Email Addresses
Create a Comma-Separated String from a Single Column of an Array of Objects
PHP Remove Duplicate Values from Multidimensional Array
PHP Messing with HTML Charset Encoding
Using Password_Hash and Password_Verify