Ok so I have value being returned from a database in an ARRAY Some pages have 3 values, and some pages have 10, and other have 0.
If a page returns a value I want the label to appear ONCE, hence not in the loop.
label -> $key_label = '<span class="hl_reference"><u>key references:</u></span>';
If the page doesn’t have any $reference_keys I don’t want the label to appear, hence $key_label = false;
So I want to check the ARRAY for a value and if >= 1: the label will also appear.
My code doesn’t seem to be validating if(array($value => 1) && !$key_label) I guess my syntax is wrong or maybe I should be using a different function?
Thanks for help!
$key_label = false;
$key_label = '<span class="hl_reference"><u>key references:</u></span>';
foreach($reference_keys as $value)
{
echo $value .' ';
}
if(array($value => 1) && !$key_label)
{
$key_label = true;
echo $key_label;
};
——————————-UPDATE—————————–
This code appears to only work on pages with only 1 reference_key… Strange as count is set to >= 1
$key_label = '<span class="hl_reference"><u>key references:</u></span>';
foreach($reference_keys as $value)
{
echo $value .' ';
}
$result = count($value);
if ($result > 1)
{
echo $key_label;
}
——————————-UPDATE—————————–
Most recent code by John C appears to work but it displays a “1” immediately before the 2nd $reference_key on each page. But it does only display on pages with $reference_key
$key_flag = false;
$key_label = '<span class="hl_reference"><u>key references:</u></span>';
foreach($reference_keys as $value)
{
if (!empty($value)) {
if (!$key_flag) {
echo $key_label;
$key_label = true;
}
echo $value .' ';
}
};
——————————-UPDATE—————————–
John C solved this one.. Working code below! Thank you everyone! ->
$key_flag = false;
$key_label = '<span class="hl_reference"><u>key references:</u></span>';
foreach($reference_keys as $value)
{
if (!empty($value)) {
if (!$key_flag) {
echo $key_label;
**$key_flag = true;**
}
echo $value .' ';
}
};
If I read this right, you would like to display the
spanif there are values in your$reference_keysarray, then loop through and show the values in the array.To do this, you could do something like:
EDIT If there are empty values in the
$reference_keysarray, then thecountwill come back greater than 0 but no keys will show. If this is the case this code will work around: