This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to find the repeating elements in an array?
If I have this array : array("hey", "test", "hey");
And I want to count how many times I have the word "hey", how can I do that?
Wouldn't it be great if there were a function like array_count_values?
</sarcasm>
Some example code of usage:
$arr = array(...);
$valCounts = array_count_values( $arr );
echo $valCounts['hey'];
I highly recommend browsing php.net and, in-particular, learning the array functions.
$count = 0;
foreach($array as $item) {
if($item == 'hey') {
$count++;
}
}
print $count;
The command is array_count_values. Check
http://www.php.net/manual/en/function.array-count-values.php
You just need to loop over the array.
$x = 0;
foreach (array("hey","test","hey") as $value) {
if ($value === "hey") $x++;
}
For a less efficient, but shorter, solution, you could use array_count_value.
$counts = array_count_values(array("hey","test","hey"));
$x = $counts["hey"];
Related
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 3 years ago.
I want to loop through an associate array.
foreach($details as $key=>$value){
echo $details['image1'];
}
Above code works fine.
What i want if i can replace the 1 in $details['image1'] to 2,3,4 ..etc
what i tried
$j=i;
foreach($details as $key=>$value){
echo $details['image.$j'];
$j++;
}
But it does not work.
It there a way to dynamically change the key of associate array.
like
'$details['image2'];
$details['image3'];'
You should use double quote mark
$j=i;
foreach($details as $key=>$value){
echo $details["image{$j}"];
$j++;
}
This is one way to do it
$j = $i;
$newArray = [];
foreach ($details as $key => $value) {
$newArray['image'. $j] = $value;
$j++;
}
In echo $details['image.$j']; $j is inserted as a literal.
You can either use
echo $details['image'.$j]; or
echo $details["image{$j}"];
to correctly concat.
Although you actually do not need a foreach loop using this syntax. A simple for-loop would be sufficient.
for ($i = 0; $i < count($details); $i++)
{
echo $details["image.{$i}"];
}
Using foreach you probably do not need to count up $i ... but that depends on your array.
Have a look at https://www.php.net/manual/en/control-structures.foreach.php
This question already has answers here:
Math average with php
(11 answers)
Closed 6 years ago.
I need to get the average of all the even numbers in this array:
$aReeks = array(23,245,1,2,12,-10,46,6,66,9999,-55,348,56,6,66,983);
Can someone please help me? I'm already trying over an hour.
I have to do it with a for/while loop.
Use array_sum() and count()
$aReeks = array(23,245,1,2,12,-10,46,6,66,9999,-55,348,56,6,66,983);
echo array_sum($Reeks) /count($aReeks);
you can get the array with only the even number this way
foreach($aReeks as $key => $value) if($key&1) unset($aReeks[$key]);
and then
echo array_sum($Reeks) /count($aReeks);
$count = 0;
$average = 0;
foreach ($aReeks as $value)
{
$count++;
$average += $value;
}
$average = $average/$count;
echo "average: {$average}<br>";
This question already has answers here:
Finding the subsets of an array in PHP
(5 answers)
Closed 7 years ago.
I will do my best to explain this idea to you. I have an array of values, i would like to know if there is a way to create another array of combined values. So, for example:
If i have this code:
array('ec','mp','ba');
I would like to be able to output something like this:
'ec,mp', 'ec,ba', 'mp,ba', 'ec,mp,ba'
Is this possible? Ideally i don't want to have duplicate entries like 'ec,mp' and 'mp,ec' though, as they would be the same thing
You can take an arbitrary decision to always put the "lower" string first. Once you made this decision, it's just a straight-up nested loop:
$arr = array('ec','mp','ba');
$result = array();
foreach ($arr as $s1) {
foreach ($arr as $s2) {
if ($s1 < $s2) {
$result[] = array($s1, $s2);
}
}
}
You can do it as follows:
$arr = array('ec','mp','ba', 'ds', 'sd', 'ad');
$newArr = array();
foreach($arr as $key=>$val) {
if($key % 2 == 0) {
$newArr[] = $val;
} else {
$newArr[floor($key/2)] = $newArr[floor($key/2)] . ',' . $val;
}
}
print_r($newArr);
And the result is:
Array
(
[0] => ec,mp
[1] => ba,ds
[2] => sd,ad
)
Have you looked at the function implode
<?php
$array = array('ec','mp','ba');
$comma_separated = implode(",", $array);
echo $comma_separated; // ec,mp,ba
?>
You could use this as a base for your program and what you are trying to achieve.
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 8 years ago.
I am facing problem when I am trying to insert values from array to mysql database.
foreach ( $_POST['product_id'] as $key=>$value AND $_POST['discount'] as $key1=>$discount) { }
check the above given code where I am going wrong?
You can use a regular for loop as long as the indexes match:
$count = count($_POST['product_id']);
for($i = 0; $i < $count; $i++) {
echo $_POST['product_id'][$i];
echo $_POST['discount'][$i];
}
use array_map this will loop through all keys in all arrays provided simultaneously.
array_map(function(){
$args = func_get_args();
foreach($args as $k => $v) {
echo $v;
}
}, $arr1, $arr2 ...);
This question already has answers here:
Count number of values in array with a given value
(8 answers)
Closed 3 years ago.
I have an array named $uid. How can I check to see how many times the value "12" is in my $uid array?
Several ways.
$cnt = count(array_filter($uid,function($a) {return $a==12;}));
or
$tmp = array_count_values($uid);
$cnt = $tmp[12];
or any number of other methods.
Use array_count_values(). For example,
$freqs = array_count_values($uid);
$freq_12 = $freqs['12'];
Very simple:
$uid= array(12,23,12,4,2,5,56);
$indexes = array_keys($uid, 12); //array(0, 1)
echo count($indexes);
Use the function array_count_values.
$uid_counts = array_count_values($uid);
$number_of_12s = $uid_counts[12];
there are different solution to this:
$count = count(array_filter($uid, function($x) { return $x==12;}));
or
array_reduce($uid, function($c, $v) { return $v + ($c == 12?1:0);},0)
or just a for loop
for($i=0, $last=count($uid), $count=0; $i<$last;$i++)
if ($uid[$i]==12) $count++;
or a foreach
$count=0;
foreach($uid as $current)
if ($current==12) $count++;
$repeated = array();
foreach($uid as $id){
if (!isset($repeated[$id])) $repeated[$id] = -1;
$repeated[$id]++;
}
which will result for example in
array(
12 => 2
14 => 1
)