Two arrays to one chunked [duplicate] - php

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 5 months ago.
I have a two arrays and need to get one from them. array_merge, array_map etc. Doesn't give right result.
$array1 = [1,2,3,4,5];
$array2 = [a,b,c,d,e];
I need $array3 = [[1,a], [2,b], [3,c]]...
What is the best way to get that result?

using array_map(null, $arr1, $arr2) you can achieve the result.
http://php.net/manual/en/function.array-map.php
php > $q = array_map(null, $array1, $array2);
php > print_r($q);
Array
(
[0] => Array
(
[0] => 1
[1] => a
)
[1] => Array
(
[0] => 2
[1] => b
)
[2] => Array
(
[0] => 3
[1] => c
)
[3] => Array
(
[0] => 4
[1] => d
)
[4] => Array
(
[0] => 5
[1] => e
)
)

The for loop approach is the following:
<?php
$array1 = [1,2,3,4,5];
$array2 = ['a','b','c','d','e'];
$array3 = array();
if(count($array1) == count($array2))
{
for($i = 0; $i < count($array1); $i++)
{
$array3[] = [$array1[$i],$array2[$i]];
}
}
else
{
die("SIZE MISMATCH");
}
echo '<pre>';
print_r($array3);
And the output is:
Array
(
[0] => Array
(
[0] => 1
[1] => a
)
[1] => Array
(
[0] => 2
[1] => b
)
[2] => Array
(
[0] => 3
[1] => c
)
[3] => Array
(
[0] => 4
[1] => d
)
[4] => Array
(
[0] => 5
[1] => e
)
)

Related

Combine two array and add value of array count

I have two array like this
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
echo "<pre>";
print_r($arr1);
print_r($arr2);
And I want output like this
Array
(
[0] => Array
(
[0] =>Prabhash
[1] =>9
)
[1] => => Array
(
[0] =>Nagda
[1] =>1
)
[2] => => Array
(
[0] =>Sayyed
[1] =>2
)
)
I have tried to combine and merge array but not success, Hope someone will help me for this better.
PHP code demo
<?php
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$result=array();
foreach($arr1 as $key => $value)
{
if(isset($result[$value]))
{
$result[$value][1]+=$arr2[$key];
}
else
{
$result[$value]=array($value,$arr2[$key]);
}
}
$result= array_values($result);
print_r($result);
Output:
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)
Short solution using array_map, array_keys, array_flip, array_unique, array_intersect_key and array_sum functions:
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$result = array_map(function($n) use($arr1, $arr2){
$sum = array_sum(array_intersect_key($arr2, array_flip(array_keys($arr1, $n))));
return [$n, $sum];
}, array_unique($arr1));
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)
Try it.
<?php
$arr1 = array('Prabhash', 'Nagda', 'Sayyed','Prabhash');
$arr2 = array('4', '1', '2','5');
$newArray = array();
foreach($arr1 as $key => $value) {
$newArray[$value][0] =$value;
if(!isset($newArray[$value][1]) || $newArray[$value][1] == null)
$newArray[$value][1] = $arr2[$key];
else
$newArray[$value][1] = $newArray[$value][1]+$arr2[$key];
}
$newArray = array_values($newArray);
echo "<pre>";
print_r($newArray);
?>
OUTPUT :
Array
(
[0] => Array
(
[0] => Prabhash
[1] => 9
)
[1] => Array
(
[0] => Nagda
[1] => 1
)
[2] => Array
(
[0] => Sayyed
[1] => 2
)
)

PHP copy array without references

When I copy PHP array with reference, copy already has references from original
$arr = [1,2,3];
print_r($arr); echo"<br>";
$x = &$arr[1];
$arr2 = $arr;
print_r($arr); print_r($arr2); echo"<br>";
$x = 8;
print_r($arr); print_r($arr2); echo"<br>";
Result:
Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 8 [2] => 3 ) Array ( [0] => 1 [1] => 8 [2] => 3 )
How can I copy an array, so it has not changed with the original reference?
Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 2 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
Array ( [0] => 1 [1] => 8 [2] => 3 ) Array ( [0] => 1 [1] => 2 [2] => 3 )
If your issue is solved using the duplicate link in your question's first comment (supported by 4 others at the time I'm writing this). Please delete your question so that SO can reduce duplicate questions / needless bloat.
Otherwise, just declare a static copy of the original array for future use.
$arr = [1,2,3];
$copy=$arr; // preserve the original
print_r($arr); echo"<br>";
$x = &$arr[1];
$arr2 = $arr;
print_r($arr); print_r($arr2); echo"<br>";
$x = 8;
print_r($arr); print_r($copy); echo"<br>";

Remove duplicate values from a multi-dimensional array and sum them

suppose I have an array like this :
Array
(
[0] => Array
(
[0] => A
[1] => 20
)
[1] => Array
(
[0] => B
[1] => 10
)
[2] => Array
(
[0] => G
[1] => 5
)
[3] => Array
(
[0] => A
[1] => 15
)
)
I would like to remove duplicate values and sum just a row of array :
What I want :
Array
(
[0] => Array
(
[0] => A
[1] => 35 // <= sum : 20 + 15
)
[1] => Array
(
[0] => B
[1] => 10
)
[2] => Array
(
[0] => G
[1] => 5
)
)
I've read this question before.
updated
while($row = $stmt->fetch()){
$arr = array(
'GoodMainCode'=>persian_sql_to_php($row['GoodMainCode']), // <= like A in the example
'title'=> persian_sql_to_php($row['GoodName']),
'author'=>persian_sql_to_php($row['moalef']),
'publisher'=>persian_sql_to_php($row['Nasher']),
'translator'=>persian_sql_to_php($row['Motarjem']),
'price'=>persian_sql_to_php($row['SellPrice1']),
'isbn'=>persian_sql_to_php($row['ISBN']),
'amount'=>persian_sql_to_php($row['Amount']), // <= if GoodMainCode is same key, I must sum it.
'year_of_publish'=>persian_sql_to_php($row['SaleChap']),
'period_print'=>persian_sql_to_php($row['NobateChap'])
);
array_push($mjson,$arr);
}
//added
foreach($mjson as $v){
if(!isset($result[$v['GoodMainCode']]))
$result[$v['GoodMainCode']] = $v;
else
$result[$v['GoodMainCode']]['amount'] += $v['amount'];
}
This should work for you:
Just loop through your array and check if in your $result array is a key with the letter of the current inner Array from $arr. If not add it to the $result array and initialize the second key with the number.
If there is already a key with this letter you can simply add the numbers together in this array. At the end I simply use array_values() to reindex the entire array.
<?php
foreach($arr as $v) {
if(!isset($result[$v[0]]))
$result[$v[0]] = $v;
else
$result[$v[0]][1] += $v[1];
}
$result = array_values($result);
print_r($result);
?>
output:
Array
(
[0] => Array
(
[0] => A
[1] => 35
)
//...
[2] => Array
(
[0] => G
[1] => 5
)
)

PHP Rename all the keys in arrays [duplicate]

This question already has answers here:
PHP Change Array Keys
(12 answers)
Closed 8 years ago.
I have an array of arrays, something like this:
Array
(
[0] => Array
(
[0] => DC1F180E-FE57-622C-28AE-8194843B4D84
[1] => First Choice
[2] => 1
)
[1] => Array
(
[0] => EB877F3C-7A3B-98A7-9240-580FB797030A
[1] => Second Choice
[2] => 0
)
[2] => Array
(
[0] => D3C0EA56-73D2-C7E3-8236-EEA2400DFA9C
[1] => Third Choice
[2] => 0
)
)
How do I can rename all the keys in "nested" arrays to get something like this in all "nested" arrays:
...
[1] => Array
(
[id] => EB877F3C-7A3B-98A7-9240-580FB797030A
[name] => Second Choice
[status] => 0
)
...
This way you can change keys in your array:
for ($i=0, $c = count($array); $i<$c; ++$i) {
$array[$i]['id'] = $array[$i][0];
$array[$i]['name'] = $array[$i][1];
$array[$i]['status'] = $array[$i][2];
unset($array[$i][0];
unset($array[$i][1];
unset($array[$i][2];
}
You have to use syntax $array[$key1][$key2] to use multidimensional array.
You could use array_walk and array_combine like this:
$a = array(array('foo','bar'),array('foo','bar'));
print_r($a);
$keys = array('first','second');
$new_array = array();
array_walk($a,function($x) use (&$new_array,$keys) {
$new_array[] = array_combine($keys,$x);
});
print_r($new_array);
array_walk goes through each element of your array, applying a callback function. array_combine combines an array of keys with an array of values to produce a new array.
output:
Array ( [0] => Array ( [0] => foo [1] => bar )
[1] => Array ( [0] => foo [1] => bar ) )
Array ( [0] => Array ( [first] => foo [second] => bar )
[1] => Array ( [first] => foo [second] => bar ) )

Creating a multidimensional array

I have 2 arrays, that I want to put into 1 multidimensional array
$array_result = array();
Array1 = a,b,c,d
Array2 = 1,2,3,4
The result that I want to get is
$array_result = [0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
) etc...
I can't work out how to do this. Then length of Array1 and Array2 varies as it is dynamic data.
Can someone point me in the right direction?
Try this
$arr1 = array(1,2,3,4);
$arr2 = array('a','b','c','d');
$arr3 = array();
for($i = 0;$i< count($arr1);$i++) {
$arr = array();
$arr[] = $arr2[$i];
$arr[] = $arr1[$i];
array_push($arr3,$arr);
}
Output
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 2
)
[2] => Array
(
[0] => c
[1] => 3
)
[3] => Array
(
[0] => d
[1] => 4
)
)
Codepad Demo
Use array_merge() function. It should do what you want to do.
$array_result=array_merge($array1, $array2, ...);

Categories