0

My images folder has files format like below

    <img src='images/AAA_1.jpg'>
    <img src='images/AAA_2.jpg'>
    <img src='images/AAA_3.jpg'>
    <img src='images/BBB_1.jpg'>
    <img src='images/BBB_2.jpg'>
    <img src='images/BBB_3.jpg'>
    <img src='images/CCC_1.jpg'>
    <img src='images/DDD_1.jpg'>
    <img src='images/EEE_1.jpg'>
    <img src='images/EEE_2.jpg'>
......

my function is

foreach ($those as $image) {
  if(strpos($image, $name) === 0){
          echo "<div class='box'><img src='imgs/$image'/></div>";
   }
}

$those is an Array

  Array  ( [0] => AAA_1.jpg [1] => AAA_2.jpg [2] => AAA_3.jpg [3] => BBB_1.jpg [4] => BBB_2.jpg [5] => BBB_3.jpg [6] => CCC_1.jpg ........

how do we test if the same format file name has one more ? like file name starts AAA has 3, BBB has 3, C has 1, D has 1, EEE has 2,

I'd like to achieve is that

if ( check if the file name contains same string more than once ) {
      echo "<div class='box'><img src='imgs/$image'/></div>";
   }else{
      echo "<div class='container'><img src='imgs/$image'/></div>";
   }

2 Answers 2

1
// Assocative array containting quantities
// of duplicated images Array( [AAA] => 3, [BBB] => 3, [CCC] => 1, [DDD] => 1, [EEE] => 2)
$duplicates = Array();

// Prepare $duplicates
foreach ($those as $image) {
   // $base is string `AAA`, `BBB`
   list($base) = explode("_", $image, 2);
   $duplicates[$base] = isset($duplicates[$base]) ? ($duplicates[$base]+1) : 1;
}

// Now go thru your loop
foreach ($those as $image) {
   list($base) = explode("_", $image, 2);
   if ($duplicates[$base] > 1) {
       echo "<div class='box'><img src='imgs/$image'/></div>";
   }else{
       echo "<div class='container'><img src='imgs/$image'/></div>";
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can modify my answer from here to do this. This is not the most glamorous solution but does the trick.

Loop over the array once, counting the occurences of the specified string, then loop again to output your results:

function filter($name){
    global $carousel;
    // create array to store match counts
    $matches = array();
    foreach($carousel as $image) {
        if(!array_key_exists($name, $matches))
            $matches[$name] = 0; // initialize array keys

        if(strpos($image, $name) === 0)
            $matches[$name]++; // add to the count
    }
    // got the counts, do the outputs
    foreach($carousel as $image) {
        $class_name = 'container'; // default (only one match)
        if($matches[$name] > 1) // get number of matches from previous loop
            $class_name = 'box';
        $html = "<div class='%s'><img src='imgs/%s'/></div>" . PHP_EOL;
        echo sprintf($html, $class_name, $image); // output formatted string
    }
}

Note: use stripos() for case insensitive string comparison

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.