This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Merge 2 Arrays and Sum the Values (Numeric Keys)
I have two arrays both have same keys, i want to add key values,
Example
$arr1 = array("first"=>10,"second"=>20);
$arr2 = array("first"=>20,"second"=>30);
want to create new array which add these two array values
like
$arr3 = array("first"=>30,"second"=>50);
How can i do that in one line, i can do it using forach or other technique, but wondering if some one know a good way of doing?
Example for string keys (note that no error checking is done!):
$arr1 = array("first"=>10,"second"=>20);
$arr2 = array("first"=>20,"second"=>30);
$arr3 = $arr1;
array_walk($arr3, function (&$val, $key, $foo) { $val += $foo[$key]; }, $arr2);
print_r($arr3);
/*Array
(
[first] => 30
[second] => 50
)*/
Related
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
Closed 8 months ago.
I have an array as shown bottom
array (
[det1] => 1,2,3,4
[det2] => 5,6
);
So i want to join items of this array and convert array to an string like bottom
$uru = 1,2,3,4,5,6
How can i do this work?
Did you try like this with implode()
<?php
$arr = array('det1'=>'1,2,3,4', 'det2'=>'5,6');
$uru = implode(',',$arr);
echo $uru;
?>
DEMO: https://3v4l.org/UBQrv
You say multidimensional array, but your array is single dimensional with strings?
I assume that is a typo and your array is multidimensional.
In that case loop the array and merge the new array with the subarray.
$arr = array (
"det1" => [1,2,3,4],
"det2" => [5,6]
);
$new= [];
foreach($arr as $sub){
$new = array_merge($new, $sub);
}
echo implode(",",$new); // 1,2,3,4,5,6
https://3v4l.org/NaDXN
This can be easily done by extracting into array_merge and then joining the resulting array.
$data = [
[1,2,3,4,5],
[6,7,8],
];
echo join(',', array_merge(... $data));
Output:
1,2,3,4,5,6,7,8
This question already has answers here:
Combine two arrays
(11 answers)
Closed 6 months ago.
i have two arrays in php
Array
(
[0]=>30
)
Array
(
[0]->43
)
i want to merge these arrays in a single array my desired output is
Array
(
[0]=>30
[1]=>43
)
can anyone tell me how to achieve this
function get_minutes($sess_time)
{
# code...
if (strstr($sess_time, ':'))
{
$separatedData = split(':', $sess_time);
$minutesInHours = $separatedData[0] * 60;
$minutesInDecimals = $separatedData[1];
$totalMinutes = $minutesInHours + $minutesInDecimals;
}
else
{
$totalMinutes = $sess_time * 60;
}
if ($totalMinutes<=60)
{
# code...
return $totalMinutes;
}
else
{
$result=$totalMinutes/60;
$y=explode(".",$result);
$hours=$y[0];
$hours_mins=$hours*60;
$remaining_mins=$totalMinutes-$hours_mins;
$remaining_array=array($remaining_mins);
print_r($remaining_array);
}
}
when i print the remaining array the ourput is two arrays
Try to use array_merge function.
array_merge — Merge one or more arrays
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
Output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
Reference: http://www.w3schools.com/php/func_array_merge.asp
This question already has answers here:
How to reindex an array?
(6 answers)
Closed 6 years ago.
array(
[0]=>1
[1]=>2
[2]=>3
[3]=>4
)
If delete second element, you have
array(
[0]=>1
[2]=>3
[3]=>4
)
Now how to change indexes to
array(
[0]=>1
[1]=>3
[2]=>4
)
If you want to re-index your array starting to zero, simply do the following:
$myNewArray = array_values($myOldArry);
From the php docs:
http://php.net/manual/en/function.array-splice.php
array_splice: Removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied.
So, you would use:
$arr = array(1,2,3,4);
array_splice($arr, 2, 1); //Will give you an array: [1, 2, 4]
This question already has answers here:
PHP - Merge two arrays (same-length) into one associative?
(4 answers)
Closed 8 years ago.
I have two array like these.
array("a","b","c");
array("1","2","3");
I have to add those items like that way.
$c=array("a"=>"1","b"=>"2","c"=>"3");
how can i retrieve each inner array by this way.
echo $c[0];
Use array_combine()
Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.
<?php
$a = array("a","b","c");
$b = array("1","2","3");
$c = array_combine($a, $b);
print_r($c);
?>
The above example will output:
Array
(
[a] => 1
[b] => 2
[c] => 3
)
$new_array = $array;
array_unshift($new_array, $value1);
array_push($new_array, $value2);
Use array_merge method for combining two arrays . Array_merge($arr1'$arr2);
This question already has answers here:
PHP reindex array? [duplicate]
(4 answers)
Closed 9 years ago.
I have got an array
$array = array(
0=>'c'
1=>'a',
2=>'b',
3=>'d',
);
Now, i am going to unset the 1 and 2 keys.
unset($array[1]); unset($array[2]);
How to do if i want an array
array(0=>'c', 1=>'d');
Try This:
$array = array_values($array);
Try
unset($array[1]);
unset($array[2]);
$array = array_values($array);
This works because array_values returns the array you gave, but with increasing keys starting at 0.
Have you tired to use something like that ?
$myarray [0] a->1
[1] a-7 b->3
[3] a-8 b->6
[4] a-3 b->2
array_values
$myarray = array_values($myarray);
btw here is a good answer posted