i have an array like this:
Array
(
[206] => Array
(
[1] => Array
(
[formatid] => 1
[format] => CD
[formatseourl] => cd
)
)
[556] => Array
(
[] => Array
(
[formatid] =>
[format] =>
[formatseourl] =>
)
)
[413] => Array
(
[3] => Array
(
[formatid] => 3
[format] => CASETE
[formatseourl] => casete
)
)
)
...and for a specific key i want to get the values, but before i need to validate if there are values. The code is build but i dont know how to validate if there are values inside the array... ive tried isset, empty, count, array_key_exists with no success... i know im close...
$key = 556;
//if there are no formats in array, dont bother to create the ul
if (?????????formats exist in $array[$key]) { ?>
<ul><?php
foreach ($array[$key] as $value) { ?>
<li><?php echo $value['format']; ?></li><?php
} ?>
</ul><?php
} ?>
Try this..
<?php
$key = 556;
$desiredValue = "format";
$subArray = $array[$key];
$arrayForFunction = array_pop($subArray);
if(array_key_exists($desiredValue,$arrayForFunction) && strlen($arrayForFunction[$desiredValue]))
echo "<ul><li>".$arrayForFunction[$desiredValue]."</li></ul>";
?>
Call this function as format_exists($array[$key])
which will dig into the value to see if format isset.
function format_exists($values) {
if (!$values || !is_array($values)) return false;
$data = array_shift($values);
return $data && is_array($data) && isset($data["format"]);
}