Well, you could of course just process the array(s), element by element
$units = array
(
'count' => 2,
0 => array
(
'count' => 2,
'cn' => array
(
'count' => 1,
'0' => 'Unit1001'
),
0 => 'cn',
'ref' => array
(
'count' => 1,
'0' => 'dc910b00'
),
1 => 'ref',
'dn' => 'cn=Unit1001,cn=Units'
),
1 => array
(
'count' => 2,
'cn' => array
(
'count' => 1,
'0' => 'Unit1002'
),
0 => 'cn',
'ref' => array
(
'count' => 1,
'0' => '4c6e0000'
),
1 => 'ref',
'dn' => 'cn=Unit1002,cn=Units'
)
);
# in data to search for
$find = 1001;
$len = strlen($find);
$match = false;
foreach ($units as $k => $u)
{
if (is_array($u))
{
if (isset($u['cn']) && is_array($u['cn']) && isset($u['cn'][0]) &&
$find == substr($u['cn'][0], -1 * $len))
{
$match = $k;
break;
}
}
}
if ($match !== false)
{
echo 'element ' . $k . ' matched';
}
else
{
echo 'no match';
}
But if you are going to use these arrays for anything else, it may still be a good idea to restructure them. Well, depending on what you need them for. Also note that I've no idea how ldap works, so if you use these as input data for ldap later on, these array might need to retain their current format. Otherwise, this might be a start
function fix_insanities($arr)
{
# function is only intended to work on arrays
if (!is_array($arr))
return;
# remove the "count" element
unset($arr['count']);
# process all other elements in the array
foreach ($arr as $k => $v)
{
# process nested arrays recursively
if (is_array($v))
{
$arr[$k] = fix_insanities($v);
# If the array contains only one element after recursion
# "flatten" it. That is, it doesn't need to be an array at all...
if (count($arr[$k]) == 1)
{
$arr[$k] = $arr[$k][0];
}
}
# If this element's only "purpose" is to annoyingly
# tell you that there is another element which uses this element's value
# as a key, then remove it
elseif (is_numeric($k) && isset($arr[$v]))
{
unset($arr[$k]);
}
}
return $arr;
}
$units = fix_insanities($units);
printf('<pre>%s</pre>', print_r($units,1));