-3

Following is the array structure that i have used

    $arr =  Array ( 
                  [0] => Array 
                  (
                        [Research] => '#00c0ef' ,
                        [class_name] => box-info 
                   )
                  [1] => Array
                  ( 
                        [Review] => '#00a65a' ,
                        [class_name] => box-success 
                    ) 
                    [2] => Array 
                    ( 
                         [Case Study] => '#3c8dbc',
                          [class_name] => box-primary 
                     ) 
        );

I want to get the key of '#3c8dbc', which means i get the output as Case Study.

Is the following code is a better solution for this ?

 $search_value = '#3c8dbc';

  $selected_array = array_filter(array_map(function ($ar) use 
 ($search_value) {return array_search($search_value,$ar);}, $userdb));

  $selected_item = reset($selected_array);
  The $selected_item will print the key of value '#3c8dbc' .

   **Output :  Case Study**
Preethi
  • 47
  • 11
  • 2
    Did you look through the [PHP Manual and try and find one?](http://www.php.net/manual/en/ref.array.php) – RiggsFolly Sep 11 '18 at 12:05
  • So, if you search `40489`, the output should be `uid`, if you search `urlof100`, the output should be `pic_square` ? In that case, what to do with doubles values? There might be, in example, twice the same name – Cid Sep 11 '18 at 12:09
  • I think this may help you. https://stackoverflow.com/questions/8102221/php-multidimensional-array-searching-find-key-by-specific-value – Yagnesh Makwana Sep 11 '18 at 12:12
  • HI RIGGSFOLLY - The below Link you mentioned may be same question but output i need is different . My requirement is different thats why i add as a question . PHP Multidimensional Array Searching (Find key by specific value) – Preethi Sep 11 '18 at 12:54

1 Answers1

0
$signal = false;
for($i = 0, $l = count($userdb); $i < $l; $i++) {
    $subarray = $userdb[$i];
    foreach ($subarray as $index => $value) {
        if ($value === $myValue) {
            $key = $index;
            $signal = true;
            break;
        }
    }
    if ($signal) { break; }        
}

if (isset($key)) {
    var_dump($key);            // key name of the subarray
    var_dump($userdb[$i]);     // full subarray. $i containing the index of it in $userdb
}

This should do the trick.

I loop over each element with a for loop so I can keep the interesting index at the end of the loop. When the element is found, I leave the loop. Hope it helps

edit (specific to the array shown in the question)

// $myValue contains the input you have
if (gettype($myvalue) === "integer") {
    $key = "uid";
} else {
    if (preg_match("/^urlof/", $myValue)) {
        $key = "url";
    } else {
        $key = "name";
    }
}
Zyigh
  • 425
  • 4
  • 16
  • Note that I propose this answer as native functions exist to do so, so I assume you don't need them in your algorithm – Zyigh Sep 11 '18 at 12:16
  • I got the ouput using the above code. Is there any to get the result without using loop – Preethi Sep 11 '18 at 12:17
  • As you have to check each element of the array, I guess you have to. What could be done also would be to have typed values. So if it's numeric you know that your key is uid, if not, try a regex matching to see if it starts with "urlof", and you have it without looking at your array – Zyigh Sep 11 '18 at 12:24
  • HI RiggsFolly - The below Link you mentioned may be same question but output i need is different . My requirement is different thats why i add as a question . PHP Multidimensional Array Searching (Find key by specific value) – Preethi Sep 11 '18 at 12:44