-1

How can I achieve this behavior?? I've tried to write a function to implement this logic, but for some reason it does not work. Here is the link that I tried to do http://sandbox.onlinephpfunctions.com/code/638fc7796a8918e2b7ef1469b746c29adba8d0cd

<?php

    $test = [
      'deepLink' => [
        1,2,3,4,5, 'testMe'
      ],    
      'base' => [ // child for (2 and target)
        'SuperMan',
        'deepLink'  // expected true, if we call array_search_for_target_key($test, 'target', 'testMe')
      ],    
      2 => [
        'base' // link on child (base)
      ],    
      'target' => [
        2   // link on child (2)
      ],
    ];

function array_search_for_target_key($array = array(), $targetKkey, $search) {
   $res = false;
    foreach($array as $k => $v) {
        if ($k === $targetKkey && in_array($search, $v)) {
              $res = true;
        } else {
           if (is_array($v)) {
               foreach ($v as $nested) {
                 $array =  $nested;
                 $res = array_search_for_target_key($array, $nested, $search);   
               }
           }
        }
    }

    return $res;
}

var_dump( array_search_for_target_key($test, 'target', 'SuperMan'));

So i soleved my problem, here the link

1 Answers1

0

You are getting error in this part

$array =  $nested;
$res = array_search_for_target_key($array, $nested, $search); 

The $array is not an array so it throws an error. Which is

Invalid argument supplied for foreach() 

So Change your code from this:

foreach ($v as $nested){
    $array =  $nested;
    $res = array_search_for_target_key($array, $nested, $search);   
} 

With this

foreach ($v as $nested) {
    $res = array_search_for_target_key($v, $nested, $search);   
}

After changing that we will encounter a new error

in_array() expects parameter 2 to be array, integer given

To fix this we need to check if the variable is an array.

Change your code from this:

 if ($k === $targetKkey && in_array($search, $v))

With this

 if ($k === $targetKkey && is_array($v) && in_array($search, $v))

Also in your:

if ($k === $targetKkey && is_array($v) && in_array($search, $v)) {
    $res =  true;
} 

Change $res = true to return true;. We don't want to continue if we already found the target. Change $k === $targetKkey to $k == $targetKkey. 2 === '2' will return false so change it to ==

Here is the Whole Code

<?php

    $test = [
      'deepLink' => [
        1,2,3,4,5, 'testMe'
      ],    
      'base' => [ // child for (2 and target)
        'SuperMan',
        'deepLink'  // expected true, if we call array_search_for_target_key($test, 'target', 'testMe')
      ],    
      2 => [
        'base' // link on child (base)
      ],    
      'target' => [
        2   // link on child (2)
      ],
    ];

function array_search_for_target_key($array, $targetKkey, $search) {
   $res = false;
    foreach($array as $k => $v) {
       if ($k == $targetKkey && is_array($v) && in_array($search, $v)) {
          return true;
       } else {
           if (is_array($v)) {
           foreach ($v as $nested) {
             $res = array_search_for_target_key($v, $nested, $search);   
           }
       }
    }
 }

    return $res;
}

var_dump( array_search_for_target_key($test, 'target', 'SuperMan'));

Test here

Bluetree
  • 1,324
  • 2
  • 8
  • 25
  • Thanks, but the result of the function returns false. It turns out that the logic of the work is incorrect. I need a value to be found via a link to key 2( That is, for the value to be taken through key 2 from base! – Andrey Grodnov Dec 20 '17 at 02:16
  • @AndreyGrodnov okay so it wont work only in 2 => [ 'base' // link on child (base) ]? – Bluetree Dec 20 '17 at 02:18
  • @AndreyGrodnov I updated my answer. please change `$k === $targetKkey` to `$k == $targetKkey` – Bluetree Dec 20 '17 at 02:20