0

I have a function which checks the children on a tree given the parent ID:

function categoryChild($id) {

    $mysqli = dbConnect();

    $query = "select folders_id, childof from folders where childof = $id";
    $result = $mysqli->query($query);

    $children = array();

    if(mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_array($result)) {
            $children[$row['folders_id']] = categoryChild($row['folders_id']);
        }
    }

    return $children;
}

If I use: print_r(categoryChild($folder_id)); I get e.g. the following:

Array ( [21] => Array ( [22] => Array ( ) ) [24] => Array ( [25] => Array ( ) ) ) 

I want to be able to check simply check against the numbers in the array(s)...I've tried:

if(in_array ("25", categoryChild($folder_id))){
    echo 'yeah';
};

But it simply does nothing.

Is there a way to do this?

Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
StudioTime
  • 22,603
  • 38
  • 120
  • 207
  • 2
    You're injecting SQL into your code making it **insecure**. Use prepared statements and bound variables instead. – h2ooooooo Oct 16 '13 at 19:03
  • @h2ooooooo thanks but I'll deal with that later, I will be using PDO probably when I get it all to work as needed – StudioTime Oct 16 '13 at 19:11

2 Answers2

1

What you want is to check the key no the value

in_array you are checking if the value exist

You have to use array_key_exists in order to check the key

if (array_key_exists(25, categoryChild($folder_id))) {
    echo 'yeah';
 }

To find a key in nested array you can use this:

 $keySearch=25;
 $array=categoryChild($folder_id);

foreach ($array as $array_data) {
    if (array_key_exists($keySearch, $array_data)) {
     echo 'yeah';
    }
}

NOTE:

25 is INT no a string

A useful link how to prevent SQL Injections in PHP

UPDATE

if your array is something like this

$array = array(
    21 => '',
    24 => array(
        22 => '',
        25 => ''
    )
);

$keySearch = 21;

if (count($array) == count($array, COUNT_RECURSIVE)) {
//  single array
  if (array_key_exists($keySearch, $array)) {
    $result = 'yeah1';
  }else{
    $result = 'no';
  }
} else {
//  nested array;
  foreach ($array as $value) {
    if (array_key_exists($keySearch, $array)) {
      $result = 'yeah2';
    }else{
      $result='no';
    }
  }
}

echo $result;//output yeah2
Community
  • 1
  • 1
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
  • 1
    as his output (print_r) the first example with array_key_exists should be working. – Kilise Oct 16 '13 at 19:21
  • Yes...another option -:) – Emilio Gort Oct 16 '13 at 19:21
  • Still not working properly - in this it only sees 22 & 25 as keys - Array ( [21] => Array ( ) [24] => Array ( [22] => Array ( ) [25] => Array ( ) ) ) – StudioTime Oct 16 '13 at 21:16
  • @DarrenSweeney post your array to test, the array what you posted is incorrectly [try this](http://phpoverflow.eu01.aws.af.cm/print_r/) – Emilio Gort Oct 16 '13 at 21:20
  • @EmilioGort it shows "$object = NULL;" but it is exactly what print_r(categoryChild($folder_id)) gives me as shown in OP. – StudioTime Oct 16 '13 at 21:26
  • @EmilioGort Thanks, but the nesting could be 3, 5 or even 10 deep... can it not recurse somehow until its at the end of the tree? – StudioTime Oct 16 '13 at 21:56
  • http://stackoverflow.com/questions/16808220/php-check-if-some-keys-or-values-are-in-a-multidimensional-array?rq=1#answer-16809626 – Emilio Gort Oct 16 '13 at 22:10
1

You can use array_key_exists() or a simple isset.

if(isset($ARR[$key]))
     echo "yep!";
Kilise
  • 1,051
  • 4
  • 15
  • 35