ricky spires Posted November 7, 2012 Share Posted November 7, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270409-how-do-i-count-an-array/ Share on other sites More sharing options...
trq Posted November 7, 2012 Share Posted November 7, 2012 That is exactly how you count the elements within an array. Post your actual code if your still having issues. Quote Link to comment https://forums.phpfreaks.com/topic/270409-how-do-i-count-an-array/#findComment-1390769 Share on other sites More sharing options...
Beeeeney Posted November 7, 2012 Share Posted November 7, 2012 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'); Quote Link to comment https://forums.phpfreaks.com/topic/270409-how-do-i-count-an-array/#findComment-1390774 Share on other sites More sharing options...
ricky spires Posted November 7, 2012 Author Share Posted November 7, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/270409-how-do-i-count-an-array/#findComment-1390780 Share on other sites More sharing options...
salathe Posted November 7, 2012 Share Posted November 7, 2012 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)); Quote Link to comment https://forums.phpfreaks.com/topic/270409-how-do-i-count-an-array/#findComment-1390783 Share on other sites More sharing options...
ricky spires Posted November 7, 2012 Author Share Posted November 7, 2012 fixed it <p>{IMGno}/{noPics}</p> $noPics = count($aUnits); 'noPics' => $noPics thanks Quote Link to comment https://forums.phpfreaks.com/topic/270409-how-do-i-count-an-array/#findComment-1390784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.