how to get index of repeated data from a multi dimension array using array_search() or array_column() method
function Search($value, $array)
{
return(array_search($value, $array,false));
}
$array = array(45, 5, 1, 22, 22, 10, 10);
$value = "10";
$index1= Search($value, $array);
echo $index1;
this displays index of first '10' from array. How do I get index of 2nd 10 from the array in $index2 varaible. Please help this will help me a lot.
It is described in array_search
manual :
function Search($value, $array)
{
return array_keys($array, $value, false);
}
$array = array(45, 5, 1, 22, 22, 10, 10);
$value = "10";
$indexes = Search($value, $array);
print_r($indexes);
You can see full documentation of array_keys
here
Use array_count_values() and array_keys
<?php
$array = array(45, 5, 1, 22, 22, 10, 10);
//use array_count_values to counts all the values of an array.
$get_repeated_value = array_count_values($array);
$final_array = array();
foreach($get_repeated_value as $key => $value){
//If value is repeated, get the index of that values from array.
if($value > 1){
$final_array[$key] = array_keys($array, $key);
}
}
echo "<pre>";
print_r($final_array);
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.