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
)
Related
I am still beginner.
I want to subtract a value in an array, then I want to compare values. I have an array, the values are not known, depend on the result of a function.
Example :
$value = [5,8,13,15];
I want to subtract each value and save it in an array. Example :
8-5 = 3
13-8 = 5
15-13 = 2
then I want to compare each value (3, 5, 2), which one is bigger.
Please help me. Thank you before.
$value = [5,18,13,15];
sort($value); //to not get negative results
$loop = 0;
$results = array();
while ($loop < count($value))
{
if ($loop == 0)
{
$loop++;
}
else
{
$firstval = $value[$loop];
$secondval = $value[$loop-1];
$results[] = intval($firstval) - intval($secondval);
$loop++;
}
}
sort($results);
$thebiggestkey = $results[count($results)-1];
This should do it for you
A small example with Indexes and Array, if you here is wishes it you can use Foreach to subtract all that there is in the Array
( http://php.net/manual/en/control-structures.foreach.php )
$value = [5,8,3,13,15];
$rep = $value[0] - $value[1];
//5 - 8
echo $rep;
//return -3
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:
Closed 10 years ago.
Possible Duplicate:
Combing 3 arrays into one array
I have the following arrays:
$front = array("front_first","front_second");
$back = array("back_first", "back_second", "back_third","back_fourth");
what I'm trying to do is merge them so an output like this would result:
$final = array(
"back_first",
"front_first",
"back_second",
"front_second",
"back_third",
"front_second",
"back_fourth",
"front_second"
);
How can I have it repeat the last value in the shortest array so that it can combine into one $final[] with no empty values?.
php's array_merge
$final = array_merge($front, $back);
and maybe a combination with array_pad?
$front = array_pad($front, count($back)-1, 'second_front');
$final = array_merge($front, $back);
The first bit is easy, just use array_merge() to construct your combined array.
The second bit requires arbitrary sorting and therefore will require use of usort() to sort the array according to rules you implement in the callback function.
Is the order really important?
Working Codepad - bgIYz9iw
$final = array();
for( $i = 0; $i < 4; $i++ ) {
if( $i > 1 ) {
$final[] = $back[$i];
$final[] = $front[1];
}
else {
$final[] = $back[$i];
$final[] = $front[$i];
}
}
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"];
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php - get numeric index of associative array
$array = ('a'=>'a', 'b'=>'b');
foreach($array as $key => $value ){
//echo $position ( 1,2 )
}
Can I get the position in the array with a simple function ?
Try:
$i = 0;
$array = ('a'=>'a', 'b'=>'b');
foreach($array as $key => $value ){
$i++;
echo $i;
}
$array = array('a'=>'a', 'b'=>'b');
for ($x = 0; $x < count($array);$x++)
echo $x."<br >";