I have array like this i need to count by array values
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ) [MSN] => Array ( [0] => Dozen ) [NeK] => Array ( [0] => Suhan [1] => Ebao ) [NetSE] => Array ( [0] => SuZhan [1] => Guhang ) )
For example
Array ( [Cop] => Array ( [0] => Dozen [1] => Dozen [2] => Akls [3] => Akls ))
In the Cop key i have two different values for cop so i need cop should be 2
Cop - 2
MSn - 1
NeK - 2
NetSE - 2
I need the count like above how can i do this ?
Try simply using array_map,count,& array_unique like as
array_map(function($v) {
return count(array_unique($v));
}, $arr);
Use array_unique() and then count.
count(array_unique($array['Cop']));// output 2
If you want to print for every key do following:
$array = array('Cop'=>array('Dozen','Dozen','Akls','Akls'), 'MSN'=> array('Dozen'), 'NeK'=> array('Suhan','Ebao'));
foreach($array as $key => &$value) {
$value = count(array_unique($array[$key]));
}
print_r($array);
Output:
Cop = 2
MSN = 1
NeK = 2
You should use array_count_values() for that, here's an example:
$data = array('cop' => array(0 => 'test', 1 => 'test', 2 => 'test2'));
foreach($data as $item){
$result = array_count_values($item);
print_r($result);
}
Outputs:
Array
(
[test] => 2
[test2] => 1
)
Related
I have an array which looks like this below:
Array ( [:status0] => 1 [:status1] => 2 )
I would want to convert it into something like this:
Array ( [:status0] => Array ( [0] => 1 [1] => 1 ) [:status1] => Array ( [0] => 2 [1] => 1 ) )
i want to do this with flexibility because the number of the array and names are random. I was thinking of using a for loop something like this:
foreach ($newParam as $row){
$newArray[$row['Continent']][$row['Country']][] = $row['City'];
}
But i cant use this in my case, please help
This code should do what you want:
$newParam = Array ( ':status0' => 1, ':status1' => 2 ) ;
foreach ($newParam as $key => $value) {
$newArray[$key] = array($value, 1);
}
print_r($newArray);
Output:
Array (
[:status0] => Array (
[0] => 1
[1] => 1
)
[:status1] => Array (
[0] => 2
[1] => 1
)
)
Demo on 3v4l.org
I would like to sum up several arrays. I have a script that dynamically creates arrays without name. Below is an example. In my script i have a 90+ arrays. I want to sum up all it. All keys in that arrays be user id, so i only want sum values of keys. How to do it? Regards
Array
(
[1] => 1
[2] => 1
[3] => 1
)
Array
(
[1] => 1
[2] => 1
)
Array
(
[1] => 1
)
I want to get only one array result like:
Array
(
[1] => 3
[2] => 2
[3] => 1
)
Add all your arrays into one array.
$all_arrays[] =Array
(
[1] => 1 ,
[2] => 1,
[3] => 1
);
$all_arrays[] = Array
(
[1] => 1,
[2] => 1
) ;
$all_arrays[] = Array
(
[1] => 1
);
$results = [];
foreach($all_arrays as $arr){
foreach($arr as $user_id=>$value){
if(in_array($user_id,$results)){
$results[$user_id] = $results[$user_id] + $value;
}else{
$results[$user_id] = $value;
}
}
}
You would need to specify all arrays in the array_merge:
$result = array_count_values(array_keys(array_merge($array1, $array2, $array3)));
If you can get the arrays dynamically added to another $main_array like:
Array
(
[0] => Array
(
[1] => 1
[2] => 1
[3] => 1
)
[1] => Array
(
[1] => 1
[2] => 1
)
[2] => Array
(
[1] => 1
)
)
Then it would be much easier:
$result = array_count_values(array_keys(array_merge(...$main_array)));
I have an array like this:
Array
(
[0] => Array
(
[0] => 1
[1] => Firstname one
[2] => Lastname one
)
[1] => Array
(
[0] => 2
[1] => Firstname two
[2] => Lastname two
)
[2] => Array
(
[0] => 3
[1] => Firstname three
[2] => Lastname three
)
)
Now, I would like to remove the entire index 1, i.e., all the firstname's from the array. I was planning on using array_splice by looping through the entire array and removing its index 1. But is there a better way. (I want the re-indexing after deletion of elements.)
$yourarray = array_map(function($el) {
unset($el[1]); //remove index 1
return array_values($el); //return and reindex
}, $yourarray);
You can also use array_slice function like
foreach($array as $k=>$v)
{
array_splice($v, 1,1);
$array[$k] = $v;
}
OUTPUT :
Array
(
[0] => Array
(
[0] => 1
[1] => Lastname one
)
[1] => Array
(
[0] => 1
[1] => Lastname two
)
[2] => Array
(
[0] => 1
[1] => Lastname three
)
)
You should use array_map
$input_array = [[1,'Firstname one','Lastname one'],
[2,'Firstname two','Lastname two'],
[3,'Firstname three','Lastname three']];
$resultArray = array_map(function($record) {
return [$record[0], $record[2]]; // add your desired records
}, $input_array);
echo '<pre>'; print_r($resultArray);
output:-
Array
(
[0] => Array
(
[0] => 1
[1] => Lastname one
)
[1] => Array
(
[0] => 2
[1] => Lastname two
)
[2] => Array
(
[0] => 3
[1] => Lastname three
)
)
foreach($array as $k=>$v)
{
unset($array[$k][1]); /// Chnage the key of array which you want to remove
}
I've got the following 2D array, stored inside the array variable $my_array
Array
(
[0] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[1] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
[2] => Array
(
[0] => 3
[1] => 6
[2] => 3
)
)
I wanted to decrement all the [1] sub array values by 3. Tried the following code, with no success.
$my_array[$i]['1']=($my_array[$i]['1'])-3;
print_r($my_array);
Ideas?
foreach ($my_array as &$val) {
$val[1] -= 3
}
Something like this is what you're after.
foreach($my_array as $k=>$v){
if (isset($my_array[$k][1]) && is_numeric($my_array[$k][1])){
$my_array[$k][1] -= 3;
}
}
I have an array contain this data
Array
(
[id] => Array
(
[0] => 1
[1] => 10
[2] => 4
)
[age] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
)
Now I want to remove duplicates from the ['age'] and leave the first one in tact.
So this would return
Array
(
[id] => Array
(
[0] => 1
[2] => 4
)
[age] => Array
(
[0] => 1
[2] => 2
)
)
Any ideas? Or is there a function already in place to do this?
Like Gordon said, you'd need a custom function to make the relationship but you can use http://php.net/manual/en/function.array-unique.php ?
Wouldn't it be better to have the keys of the age array the corresponding values of the id array?
<?php
$array = array(
'id' => array(0 => 1, 1 => 10, 3 => 4),
'age' => array(0 => 1, 1 => 1, 2 => 2)
);
array_walk($array, 'dupe_killer');
print_r($array);
function dupe_killer(&$value, $key)
{
$value = array_unique($value);
}
?>
You could try this
$array = array('id' => array(1,10,4), 'age'=>array(1,1,2));
$age_array = array();
foreach ($array['age'] as $key => $val) {
if (in_array($val, $age_array))
unset($array['id'][$key], $array['age'][$key]);
$age_array[] = $val;
}
print_r($array);
this returns Array ( [id] => Array ( [0] => 1 [2] => 4 ) [age] => Array ( [0] => 1 [2] => 2 ) )
Regards
Luke