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
So I have an array
array ( 1, 2, 2, 5, 4, 5, 4, 1, 1, 5 ) and I need to collect same array values and divide it to different arrays. So after this action from this array I should have 4 different arrays:
array ( 1, 1, 1 )
array ( 2, 2 )
array ( 5, 5, 5 )
array ( 4, 4 )
what is the best way to do it?
$result = [];
foreach (array_count_values($values) as $value => $occurrence) {
$result[] = array_fill(0, $occurrence, $value);
}
This assumes that the individual value identity doesn't matter, i.e. that you don't have objects whose individual instance you need.
Though I'm not sure why you'd need that particular array format then in the first place. Just use the result of array_count_values.
$input = array ( 1, 2, 2, 5, 4, 5, 4, 1, 1, 5 );
$output = array();
foreach ($input as $value)
$output[$value][] = $value;
var_dump($output)
You can make another array, with the distinct values as the array keys.
<?php
$array = array(1, 2, 2, 5, 4, 5, 4, 1, 1, 5);
$result = array();
foreach ($array as $val) {
if (!isset($result[$val])) { // Check if the index exists
$result[$val] = array();
}
$result[$val][] = $val;
}
print_r($result);
You can try something like below
<?php
$a = array ( 1, 2, 2, 5, 4, 5, 4, 1, 1, 5 );
$a = array_count_values($a);
$arr = array();
foreach($a as $k=>$v){
for($i = 0; $i < $v; $i++){
$arr[$k][] = $k;
}
}
print_r($arr );
?>
Output:
Array
(
[1] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[2] => Array
(
[0] => 2
[1] => 2
)
[5] => Array
(
[0] => 5
[1] => 5
[2] => 5
)
[4] => Array
(
[0] => 4
[1] => 4
)
)
Related
This question already has answers here:
Push elements from one array into rows of another array (one element per row)
(4 answers)
Closed 5 months ago.
I need to evenly/synchronously push values from my second array into the rows of my first array.
The arrays which have the same size, but with different keys and depths. The first is an array of rows and the second is a flat array.
$array1 = [
12 => [130, 28, 1],
19 => [52, 2, 3],
34 => [85, 10, 5]
]
$array2 = [4, 38, 33]
Preferred result:
[
12 => [130, 28, 1, 4],
19 => [52, 2, 3, 38],
34 => [85, 10, 5, 33]
]
(I would like to keep the same indices of array 1, however it is not mandatory.)
I have tried these methods, but none of them work because the first array keys are unpredictable.
$final = [];
foreach ($array1 as $idx => $val) {
$final = [$val, $array2[$idx]];
}
Another:
foreach ($array1 as $index => $subArray) {
$array1 [$index][] = $array2[$index];
}
Here is one way to do this:
$merged = array_map('array_merge', $array1, array_chunk($array2, 1));
$result = array_combine(array_keys($array1), $merged);
The second step with array_combine is necessary to reapply the non-sequential keys because array_map won't preserve them in the first step.
An example using foreach
<?php
$a = [
2 => [130, 28, 1, 1, 6],
3 => [52, 2, 3, 3, 27]
];
$b = [5, 38];
$output = [];
$idx = 0;
foreach ($a as $key => $value) {
$value[] = $b[$idx];
$output[$key] = $value;
++$idx;
}
print_r($output);
Sandbox HERE
You can loop $array1 using a foreach to get the current key $index
Get the value from $array2 by using a counter as the array key which, is incremented by 1 for every iteration.
Then add the value to the end of the current array.
$array1 = [
2 => [130, 28, 1, 1, 6],
3 => [52, 2, 3, 3, 27],
13 => [41, 20, 27, 13, 37]
];
$array2 = [89, 99, 109];
$counter = 0;
foreach ($array1 as $index => $subArray) {
$array1[$index][] = $array2[$counter++];
}
print_r($array1);
Output
Array
(
[2] => Array
(
[0] => 130
[1] => 28
[2] => 1
[3] => 1
[4] => 6
[5] => 89
)
[3] => Array
(
[0] => 52
[1] => 2
[2] => 3
[3] => 3
[4] => 27
[5] => 99
)
[13] => Array
(
[0] => 41
[1] => 20
[2] => 27
[3] => 13
[4] => 37
[5] => 109
)
)
See a PHP demo.
Maintaining a counter while iterating is a simple way of accessing second array values while iterating the first. It is not necessary to make multiple passes of the arrays, just one iteration is all that is required.
Codes: (Demos)
a mapper:
$i = -1;
var_export(
array_map(fn($row) => array_merge($row, [$array2[++$i]]), $array1)
);
a looper:
$i = -1;
foreach ($array1 as &$row) {
array_push($row, $array2[++$i]);
}
var_export($array1);
a walker:
$i = -1;
array_walk($array1, fn(&$row, $k) => array_push($row, $array2[++$i]));
var_export($array1);
If you don't care about preserving the first array's keys in the result array, then you can simply use:
var_export(
array_map('array_merge', $array1, array_chunk($array2, 1))
);
This question already has answers here:
How to Sum Columns of a Multi-Dimensional Array?
(4 answers)
Closed 5 months ago.
I have an arrays with dynamic name. My array could be more than 3, depends and array variable should be unique
$loopThrice = 3;
$getSum = 0;
$total = array();
$array0 = array(5, 10, 15, 20, 25, 30);
$array1 = array(1, 2, 3, 4, 5, 6);
$array2 = array(2, 6, 8, 10, 12, 14);
for($a=0; $a < $loopThrice; $a++){ // loop 3x to get unique array name
foreach (${'array'.$a} as $key => $value) { // $array0, $array1, $array2,
//Right here is my problem, I'm not sure if this the correct way to get the sum of $array0,1,2
//get the sum of array0,1,2 -- newarray(8, 18, 26, 34, 42, 50)
$getSum +=
//store newarray
array_push($total, $getSum);
}
}
I need to get an output like this:
Array (
[0] => 8
[1] => 18
[2] => 26
[3] => 34
[4] => 43
[5] => 50
)
Why aren't you using a multidimensional array?
$array = array(); // hungarian notation
$array[] = array(5, 10, 15, 20, 25, 30);
$array[] = array(1, 2, 3, 4, 5, 6);
$array[] = array(2, 6, 8, 10, 12, 14);
In this case you will have an array of arrays:
Array
(
[0] => Array
(
[0] => 5
[1] => 10
[2] => 15
[3] => 20
[4] => 25
[5] => 30
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
[2] => Array
(
[0] => 2
[1] => 6
[2] => 8
[3] => 10
[4] => 12
[5] => 14
)
)
You can go with nested for loops:
$sumArray = array();
$arrayCount = count($array);
$elementCount = 0;
foreach($array as $subarray)
{
$count = count($subarray);
$elementCount = $elementCount < $count ? $count : $elementCount;
}
for($i = 0; $i < $elementCount; $i++)
{
$sumArray[$i] = 0;
for($j = 0; $j < $arrayCount; $j++)
{
$sumArray[$i] += $array[$j][$i];
}
}
print_r($sumArray);
The output is
Array
(
[0] => 8
[1] => 18
[2] => 26
[3] => 34
[4] => 42
[5] => 50
)
Now, if you have a disproportioned sub-arrays (i.e. different count of elements in each sub-array), you will still get some sort of result, as missing elements will be assumed to be 0. So, with the input of:
$array = array(); // hungarian notation
$array[] = array(5, 10, 15, 20, 25);
$array[] = array(1, 2, 3, 4, 5, 6);
$array[] = array(2, 6, 8, 10);
You will still get the result:
Array
(
[0] => 8
[1] => 18
[2] => 26
[3] => 34
[4] => 30
[5] => 6
)
Well the multi array is the way to go, but you can still do it this way:
$loopThrice = 3;
$getSum = 0;
$total = array();
$array0 = array(5, 10, 15, 20, 25, 30);
$array1 = array(1, 2, 3, 4, 5, 6);
$array2 = array(2, 6, 8, 10, 12, 14);
// find which arrray has the most values
for($a=0; $a < $loopThrice; $a++){
$max_index = (count(${'array'.$a}) > $max_index ? count(${'array'.$a}) : $max_index);
}
for($i=0; $i < $max_index; $i++){
for($a=0; $a < $loopThrice; $a++){
$total[$i] += ${'array'.$a}[$i];
}
}
print_r($total);
// prints Array ( [0] => 8 [1] => 18 [2] => 26 [3] => 34 [4] => 42 [5] => 50 )
This should work for you:
$array0 = array(5, 10, 15, 20, 25, 30);
$array1 = array(1, 2, 3, 4, 5, 6);
$array2 = array(2, 6, 8, 10, 12, 14);
$var_prefix = 'array';
$arr_count = 0;
$max_fields = 0;
while(isset(${$var_prefix.$arr_count})) {
$data[] = ${$var_prefix.$arr_count};
if(count(${$var_prefix.$arr_count})>$max_fields) {
$max_fields = count(${$var_prefix.$arr_count});
};
$arr_count++;
}
for($i=0; $i<$max_fields; $i++) {
$result[$i] = array_sum(array_column($data, $i));
}
echo '<pre>';
print_r($result);
echo '</pre>';
die();
I don't know why you have individual array variables, but the process remains the same as if you declare a single array containing those arrays as rows in a multi-dimensional array.
Once you have a multi-dimensional array, the spread operator (...) will feed columns of data into array_map()'s custom function body -- where array_sum() can be called upon.
This task is effectively summing transposed data.
Code: (Demo)
var_export(
array_map(fn() => array_sum(func_get_args()), $array0, $array1, $array2)
);
Or:
var_export(
array_map(fn(...$cols) => array_sum($cols), $array0, $array1, $array2)
);
Output (from either approach):
array (
0 => 8,
1 => 18,
2 => 26,
3 => 34,
4 => 42,
5 => 50,
)
I have an array of some values which I need to convert to new array and sum every value with all previous values. For example (array length, keys and values always differ), this is what I have:
Array
(
[0] => 1
[1] => 1
[2] => 5
[3] => 1
[4] => 1
[7] => 1
[8] => 3
[9] => 1
)
and this is what I need:
Array
(
[0] => 1
[1] => 2
[2] => 7
[3] => 8
[4] => 9
[7] => 10
[8] => 13
[9] => 14
)
I tried many different ways but always stuck at something or realized that I'm wrong somewhere. I have a feeling that I'm trying to reinvent a wheel here, because I think there have to be some simple function for this, but had no luck with finding solution. This is last way I tried:
$array = array( "0"=> 1, "1"=> 1, "2"=> 5, "3"=> 1, "4"=> 1, "7"=> 1, "8"=> 3, "9"=> 1 );
$this = current($array);
$next = next($array);
$end = next(end($array));
$sum = 0;
$newArray = array();
foreach ($array as $val){
if($val != $end){
$sum = ($this += $next);
array_push($newArray, $sum);
}
}
print_r($newArray);
..unfortunately wrong again. I spend lot of time finding ways how not to get where I need to be, can someone kick me into right direction, please?
Suggest you to use array_slice() & array_sum()
$array = array( "0"=>1,"1"=>1,"2"=>5,"3"=>1,"4"=>1,"7"=>1,"8"=>3,"9"=>1);
$keys = array_keys($array);
$array = array_values($array);
$newArr = array();
foreach ($array as $key=>$val) {
$newArr[] = array_sum(array_slice($array, 0, $key+1));
}
$newArr = array_combine($keys, $newArr);
print '<pre>';
print_r($newArr);
print '</pre>';
Output:
Array
(
[0] => 1
[1] => 2
[2] => 7
[3] => 8
[4] => 9
[7] => 10
[8] => 13
[9] => 14
)
Reference:
array_slice()
array_sum()
array_combine()
array_keys()
let assume your array variable is $a;
You can use simple for loop
for($i=1;$i<sizeof($a);$i++)
{
$a[$i]=$a[$i]+$a[$i-1];
}
You can just go over the array sum and add to a new array (in a simple way):
$array = array( "0"=> 1, "1"=> 1, "2"=> 5, "3"=> 1, "4"=> 1, "7"=> 1, "8"=> 3, "9"=> 1 );
var_dump($array);
$newArray = array();
$sum = 0;
foreach ($array as $a){
$sum += $a;
$newArray[]=$sum;
}
var_dump($newArray);
$sums = array_reduce($array, function (array $sums, $num) {
return array_merge($sums, [end($sums) + $num]);
}, []);
In other words, this iterates over the array (array_reduce) and in each iteration appends (array_merge) a number to a new array ($sums) which is the sum of the previous item in the array (end($sums)) and the current number. Uses PHP 5.4+ array syntax ([]).
You complicated too much. Loop trough all elements of array and add current element to sum. Then assign that sum to new array.
$array = array( "0"=> 1, "1"=> 1, "2"=> 5, "3"=> 1, "4"=> 1, "7"=> 1, "8"=> 3, "9"=> 1 );
$sum = 0;
$newArray = array();
foreach ($array as $key=>$val){
$sum += $val;
$newArray[$key]=$sum;
}
Make sure to get indexes right.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is an example array I want to split:
(1,2,3,4,5,0,6,7,0,8,9,10,11,12,0)
How do I split it in 3 arrays like this?
(1,2,3,4,5,0) (6,7,0) (8,9,10,11,12,0)
Here's the solution. (Author said he wants to split at '0' instead of duplicate values)
function split_array_by_duplicate_values($a, $duplicate = 0)
{
$c = array_intersect($a, array($duplicate));
$r = array();
$i = 0;
foreach($c as $k => $v) {
$l = ++$k - $i;
$r[] = array_slice($a, $i, $l);
$i = $k;
}
return $r;
}
$a = array(1,2,3,4,5,0,6,7,0,8,9,10,11,12,0);
print_r(split_array_by_duplicate_values($a));
returns
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 0
)
[1] => Array
(
[0] => 6
[1] => 7
[2] => 0
)
[2] => Array
(
[0] => 8
[1] => 9
[2] => 10
[3] => 11
[4] => 12
[5] => 0
)
)
Try this:
$array = array(1, 2, 3, 0, 4, 5, 6);
$array_0 = array_splice($array, 0, 4);
$array_1 = array_splice($array, 4);
I think what you're looking for is array_splice.
$array=array(1,2,3,0,4,5,6);
$newarray=array_splice($array,4);
print_r($array); //Array ( [0] => 1 [1] => 2 [2] => 3 [3]=>0);
print_r($newarray); //Array( [0]=>4 [1]=>5 [2]=>6)
If you want to split an array into two arrays in one multidimensional array, you could do
$array=array(1,2,3,0,4,5,6);
$newarray=array_chunk($array,4); //returns [[1,2,3,0],[4,5,6]]
You should use array split to split the array as you would like
<?php
$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
// note the differences in the array keys
print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>
Consider the factors of the following two numbers...
120: 2, 2, 2, 3, 5
240: 2, 2, 2, 2, 3, 5
I'm trying to identify the matching prime factor pairs, and multiply them together. I have each these listed in two arrays: $factor_list_1 and $factor_list_2
Using: $commons = array_intersect_assoc($factor_list_1, $factor_list_2);
gives: Array ( [0] => 2 [1] => 2 [2] => 2 )
Incorrect
Using: $commons = array_intersect($factor_list_1, $factor_list_2);
gives: Array ( [0] => 2 [1] => 2 [2] => 2 [3] => 3 [4] => 5 )
Correct
However, if I the numbers are 99 and 957, we get...
99: 3, 3, 11
957: 3, 11, 29
I'm trying to identify the matching prime factor pairs, and multiply them together. I have each these listed in two arrays: $factor_list_1 and $factor_list_2
Using: $commons = array_intersect_assoc($factor_list_1, $factor_list_2);
gives: Array ( [0] => 3 ) `
Incorrect
Using: $commons = array_intersect($factor_list_1, $factor_list_2);
gives: Array ( [0] => 3 [1] => 3 [2] => 11 )
Incorrect
I'm randomly generating the numbers, so any ideas on a way to do this that correctly identifies matching pairs of prime factors?
$l = array(2, 2, 2, 3, 5);
$r = array(2, 2, 2, 2, 3, 5);
$cnt_l = array_count_values($l);
$cnt_r = array_count_values($r);
$result = array();
foreach ($cnt_l as $number => $count) {
if (isset($cnt_r[$number])) {
$result = array_merge($result, array_fill(0, min($count, $cnt_r[$number]), $number));
}
}
var_dump($result);
So you:
count how many times every factor occurs
check if it's in another array
append the factor N times into the result array
Just use array_product(array_intersect()).