Check If a Specific Value Exists At a Specific Key in Any Subarray of a Multidimensional 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()

if (in_array($valueToFind, array_column($array, 'number'))){

echo 'value found';

echo PHP_EOL;

$key = array_search($valueToFind, array_column($array, 'number'));

echo "matched array is";

echo PHP_EOL;

print_r($array[$key]);
}

Output:-https://3v4l.org/ZlYGp

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

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';
}

in_array() and multidimensional array

in_array() does not work on multidimensional arrays. You could write a recursive function to do that for you:

function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}

return false;
}

Usage:

$b = array(array("Mac", "NT"), array("Irix", "Linux"));
echo in_array_r("Irix", $b) ? 'found' : 'not found';

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;
}

Determine if specific value exists in any level of a multidimensional array

You could use the function array_walk_recursive() to get all the values with any level of nested array.

https://secure.php.net/manual/en/function.array-walk-recursive.php

<?php
$status = array(
"house" => "OK",
"car" => array(
"car1" => "OK",
"car2" => "OK"
),
"boat" => "OK"
);

$required = array();
array_walk_recursive($status, function ($value, $key) use (&$required){
$required[] = $value;
}, $required);
print '<pre>';
print_r($required);
print '</pre>';
?>

Output:

Array
(
[0] => OK
[1] => OK
[2] => OK
[3] => OK
)

Check existence of a key in an array with multiple keys in Tcl

As you can see that test_arr cotains two keys named n1_temp and x_cell

No, it has one key named n1_temp,x_cell.

Use a dict instead, which supports actual multi dimensional structures:

dict set test_dict n1_temp x_cell 3
if {[dict exists $test_dict n1_temp]} {
# ...
}

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 ";
}


Related Topics



Leave a reply



Submit