Recursive array_search
You can change your recursive function like this, which should give you the solution:
function recursive_array_search($needle, $haystack, $currentKey = '') {
foreach($haystack as $key=>$value) {
if (is_array($value)) {
$nextKey = recursive_array_search($needle,$value, $currentKey . '[' . $key . ']');
if ($nextKey) {
return $nextKey;
}
}
else if($value==$needle) {
return is_numeric($key) ? $currentKey . '[' .$key . ']' : $currentKey . '["' .$key . '"]';
}
}
return false;
}
This will result in
[1][1][0]["CategoryID"]
Since CategoryID is also a key in your multidimensional array.
If you don't want this, you can adapt the function to
function recursive_array_search($needle, $haystack, $currentKey = '') {
foreach($haystack as $key=>$value) {
if (is_array($value)) {
$nextKey = recursive_array_search($needle,$value, $currentKey . '[' . $key . ']');
if ($nextKey) {
return $nextKey;
}
}
else if($value==$needle) {
return is_numeric($key) ? $currentKey . '[' .$key . ']' : $currentKey;
}
}
return false;
}
Recursive array_search?
Simple foreach loop would do,
$result = [];
foreach ($arr as $key => $value) {
$result[$value][] = $key; // grouping array as per value as key
}
print_r($result);
Demo
Output:-
Array
(
[dummy2] => Array
(
[0] => https://i.imgur.com/vyGHgZN.jpg
)
[dummy] => Array
(
[0] => https://i.imgur.com/UYK4Agz.png
[1] => https://i.imgur.com/xEXdKYn.jpg
)
)
PHP recursive array_search
You do not need a recursion here, simply add an additional loop
in the get_range()
function. The following example is based on your code and is a possible solution to your problem:
<?php
$array = [8,2,3,7,4,6,5,1,9];
$subsets = array (
array(6,3,7),
array(4,2,9),
array(3,5,6),
);
function get_range($array, $subsets)
{
$result = array();
foreach ($subsets as $subset) {
$min = sizeof($array);
$max = 0;
foreach($subset as $value) {
$occurrence = array_search($value, $array);
if( $occurrence < $min ) {
$min = $occurrence;
}
if( $occurrence > $max ) {
$max = $occurrence;
}
}
$result[] = [$min, $max];
}
return $result;
}
$range = get_range($array, $subsets);
echo print_r($range, true);
?>
Result:
Array (
[0] => Array ( [0] => 2 [1] => 5 )
[1] => Array ( [0] => 1 [1] => 8 )
[2] => Array ( [0] => 2 [1] => 6 )
)
array_search not working in multi dimensional array as expected
array_search
function is not recursive, so you have to iterate over the array and search in the subarrays:
$foundInParent = false;
foreach($array as $parentKey => $subArray) {
if (array_search(5.97, $subArray)) {
$foundInParent = $parentKey;
break;
}
}
echo $foundInParent;
Just wrap it in a function..
array_search recursive function php
Ok here's what you should change it to:
$ex = [12,18,15];
for($i=0; $i<20;$i++) {
print randWithout(10,20,$ex) . PHP_EOL;
}
function randWithout($from, $to, array $exceptions) {
do {
$number = mt_rand($from, $to);
} while(in_array($number,$exceptions));
return $number;
}
Just tested it and it works.
Search PHP array for value and get parent key
You can just use array_search
and array_column
for this:
$power = 'SuperStrength';
$key = array_search($power, array_column($our_array[0]["powers"], 'power'));
echo "$key\n";
$power = 'Invisibility';
$key = array_search($power, array_column($our_array[0]["powers"], 'power'));
echo "$key\n";
Output:
0
1
Note that array_search
returns false
(which can be equivalent to 0) if it doesn't find a value, so when checking the result you should use !== false
to test for success. For example:
$power = 'Flying';
$key = array_search($power, array_column($our_array[0]["powers"], 'power'));
if ($key !== false) echo "$key\n";
else echo "power $power not found!\n";
Output:
power Flying not found!
Demo on 3v4l.org
Find specific value in multidimensional array
You can use in_array with splat operator,
Below is for multidimensional arrays
$temp = array_merge(...$array_2);
var_dump(in_array(2, $temp));
$temp = array_merge(...$array_3);
var_dump(in_array(2, $temp));
for 1D arrays,
you can directly check in_array($value_to_search, $array_1);
I am exposing array to their value level so they flatten up.
Now I just checked with in_array whether it exists in an array or not.
Demo.
using array_search for multi dimensional array
function find_car_with_position($cars, $position) {
foreach($cars as $index => $car) {
if($car['Position'] == $position) return $index;
}
return FALSE;
}
array_search Recursive in javascript
This might give you a start. Not thoroughly tested or highly optimized, and assumes use of jQuery (shouldn't be a big problem to replace the jQuery utilty functions with other implementations).
function searchArrayRecursive(needle, haystack, strict) {
function constructPath(needle, haystack, path, strict) {
if (!$.isArray(haystack)) {
return false;
}
var index;
for (index = 0; index < haystack.length; index++) {
var value = haystack[index];
var currentPath = $.merge([], path);
currentPath.push(index);
if ((strict && value === needle) || (!strict && value == needle)) {
return currentPath;
}
if ($.isArray(value)) {
var foundPath = constructPath(needle, value, currentPath, strict);
if (foundPath) {
return foundPath;
}
}
}
return false;
}
return constructPath(needle, haystack, [], strict);
}
http://jsfiddle.net/b8TxJ/2/
Related Topics
Partially Hide Email Address in PHP
PHP - Cannot Use a Scalar as an Array Warning
Mysql_Connect VS MySQL_Pconnect
Zend_Form - Array Based Elements
Time Calculation in PHP (Add 10 Hours)
Laravel Querybuilder How to Use Like in Wherein Function
Function to Create Regex Matching a Number Range
Preg_Match with International Characters and Accents
In PHP, How to Call a Function Interpolated in String
Line Break Not Working When Writing to Text File in PHP
Return One Value from Database with MySQL PHP Pdo
Google API How to Connect to Receive Values from Spreadsheet
Pdo Catch and Output MySQL Errors
PHP Password_Hash and Password_Verify Issues No Match
How to Install Imagemagick to Use with PHP on Windows 7 (3)
PHP Switch Case Statement to Handle Ranges
[PHP Warning: Mail(): " Sendmail_From" Not Set in PHP.Ini or Custom "From:" Header Missing