egy satria
egy satria

Reputation: 3

How do you avoid the same data in foreach, on sequential data?

This is the data now

string(3) "3.1"

string(3) "3.2"

string(3) "3.3"

string(3) "3.4"

string(3) "3.1"

string(3) "3.2"

And this is the data I want

string(3) "3.1"

string(3) "3.2"

string(3) "3.3"

string(3) "3.4"

stop looping here

string(3) "3.1"

string(3) "3.2"

and the following code that I made

<?php 
$tempData = null; 
foreach ( $nilai as $key ) : 
  if( $tempData !== $key->no_kd ) :
      echo "<pre>"; var_dump($key->no_kd); echo "</pre>";
      $tempData = $key->no_kd; 
   endif; 
endforeach; 
?>

Upvotes: 0

Views: 68

Answers (1)

Tola Bincom
Tola Bincom

Reputation: 36

you can try this

<?php
$tempData = array();
foreach ($nilai as $key){
    if(!(in_array($tempData,$key->no_kd))){
        echo "<pre>"; var_dump($key->no_kd); echo "</pre>";
        $tempData[] = $key->no_kd;
    }
}
?>

Upvotes: 1

Related Questions