I am counting the difference between two days in inside a foreach loop.
foreach ($result as $key => $value) {
# code...
//$temp[$value->user_id]=$value->user_id;
$count_dates=daysBetween($value->user_source_history_added,$current_date);
$tmp_array[$count_dates][] = $count_dates;
}
On debugging tmp_array, I get something like this.
Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
)
[1] => Array
(
[0] => 1
)
[3] => Array
(
[0] => 3
[1] => 3
[2] => 3
[3] => 3
)
)
Now I want to count the number of 0's, 1's, 2's, 3's, etc. So for now there are 7 0's and 1 1's and 4 3's. How do I get the count of all these numbers and how do I limit it that I get only the count till Array 20??
I tried this:
foreach($tmp_array as $tmp_val)
{
count($tmp_val)
}
But I get the count of main array that is 3
If you want the count of everything added together:
$count = 0;
foreach($tmp_array as $tmp_subarray)
{
foreach($tmp_subarray as $tmp_val) {
if($count < 20) {
$count++;
}
}
}
If you want the amount of 1s, 2s, 3s, etc:
$count = array();
foreach($tmp_array as $tmp_subarray)
{
foreach($tmp_subarray as $tmp_val) {
if($count[$temp_val]) {
$count[$temp_val]++;
} else {
$count[$temp_val] = 1;
}
}
}
This will count all the values of an array no matter how many dimensions assuming the values are arrays or numbers. This will run until the main array has reached $key == 20.
$total = 0;
function addToTotal($number) {
glob $total;
$total = $total + $number;
}
function countArrayValues($array) {
foreach($array as $key => $value) {
if(is_array ($value) {
countArrayValues($value);
} else {
addToTotal($value);
}
}
}
$mainArray; // <-- your array;
foreach($mainArray as $key => $value) {
if($key <= 20) {
countArrayValues($value);
} else {
break;
}
}
echo $total;
Try this :
$array_number_of_numbert = array();
foreach($tmp_array as $tmp_val_num => $tmp_val)
{
$array_number_of_numbert[$tmp_val_num]= $tmp_val;
}
for($i = 0; $i <=20; $i++)
{
if(isset($array_number_of_numbert[$i])) echo count($array_number_of_numbert[$i])." ". $i."'s\n";
}
if($count_array<=20){}
This is a general solution, it will take in consideration any number in you have in your $temp array and it will store this number as his record in this array
$found_numbers=array();
$results=array();
$count_array=0;
foreach($tmp_array as $first_array)
{ $count_array++;
foreach($first_array as $second_array)
{
if($count_array<=20){
if (in_array($second_array, $found_numbers)) {
$results[$second_array][0]++;
}
else{
array_push($found_numbers,$second_array);
$results[$second_array] = array();
array_push($results[$second_array],1);
}
}
}
}
//to get how many you have number n in your array you have only to type print($results[n][0]);
print($results[0][0]); //will give you 7
print($results[n][0]); //will give the record of the number n in your 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.