Display matching arrays values
Here in the below method, we created two arrays and assign to $users and $admins variables,now to compare these arrays we added for loop as shown below, and get count of $admins variable. In the previous article we had used arrays concept which will be useful for reference.
- https://www.eknowledgetree.com/codeigniter/fetch-form-data-using-angularjs-display-codeigniter-view/
we had in_array() method to get the matching array values,let us know about in_array() method,it checks for existing array values,now let’s see syntax below
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Here in the place of needle we need to add searched variable as $admins[i], which compares each and every value in $users and prints the matching values as below.
public function array_exists_display() { $users = array(1, 2, 4, 6, 8); $admins = array(2,6, 4, 5, 8, 9); for($i = 0; $i < count($admins); $i++) { if(in_array($admins[$i],$users)) { echo $admins[$i]; } } } output: 2 4 6 8
we can also display unique values in arrays by using array_unique() method,here in the below example we created array with duplicate values.
public function array_unique_results() { $values= array("3",1,"2",2,"1","3","3"); $results=array_unique($values); print_r($results); } output: Array ( [0] => 3 [1] => 1 [2] => 2 )
Thanks for reading this article
Leave A Comment