I have multi-dimensional array:
$array = array(
array(
"id" => 1,
"value" => 100
),
array(
"id" => 1,
"value" => 200
),
array(
"id" => 1,
"value" => 300
),
array(
"id" => 2,
"value" => 2
),
array(
"id" => 2,
"value" => 5
),
array(
"id" => 3,
"value" => 10.50
),
);
I need to get new multi-dimensional array, like this:
$newArray = array(
array(
"id" => "1",
"sum_of_values" => 600
),
array(
"id" => 2,
"sum_of_values" => 7
),
array(
"id" => 3,
"sum_of_values" => 10.50
),
);
I mean, key [sum_of_values] should counts values in sub-arrays with the same key [id].
I've tried this:
$cnt = count($array);
$finalArray = array();
$tempArray = array();
for($i = 0; $i < $cnt; $i++){
if($array[$i]["id"] == $array[$i++]["id"]){
$search = $array[$i]["id"];
$column = "value";
$sum = array_sum(
array_map(
function($data) use ($column) {
return $data[$column];
},
array_filter(
$array,
function($data) use ($search) {
return fnmatch($search, $data["id"]);
}
)
)
);
$tempArray = array(
"id" => $array[$i]["id"],
"sum_of_values" => $sum
);
$finalArray[] = $tempArray;
} else {
$tempArray = array(
"id" => $array[$i]["id"],
"sum_of_values" => $array[$i]["value"]
);
}
}
And it works, it counts values of sub-arrays with same [id]. But the problem is not returning sub-arrays, key [id] of which doesn't have same examples. In my case - sub-array with [id] = 3 will not return. Also, sub-arrays with [id] = 1 and [id] = 2 will repeat [n-n/2] times.
I think, that problem is in using cycle - for ($i = 0; $i < $cnt; $i++) and condition if ($array[$i]["id"] == $array[$i++]["id"]), because it looks stupid, but i can't find anything else.
Your function seems too complicated. How about this?
$result = array();
foreach ($array as $item) {
if (!empty($result[$item['id']]))
$result[$item['id']]['sum_of_values'] += $item['value'];
else
$result[$item['id']] = array(
'id' => $item['id'],
'sum_of_values' => $item['value']
);
}
If you print_r($result) you get:
Array
(
[1] => Array
(
[id] => 1
[sum_of_values] => 600
)
[2] => Array
(
[id] => 2
[sum_of_values] => 7
)
[3] => Array
(
[id] => 3
[sum_of_values] => 10.5
)
)
Just as an alternative: this is a sitter for array_reduce():
$totals = array_reduce($array, function($reduction, $current){
$id = $current["id"];
if (empty($reduction[$id])){
$reduction[$id] = ["id"=>$id, "sum_of_values"=>0];
}
$reduction[$id]["sum_of_values"] += $current["value"];
return $reduction;
}, []);
I prefer this sort of approach to generic looping/processing: I think it's a bit cleaner. But mileage varies, obviously.
Related
$data has
stdClass Object
(
[class] => srt-fields
[rules_field_1] => 1
[rules_condition_1] => 0
[rules_value_1] => text
[rules_field_2] => 3
[rules_condition_2] => 1
[rules_value_2] => another_text
...
)
Now I have another array $newdata, I need to have index $newdata['rules'] so that it should be something like:
$newdata['rules'] => array(
[field] => 1,
[condition] => 0,
[value] => text
),
array(
[field]=> 3,
[condition] =>1,
[value] => another_text
),
...
Thanks!
You could iterate over the properties of an object like an array:
$newdata['rules']=[];
foreach ($data as $key => $value) {
if (substr($key,0,6)=='rules_') {
// split key using '_'
$parts = explode('_',$key);
// get the 'name'
$name = $parts[1] ;
// get the index (-1 to be 0 based)
$idx = $parts[2] - 1;
// store data in new array
$newdata['rules'][$idx][$name] = $value;
}
}
print_r($newdata);
Outputs:
Array
(
[rules] => Array
(
[0] => Array
(
[field] => 1
[condition] => 0
[value] => text
)
[1] => Array
(
[field] => 3
[condition] => 1
[value] => another_text
)
)
)
Here is working code,
$data = [
"class" => "srt-fields",
"rules_field_1" => "1",
"rules_condition_1" => "0",
"rules_value_1" => "text",
"rules_field_2" => "3",
"rules_condition_2" => "1",
"rules_value_2" => "another_text",
];
$result = [];
foreach ($data as $k => $v) {
$num = filter_var($k, FILTER_SANITIZE_NUMBER_INT);
if (!empty($num)) {
$result['rules'][$num][(str_replace(['rules_', '_'], '', preg_replace('/[0-9]+/', '', $k)))] = $v;
}
}
$result['rules'] = array_values($result['rules']);
print_r($result);
str_replace — Replace all occurrences of the search string with the replacement string
filter_var — Filters a variable with a specified filter
preg_replace — Perform a regular expression search and replace
str_replace — Replace all occurrences of the search string with the replacement string
Here is working demo.
Definitely do not use regular expressions for this task -- because it is unnecessary resource overhead. Explode the keys on underscores, and use the individual components to construct the multi-level keys in your output array.
Code: (Demo)
$data = (object)[
"class" => "srt-fields",
"rules_field_1" => "1",
"rules_condition_1" => "0",
"rules_value_1" => "text",
"rules_field_2" => "3",
"rules_condition_2" => "1",
"rules_value_2" => "another_text"
];
foreach ($data as $key => $value) {
$bits = explode('_',$key); // this will produce a 1-element array from `class` and a 3-element array from others
if (isset($bits[2])) { // if element [2] exists, then process the qualifying data
$newdata[$bits[0]][$bits[2]-1][$bits[1]] = $value;
// ^^- make zero based
}
}
var_export($newdata);
Output:
array (
'rules' =>
array (
0 =>
array (
'field' => '1',
'condition' => '0',
'value' => 'text',
),
1 =>
array (
'field' => '3',
'condition' => '1',
'value' => 'another_text',
),
),
)
I am using -1 to make the output keys resemble a zero-based / indexed array. If your fields might not be consecutively ordered, you can remove the -1 and write $newdata['rules'] = array_values($newdata['rules']); after the loop.
$i = 0;
$j = 0;
foreach($data as $key=>$value){
if($j == 0)
{
$newdata['rules'][$i]['field'] = $value;
}
if($j == 1)
{
$newdata['rules'][$i]['condition'] = $value;
}
if($j == 2)
{
$newdata['rules'][$i]['value'] = $value;
$i++;
}
$j++;
if($j > 2)
{
$j = 0;
}
}
You can try this code. Please ignore syntax error as I have not tried this code but it should give you the result.
I have an array within an array like this:
Array
(
[0] => Array
(
[name] => B
[id] => 924572
)
[1] => Array
(
[name] => A
[id] => 120689
)
[2] => Array
(
[name] => A
[id] => 120689
)
[3] => Array
(
[name] => C
[id] => 919644
)
[4] => Array
(
[name] => A
[id] => 120689
)
[5] => Array
(
[name] => B
[id] => 924572
)
)
How can I get the most repeated value from object named name and id?
I've already tried the code below but I'm getting an error: Warning: array_count_values(): Can only count STRING and INTEGER values!
$count = array_count_values($info);
arsort($count);
$popular = array_keys($count);
echo $popular[0];
Any fix regarding to this problem?
"Serializing way" for searching most repeated couples (name,id):
$out = array();
foreach($arr as $el){
$key = serialize($el);
if (!isset($out[$key]))
$out[$key]=1;
else
$out[$key]++;
}
arsort($out);
foreach($out as $el=>$count){
$item = unserialize($el);
echo "Name = ".$item['name'].' ID = '.$item['id'].' Count = '.$count.'<br/>';
}
Output:
Name = A ID = 120689 Count = 3
Name = B ID = 924572 Count = 2
Name = C ID = 919644 Count = 1
update Without loop
.....
arsort($out);
$most = unserialize(key($out));
$most_count = array_shift($out);
echo $most['name'];
echo $most['id'];
echo $most_count;
Output:
A
120689
3
A more linear solution.
$arr = Array
(
Array
(
"name" => "B",
"id" => 924572
),
Array
(
"name" => "A",
"id" => 120689
),
Array
(
"name" => "A" ,
"id" => 120689
),
Array
(
"name" => "C",
"id" => 919644
),
Array
(
"name" => "A",
"id" => 120689
),
Array
(
"name" => "B",
"id" => 924572
));
$countArr = Array();
for($i = 0; $i < count($arr); $i++)
{
$tmpArr = $arr[$i];
if(array_key_exists($tmpArr["name"],$countArr))
$countArr[$tmpArr["name"]]++;
else
$countArr[$tmpArr["name"]] = 0;
}
arsort($countArr);
var_dump($countArr);
Maybe you can work with this solution:
<?php
$info = array(
array(
"name" => "B",
"id" => 924572
),
array(
"name" => "A",
"id" => 120689
),
array(
"name" => "A",
"id" => 120689
),
array(
"name" => "C",
"id" => 919644
),
array(
"name" => "A",
"id" => 120689
),
array(
"name" => "B",
"id" => 924572
),
);
$result = array();
foreach ($info as $infoKey => $infoValue) {
foreach ($infoValue as $itemKey => $itemValue) {
if ($itemKey != "name") {
continue;
}
if (array_key_exists($itemValue, $result)){
$result[$itemValue]++;
continue;
}
$result[$itemValue] = 1;
}
}
arsort($result);
var_dump($result);
Will result in:
array (size=3)
'A' => int 3
'B' => int 2
'C' => int 1
Based on finding the mode and mapping in PHP. Would this work?
$name_array = array_map(function($x) {return $x["name"];}, $info);
$count = array_count_values($name_array);
$mode = array_keys($count, max($count));
To return an array of "name", "id" pairs use:
$return_value = array_filter($info, function($x) use ($mode) { return (in_array($x["name"], $mode));});
Makes use of array_column (requires PHP 5.5 or shim).
$count_values = array_count_values(array_column($array, 'name'));
$most_frequent_name = array_search(max($count_values), $count_values);
Then if you want all arrays with this name:
$items = array_filter($array, function ($v) use ($most_frequent_name) {
return $v['name'] == $most_frequent_name;
});
If several names may have the same top frequency:
$count_values = array_count_values(array_column($array, 'name'));
$most_frequent_names = array_keys($count_values, max($count_values));
$items = array_filter($array, function ($v) use ($most_frequent_names) {
return in_array($v['name'], $most_frequent_names);
});
Try following code. It will give you count of occurrences of all elements
function array_icount_values($arr,$lower=true) {
$arr2=array();
if(!is_array($arr['0'])){$arr=array($arr);}
foreach($arr as $k=> $v){
foreach($v as $v2){
if($lower==true) {$v2=strtolower($v2);}
if(!isset($arr2[$v2])){
$arr2[$v2]=1;
}else{
$arr2[$v2]++;
}
}
}
return $arr2;
}
$arr = array_icount_values($array);
echo "<pre>";
print_r($arr);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to sum array in same key and different value.
Array
(
0 => Array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 2
),
1 => Array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 1
),
2 => Array
(
'locker_id' => 2,
'locker_model' => 1,
'qty' => 2
),
3 => Array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 2
),
4 => Array
(
'locker_id' => 2,
'locker_model' => 1,
'qty' => 2
)
);
I want Output it
Array
(
0 => Array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 5
),
1 => Array
(
'locker_id' => 2,
'locker_model' => 1,
'qty' => 4
)
);
Thanks.
You could browse your array while creating a new one with keys like "locker_id-locker_model", then you can easily check if your locker exists in this array with array_key_exists function.
$inputArray = array( ... ); // this is your message array
$outputArray = array();
foreach( $inputArray as $locker )
{
if( !array_key_exists( $locker["locker_id"]."-".$locker["locker_model"], $outputArray ) )
{
$outputArray[ $locker["locker_id"]."-".$locker["locker_model"] ] = array(
"locker_id" => $locker["locker_id"],
"locker_model" => $locker["locker_model"],
"qty" => 0
);
}
$outputArray[ $locker["locker_id"]."-".$locker["locker_model"] ]["qty"]++;
}
var_dump( $outputArray );
$dataArr = array
(
0 => array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 2
),
1 => array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 1
),
2 => array
(
'locker_id' => 2,
'locker_model' => 1,
'qty' => 2
),
3 => array
(
'locker_id' => 3,
'locker_model' => 1,
'qty' => 2
),
4 => array
(
'locker_id' => 2,
'locker_model' => 1,
'qty' => 2
)
);
$sumArr = array();
if(count($dataArr)>0){
foreach($dataArr as $data){
if(!isset($sumArr[$data['locker_id']])){
$sumArr[$data['locker_id']] = $data;
}
$sumArr[$data['locker_id']]['qty'] +=$data['qty'];
}
}
echo "<pre>";print_r($sumArr);
Considering $array1 is your first array,
$array2 = array();
foreach($array1 as $k=>$v) {
if(!isset($array2[$v['locker_id']])) {
$array2[$v['locker_id']] = $v;
} else {
$array2[$v['locker_id']]['qty'] += $v['qty'];
}
}
print_r($array2);
Try this
$arr is your array
$j = 0;
$new = array();
$new[0][qty];
for($i=0;$i<arraylength;$i++){
foreach($arr as $key => $value) {
if($arr[$i][lockerid] == $arr[$key][lockerid]) {
$new[$j][lockerid] = $arr[$key][lockerid];
$new[$j][lockermodel] = $arr[$key][lockermodel];
$new[$j][qty] = $new[$i][qty] + $arr[$key][qty];
J++;
}
}
}
$result = array();
foreach($array as $key => $value){
if(isset($value['locker_id'])){
$result[$value['locker_id']]['qty'] += $value['qty'];
}
else{
$result[$value['locker_id']] = $value;
}
}
You have to loop over your array and save it to another let's say $result. In $result you should put the locker_id as key and then you only have to verify if that key exists. If it is you add the qty value, if isn't you have to add the entire new item.
try this
$ARR_OUTPUT = array();
// $arr will contains your input array
foreach($arr as $arr_val)
{
$locker_id = $arr_val['locker_id'];
$locker_model = $arr_val['locker_id'];
$qty = $arr_val['locker_id'];
if(isset($ARR_OUTPUT[$locker_id][$locker_model]))
{
$ARR_OUTPUT[$locker_id][$locker_model] += $qty;
}
else
{
$ARR_OUTPUT[$locker_id][$locker_model] = $qty;
}
}
echo "<pre>";
print_r($ARR_OUTPUT);
$ARR_OUTPUT2 = array();
foreach($ARR_OUTPUT as $locker_id=>$arr_values)
{
foreach($arr_values as $locker_model=>$qty)
{
$arr_temp['locker_id'] = $locker_id;
$arr_temp['locker_model'] = $locker_model;
$arr_temp['qty'] = $qty;
$ARR_OUTPUT2[] = $arr_temp;
}
}
echo "<pre>";
print_r($ARR_OUTPUT2);
I have two arrays that I want to merge recursively, so adding arrays is not an option. This is simple example without multilevels to demonstrate the problem:
$a1 = Array(
5 => 'pronoun'
)
$a2 = Array(
2 => 'verb',
3 => 'noun'
)
$r = array_merge_recursive($a1, $a2)
And I want to get that resulting array:
Array(
5 => 'pronoun'
2 => 'verb',
3 => 'noun'
)
My problem is that array_merge_recursive function reindixes keys, and I get the following:
Array(
0 => 'pronoun'
1 => 'verb',
2 => 'noun'
)
I understand that's happening because all my keys are numeric. So I tried to make them string when adding but it doesn't seem to be working properly:
$a1[(string)7] = 'some value';
The key - 7 - is still number, or at least that's how it is displayed in debugger - $a1[7] and not $a1['7']. Any advice?
EDIT:
Addition of arrays is not an option. Please see why. I have two multilevel arrays:
$a1 = array (
1 => array (
1 => "man1",
2 => "man"
),
2 => array (
1 => "run",
2 => "nice"
)
);
$a2 = array(
2 => array (
1 => "to observe",
2 => "to examine visually"),
3 => array(
1 => "look nice",
2 => "appear, seem to be"));
$r = $a1 + $a2;
What I expect is the following:
$r = Array(
...
2 => array(
1 => array("run", "to observe")
2 => array("nice", "to examine visually")
));
But instead the options for the key 2 from the second array is not added:
$r = Array(
...
2 => array(
1 => "run",
2 => "nice"
));
you can just use $a1+$a2 to get your result
$a1 = array(
5 => 'pronoun'
);
$a2 = array(
2 => 'verb',
3 => 'noun'
);
print_r($a1+$a2);
For recursive array
$a1 = array(
5 => 'pronoun'
);
$a2 = array(array('a', 'z'), array(2 => 'p', 'q'));
print_r($a1+$a2);
Result is
Array
(
[5] => pronoun
[0] => Array
(
[0] => a
[1] => z
)
[1] => Array
(
[2] => p
[3] => q
)
)
is this what you want to achieve?
This should apply to your particular problem:
function assoc_merge(array $a, array $b)
{
$r = array();
foreach ($a as $key => $val) {
if (array_key_exists($key, $b) && is_array($val) == is_array($b[$key])) {
if (is_array($val)) {
$r[$key] = assoc_merge($a[$key], $b[$key]); // merge array
} else {
$r[$key] = array($val, $b[$key]); // merge entry
}
} else {
$r[$key] = $val; // just copy
}
}
return $r + $b; // add whatever we missed
}
print_r(assoc_merge($a1, $a2));
You can always try the manual case.
function merge () { // takes any number of arguments
$arrays = func_get_args();
$result = array();
foreach ($arrays as $array)
foreach ($array as $key => $item)
if (is_array($item))
$result = merge($result, $item);
else
$result[$key] = $item;
return $result;
Sure, it's slow, but it will work.
$a1 = array(
5 => 'pronoun'
);
$a2 = array(
2 => 'verb',
3 => 'noun'
);
foreach($a2 as $key=>$value) {
$a1[$key] = $value;
}
print_r($a1);
<?php
$a = [ 5 => 'pronoun'];
$b = [ 2 => 'verb', 3 => 'noun'];
$m = array_merge(array_keys($a), array_keys($b));
$final = array_combine($m, array_merge_recursive($a, $b));
print_r($final);
I have a large array where I basically need to count the number of uniques:
example array
The end result I am looking for something like
$result = array(
'A3D5' => array('P2' => 1, 'P3' => 1, 'P4' => 1, 'total' => 3),
'AE5S' => array('P4' => 1, 'total' => 1)
);
I've tried foreaching through but can't seem to figure out how to sort them into another key, something like $zone = "P{$array['zone']}"; $result[$zone][$prestige]++ or seems to kind of not work or just error.
$array = array(
"denizen" => array
(
"prestige" => 2,
"zone" => "A3D5",
"scope_frag" => 765
),
"생각" => array
(
"prestige" => 4,
"zone" => "A3D5",
"scope_frag" => 135
),
"Ryans" => array
(
"prestige" => 3,
"zone" => "A3D5",
"scope_frag" => 78
),
"지적인" => array
(
"prestige" => 2,
"zone" => "AE5S",
"scope_frag" => 481
)
);
foreach ($array as $group) {
$zones[$group["zone"]][] = $group["prestige"];
}
foreach ($zones as $name => $zone) {
$total = count($zone);
foreach ($zone as $prestige) {
$result[$name]["P".$prestige] += 1;
}
ksort($result[$name]);
$result[$name]["total"] = $total;
}
echo "<pre>";
print_r($result);
echo "</pre>";