PHP: universal recursive array search [duplicate] - php

This question already has answers here:
Search for a key in an array, recursively
(7 answers)
Closed 8 years ago.
I try to find a value in an array, no matter how deep that array is or what "structure" it may have. But my approach doesn't find all values. I think I get the recursion wrong, but I don't know.
$haystack = array(
'A',
'B' => array('BA'),
'C' => array('CA' => array('CAA')),
'D' => array('DA' => array('DAA' => array('DAAA')))
);
function array_find($needle, array $haystack) {
foreach ($haystack as $value) {
if (is_array($value)) {
if (in_array($needle, $value)) {
return true;
} else {
return array_find($needle, $value);
}
} else {
if ($value == $needle) {
return true;
}
}
}
return false;
}
$find = array('A', 'BA', 'CAA', 'DAAA');
foreach($find as $needle) {
if (array_find($needle, $haystack)) {
echo $needle, " found".PHP_EOL;
} else {
echo $needle, " not found".PHP_EOL;
}
}

Simply change your code to:
function array_find($needle, array $haystack) {
foreach ($haystack as $value) {
if (is_array($value)) {
if (in_array($needle, $value)) {
return true;
} else {
if (array_find($needle, $value)) {
return true;
}
}
} else {
if ($value == $needle) {
return true;
}
}
}
return false;
}
The problem is in your return statement.

I think this can be written more simply:
function array_find($needle, array $haystack) {
foreach ($haystack as $value) {
if (is_array($value)) {
if (array_find($needle, $value)) {
return true;
}
} else {
if ($value == $needle) {
return true;
}
}
}
return false;
}
I do not know whether or not there is a benefit to using in_array rather than just making the recursive call as soon as you determine it is an array.

Related

How to check given value present in nested array in PHP?

I am very new to the PHP, i want to check the value is present inside the associative array,please help me to acheive this thing.
public $array=[
'id','name',
'value.details'=>['type'=>'myFunction']
];
foreach($array as $key=>$condition){
if(in_array('myFunction',$array)){
//My logic
}
}
if you know the keys:
if($array['value.details']['type'] === 'myFunction') { }
if you walk through your array:
foreach($array as $key=>$val) {
if(is_array($val) && in_array('myFunction', $val)) {
//
}
}
Your array is mix of string and array, you might also wanna check the array inside your array.
$array=[
'id','name',
'value.details'=>['type'=>'myFunction']
];
$query = 'myFunction';
$isPresent = false;
foreach ($array as $data) {
if (gettype($data) === "array") {
foreach ($data as $val) {
if ($val === $query) {
$isPresent = true;
continue;
}
}
} else {
if ($query === $data) {
$isPresent = true;
continue;
}
}
}

Laravel getting error 'Trying to access array offset on value of type int' While call aritsan command Artisan::call('cache:clear');

I am trying to clear a cache using code. It throws me an error Trying to access array offset on value of type int
Route::get('/clear-cache', function() {
Artisan::call('cache:clear');
return "Cache is cleared";
});
Error in the line elseif ('-' === $key[0])
protected function parse()
{
foreach ($this->parameters as $key => $value) {
if ('--' === $key) {
return;
}
if (0 === strpos($key, '--')) {
$this->addLongOption(substr($key, 2), $value);
} elseif ('-' === $key[0]) {
$this->addShortOption(substr($key, 1), $value);
} else {
$this->addArgument($key, $value);
}
}
}
Key variable might not be array now. You can explicitly typecast this to array
protected function parse()
{
foreach ($this->parameters as $key => $value) {
$key = (array)$key;
if ('--' === $key) {
return;
}
if (0 === strpos($key, '--')) {
$this->addLongOption(substr($key, 2), $value);
} elseif ('-' === $key[0]) {
$this->addShortOption(substr($key, 1), $value);
} else {
$this->addArgument($key, $value);
}
}
}

Check in Multidimensional array [duplicate]

This question already has answers here:
in_array() and multidimensional array
(24 answers)
Closed 8 years ago.
Here is the function to check in multidimensional array.
public static function inarray($search,$array,$key_=NULL)
{
if(is_array($array))
{
if(!in_array( $search,$array))
{
foreach ($array as $key => $value) {
Common::inarray($search, $value,$key);
}
}
else {
return $key_;
}
}
else {
echo FALSE;
}
}
This function not returning any value but it is working.
Try this function
function search_in_array($srchvalue, $array)
{
if (is_array($array) && count($array) > 0)
{
$foundkey = array_search($srchvalue, $array);
if ($foundkey === FALSE)
{
foreach ($array as $key => $value)
{
if (is_array($value) && count($value) > 0)
{
$foundkey = search_in_array($srchvalue, $value);
if ($foundkey != FALSE)
return $foundkey;
}
}
}
else
return $foundkey;
}
}

PHP: check if an element belongs to an array

I know PHP 4 and PHP 5 supports a built in function in_array for determining is an element is in an array or not.
But, I am using a prior version of PHP for some reason and wanted to know what is the alternative for it.
Use a custom function. For future compatibility, you could use function_exists to check if the current version of PHP that you're using does indeed have in_array.
function inArray($needle, $haystack)
{
if (function_exists('in_array')) {
return in_array($needle, $haystack);
} else {
foreach ($haystack as $e) {
if ($e === $needle) {
return true;
}
}
return false;
}
}
If you are using something older than PHP 4, foreach will also be unavailable, so you will need to stick with list and each. Also to make in forward compatible, use third parameter for strict comparison:
if (!function_exists('in_array')) {
function in_array($needle, $haystack, $strict = false) {
while (list($key, $item) = each($haystack)) {
if ($strict && $needle === $item) {
return true;
} else if (!$strict && $needle == $item) {
return true;
}
}
return false;
}
}
Without checking what functions were available in what versions, and comparing against the version you are using (we're not php.net encyclopedia's), your best bet is to go back to basics, and just loop through and check.
function in_array($val, $array) {
foreach($array as $a) {
if($a === $val)
return true;
}
return false;
}
You can create your own function to mimic the functionality with
function my_in_array($ar, $val)
{
foreach($ar as $k => $v)
{
if($v == $val) return true;
}
return false;
}
or if you looking for a key
function my_in_array($ar, $val)
{
foreach($ar as $k => $v)
{
if($k == $val) return true;
}
return false;
}
Create your own in array function something like below:
function my_in_array($value, $arr) {
foreach ($arr as $a) {
if ($a == $value) {
return true;
}
}
return false;
}

How to search by value and get key in multidimensional arrays?

I have a multidimensional array like this:
$a['bla1']['blub1']="test123";
$a['bla1']['blub2']="test1234";
$a['bla1']['blub3']="test12345";
$a['bla2']['blub1']="test123456";
$a['bla2']['blub2']="test12344e45";
$a['bla2']['blub3']="test12345335";
How to search by value and get back bla1 or bla2? I don't need the subkey, only the key.
try this:
function searcharray($a, $value)
{
foreach($a as $key1 => $keyid)
{
foreach($keyid as $key => $keyid2)
{
if ( $keyid2 === $value )
return $key.','.$key1;
}
}
return false;
}
This function recursively searches an array to any depth and returns the main key under which $needle is found:
function get_main_key($arr, $needle) {
$out = FALSE;
foreach ($arr as $key => $value) {
if ($value == $needle) {
$out = $key;
} elseif (is_array($value)) {
$ret = get_main_key($value, $needle);
$out = ( ! empty($ret)) ? $key : $out;
}
}
return $out;
}

Categories