0

Here's my array: $properties Here's a print_r of my array:

Array (
 [ID] => 6585
 [ancestors] => Array ( 
   [0] => 6163
 )

Here's a variable I want to see if is in my array: $pageid

What's a function that will return true if $pageid == 6163 (the [0] element in ancestors)?

Michael Berkowski
  • 253,311
  • 39
  • 421
  • 371
Ben Dunkle
  • 119
  • 1
  • 5
  • Your terms are a little ambigous - _is in my array_ is a little different from `$pageid == 6163, the [0] element`. Can `ancestors` be multiple elements and you want to know if `$pageid` is one among many, or must `$pageid` be the `[0]` element only? – Michael Berkowski Jul 17 '14 at 18:59
  • 1
    possible duplicate of [in\_array() and multidimensional array](http://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array) – Mark Miller Jul 17 '14 at 19:01
  • I want to see if it is in the subarray, I don't care if it's in the top level array; does that make sense? – Ben Dunkle Jul 17 '14 at 19:03
  • `$bool = in_array($pageid, $properties['ancestors']);` – Mark Miller Jul 17 '14 at 19:03
  • in_array doesn't work with multidimensional arrays :( and re: that link, it might work but that function searches every element in the array for the string. I know exactly which element I want to search for the string. – Ben Dunkle Jul 17 '14 at 19:05
  • Michael, ancestors will only ever be one element; I know that makes it pointless that it should be an array, but I can't change that. – Ben Dunkle Jul 17 '14 at 19:08

2 Answers2

0
array_search(6163,$properties['ancestors'],TRUE);
markoCarbo
  • 56
  • 3
  • in my script, echo $pageid shows 6163 and [ancestors] => Array ( [0] => 6163 is showing. They match, but I still can't get a return of true – Ben Dunkle Jul 17 '14 at 19:25
  • class="quickmovein" > isn't putting the class attribute in my html – Ben Dunkle Jul 17 '14 at 19:27
  • why you dont use this: if($properties['ancestors'][0] === $pageid) – markoCarbo Jul 17 '14 at 19:35
  • markoCarbo yes that works! My array is $property not $properties and that why some of the other solutions may not have worked; sorry if I wasted anyones time. Thanks! – Ben Dunkle Jul 17 '14 at 19:44
0

Since you said you know exactly which element you want to search, try

If(array_key_exist('ancestors',$properties) && is_array($properties['ancestors']) && array_key_exist(0,$properties['ancestors'])){
    return ($properties['ancestors'][0] == 6163);
}else{
    return false;
}

I might have misunderstood your question and replies, coz its seems simple to me.

anyway if you explain a bit more I can make my answer better.

printfmyname
  • 963
  • 13
  • 28