Jump to content

How Do I Count An Array?


ricky spires

Recommended Posts

hello.

 

How do i count how many pics in this array?

 

$aUnits = array('pic1.jpg => '1', 'pic2.jpg => '2', 'pic3.jpg => '3');

 

i tried this but its not working

 

echo count($aUnits);

 

 

thanks

rick

 

You were missing some quotation marks.

 

$aUnits = array('pic1.jpg' => '1', 'pic2.jpg' => '2', 'pic3.jpg' => '3');

true.. but im my actuall code they are not missing

 

this is the code:

 

<?
$sThumbTemplate = <<<HTML
<li><a href="#" rel="nofollow" title="{title}"><img src="{fileurl}" alt="{title}" /></a></li>
HTML;
$sImageTemplate = <<<HTML
<div class="sliderkit-panel">
  <img src="{fileurl}" alt="{title}" />
  <div class="sliderkit-panel-textbox">
   <div class="sliderkit-panel-text">
	   <div class="arrow-left"><a rel="nofollow" href="#" title="Next line"></a></div>
  <p>{IMGno}/7</p>
  <div class="arrow-right"><a rel="nofollow" href="#" title="Next photo"></a></div>
   </div>
   <div class="sliderkit-panel-overlay"></div>
  </div>
</div>
HTML;
$sThumbs = $sImages = '';
$sFolder = 'assets/img/slider/';
$aUnits = array(
  'pic1.jpg' => '1', 'pic2.jpg' => '2', 'pic3.jpg' => '3', 'pic4.jpg' => '4',
  'pic5.jpg' => '5', 'pic6.jpg' => '6', 'pic7.jpg' => '7'
);
foreach ($aUnits as $sFileName => $sTitle) {
  $sThumbs .= strtr($sThumbTemplate, array('{fileurl}' => $sFolder . 't_' . $sFileName, '{title}' => $sTitle));
  $sImages .= strtr($sImageTemplate, array('{fileurl}' => $sFolder . $sFileName, '{title}' => $sTitle, '{IMGno}' => $sTitle, '{id}' => $sID));
}
header("Content-Type: application/json");
require_once('Services_JSON.php');
$oJson = new Services_JSON();
echo $oJson->encode(array('thumbs' => $sThumbs, 'images' => $sImages));
?>

 

 

if you look for

<p>{IMGno}/7</p>

at the top. i want to replace the 7 with the number of pics in the array

You could add a new placeholder into $sImageTemplate, such as {IMGcount}.

 

Then get the number of items in the $aUnits array using count.

$iUnitsCount = count($aUnits);

 

Then replace the placeholder by adding it into the array for strtr.

$sImages .= strtr($sImageTemplate, array(..., '{IMGcount}' => $iUnitsCount));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.