PHP echo a variable only once if array returns at least one value
<p>Ok so I have value being returned from a database in an <code>ARRAY</code> Some pages have 3 values, and some pages have 10, and other have 0.</p>

<p>If a page returns a value I want the label to appear ONCE, hence not in the loop.
label -> <code>$key_label = '&lt;span class="hl_reference"&gt;&lt;u&gt;key references:&lt;/u&gt;&lt;/span&gt;';</code></p>

<p>If the page doesn't have any <code>$reference_keys</code> I don't want the label to appear, hence <code>$key_label = false;</code></p>

<p>So I want to check the <code>ARRAY</code> for a value and if >= 1: the label will also appear.</p>

<p>My code doesn't seem to be validating <code>if(array($value =&gt; 1) &amp;&amp; !$key_label)</code> I guess my syntax is wrong or maybe I should be using a different function?</p>

<p>Thanks for help!</p>

<pre><code>$key_label = false;
$key_label = '&lt;span class="hl_reference"&gt;&lt;u&gt;key references:&lt;/u&gt;&lt;/span&gt;';

foreach($reference_keys as $value)
{
    echo $value .'&amp;nbsp;';
}

if(array($value =&gt; 1) &amp;&amp; !$key_label) 
{
    $key_label = true; 
    echo $key_label;
};
</code></pre>

<p>-------------------------------UPDATE-----------------------------</p>

<p>This code appears to only work on pages with only 1 reference_key... Strange as <code>count is set to &gt;= 1</code></p>

<pre><code>$key_label = '&lt;span class="hl_reference"&gt;&lt;u&gt;key references:&lt;/u&gt;&lt;/span&gt;';

foreach($reference_keys as $value)
{
   echo $value .'&amp;nbsp;';
}
$result = count($value);
if ($result &gt; 1)
{
  echo $key_label;
}   
</code></pre>

<p>-------------------------------UPDATE-----------------------------</p>

<p>Most recent code by John C appears to work but it displays a "1" immediately before the 2nd <code>$reference_key</code> on each page. But it does only display on pages with <code>$reference_key</code></p>

<pre><code>$key_flag = false;
$key_label = '&lt;span class="hl_reference"&gt;&lt;u&gt;key references:&lt;/u&gt;&lt;/span&gt;';

foreach($reference_keys as $value)
{
if (!empty($value)) {
    if (!$key_flag) {
        echo $key_label;
        $key_label = true;
    }
    echo $value .'&amp;nbsp;';
}
};
</code></pre>

<p>-------------------------------UPDATE-----------------------------</p>

<p>John C solved this one.. Working code below! Thank you everyone! -></p>

<pre><code>$key_flag = false;
$key_label = '&lt;span class="hl_reference"&gt;&lt;u&gt;key references:&lt;/u&gt;&lt;/span&gt;';

foreach($reference_keys as $value)
{
if (!empty($value)) {
    if (!$key_flag) {
        echo $key_label;
        **$key_flag = true;**
    }
    echo $value .'&amp;nbsp;';
}
};
</code></pre>
