Multiplying 2 array multidimensional and remove key - php

I have an multidimensional array which contain some numbers, and i want to multiplying value of array which contain key 0 with each other key inside one area array and erase key 0.
Array
Array
(
[0] => Array
(
[0] => 3
[1] => 5
[2] => 5
[3] => 6
[4] => 7
)
[1] => Array
(
[0] => 2
[1] => 7
[2] => 4
[3] => 2
[4] => 8
)
[2] => Array
(
[0] => 4
[1] => 2
[2] => 3
[3] => 2
[4] => 5
)
)
Here's the result i want
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)
I was already combine it using foreach and for, but it still not working for me, any idea how to do this?

The solution using array_map and array_slice functions:
// $arr is your initial array
foreach ($arr as &$v) {
$multiplier = $v[0];
$v = array_map(function($val) use($multiplier){
return $multiplier * $val;
}, array_slice($v, 1));
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => 15
[1] => 15
[2] => 18
[3] => 21
)
[1] => Array
(
[0] => 14
[1] => 8
[2] => 4
[3] => 16
)
[2] => Array
(
[0] => 8
[1] => 12
[2] => 8
[3] => 20
)
)

Related

How merge and sum values by comparing any one or two values of the same array

Below is the array in php and I need function to solve n numbers of array as displayed in Expected result by summing the (2) and (3) values if (0) and (1) are matching :
Array
(
[0] => Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 1
[3] => 2
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 3
[3] => 4
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 5
[3] => 6
)
)
[1] => Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 7
[3] => 8
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 9
[3] => 10
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 11
[3] => 12
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
)
Expected Result :
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
Need help to get the above expected result for the raised query. I tried below code :
<?php
$sum = [];
foreach($weekly as $aSet) {
foreach($aSet as $k => $kv) {
$i = 0;
foreach($kv as $v) {
$sum[$k][$i] += $v;
$i++;
}
}
}
?>
But it will add all types :
[0] => Array
(
[0] => 0
[1] => 4038
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => 0
[1] => 4038
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => 0
[1] => 4038
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => 0
[1] => 4038
[2] => 13
[3] => 14
)
I want get sum only for 2 and 3rd value and need to keep constant for 0 and 1st value for exact match
Because all your nested arrays are with predefined amount of elements, all of those with the same indexes, you can use a for loop instead of the third foreach:
$sum = [];
foreach($weekly as $aSet)
{
foreach($aSet as $k => $kv)
{
$sum[$kv[0]][0] = $kv[0];
$sum[$kv[0]][1] = $kv[1];
for($i = 2; $i <= 3; $i++)
{
$sum[$kv[0]][$i] += $kv[$i];
}
}
}
echo "<pre>";
print_r($sum);
echo "</pre>";
With a for loop you can cycle inside the array considering only the needed elements of that array (in your case the indexes 0 and 1 aren't useful in this loop).
Output:
Array
(
[Week - 1] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[Week - 2] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[Week - 3] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[Week - 4] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)
If you prefer the array with numbered indexes do that:
echo "<pre>";
print_r(array_values($sum));
echo "</pre>";
Output:
Array
(
[0] => Array
(
[0] => Week - 1
[1] => 2019-08-05
[2] => 8
[3] => 10
)
[1] => Array
(
[0] => Week - 2
[1] => 2019-08-12
[2] => 12
[3] => 14
)
[2] => Array
(
[0] => Week - 3
[1] => 2019-08-19
[2] => 16
[3] => 18
)
[3] => Array
(
[0] => Week - 4
[1] => 2019-08-26
[2] => 13
[3] => 14
)
)

Array data incorrect on loop

I have a series of variables that are collected in a while next loop.
I wanted to store these in an array so that I can access them later in some javascript, so I set
$remainingTime = array();
before while (!$listing->EOF) {
After the variables are populated I then added
$remainingTime[] = array( $remainingDay, $remainingHour, $remainingMinutes, $remainingSeconds );
If I do
echo "<pre>";
print_r($remainingTime);
echo "</pre>";
after the loop is finished I get the following output
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
)
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
[1] => Array
(
[0] => 5
[1] => 15
[2] => 33
[3] => 12
)
)
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
[1] => Array
(
[0] => 5
[1] => 15
[2] => 33
[3] => 12
)
[2] => Array
(
[0] => 8
[1] => 16
[2] => 4
[3] => 33
)
)
Array
(
[0] => Array
(
[0] => 6
[1] => 14
[2] => 34
[3] => 1
)
[1] => Array
(
[0] => 5
[1] => 15
[2] => 33
[3] => 12
)
[2] => Array
(
[0] => 8
[1] => 16
[2] => 4
[3] => 33
)
[3] => Array
(
[0] => 2
[1] => 5
[2] => 38
[3] => 17
)
)
So it appears that it is building a new array each time it goes through the loop rather than just adding the next dataset to it.
Where have I gone wrong with building the array?
The assignment syntax you used ($remainingTime[]) pushes the value you assign it as a new element into the array. Try it without using the square brackets.

how we fill missing values with one array to another on the basis of index

Hello sir below is my multidimensional array that contain some missing values on specific index
$array1 = Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] =>
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] =>
)
)
and below is my second array
I want to put the array2 value of index 3 and 4 in to the $array1 index 3 and 4 .but i dnt want to replace whole array value.
I just want to replace the those value that are null in the $array1
$array2 = Array
(
[3] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[5] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
)
Required output below where i show the replace value in single qoutes )
$array1 = Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] => '9'
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] => '7'
)
This is it:
$array1 = array("2"=> array("1"=>"2", "2"=>"4", "3"=>"9"),
"3"=> array("1"=>"4", "2"=>"6", "3"=>""),
"4"=> array("1"=>"4", "2"=>"6", "3"=>"7"),
"5"=> array("1"=>"2", "2"=>"4", "3"=>"")
);
$array2 = array("3"=> array("1"=>"2", "2"=>"4", "3"=>"9"),
"5"=> array("1"=>"4", "2"=>"6", "3"=>"7")
);
foreach ($array1 as $key => $value) {
foreach ($value as $key2 => $value2) {
if($value2 == ""){
$array1[$key][$key2] = $array2[$key][$key2];
}
}
}
echo '<pre>';
print_r($array1);
echo '</pre>';
Output:
Array
(
[2] => Array
(
[1] => 2
[2] => 4
[3] => 9
)
[3] => Array
(
[1] => 4
[2] => 6
[3] => 9
)
[4] => Array
(
[1] => 4
[2] => 6
[3] => 7
)
[5] => Array
(
[1] => 2
[2] => 4
[3] => 7
)
)

PHP convert associative array into indexed array

So long story short, I have the array $total which looks like this:
Array (
[0] => Array
(
[0] => 1
[1] => 3
)
[1] => Array
(
[0] => 6
[1] => 7
[2] => 8
)
[2] => Array
(
[0] => 9
[1] => 10
)
)
However I need to pass it to a function that looks like this:
$count = cartesian(
Array("GH20"),
Array(1,3),
Array(6,7,8),
Array(9,10)
);
I have asked a similar question but I suspect the answer they gave was correct, I do not think asked the question in the right way. The cartesain function does not seem to accept:
$count = cartesian(
Array($total[0]),
Array($total[1]),
Array($total[2])
);
After some digging around I believe this is down to the fact that the arrays have keys which is not causing an error however instead of any values being returned, it just returns "array" for everything.
My question, in two parts then, is how do I remove the keys from the arrays so it is formatted as required. If possible, could the be done dynamically?
I have tried what everybody suggestes on google which is use the array_values() function, but this did not help
Any help is greatly appreciated, thanks, Nick
As requested, casertain function:
function cartesian() {
$_ = func_get_args();
if(count($_) == 0)
return array(array());
$a = array_shift($_);
$c = call_user_func_array(__FUNCTION__, $_);
$r = array();
foreach($a as $v)
foreach($c as $p)
$r[] = array_merge(array($v), $p);
return $r;
}
This is my desired output (Every permutation of ($total):
Array (
[0] => Array (
[0] => GH20
[1] => 1
[2] => 6
[3] => 9
)
[1] => Array (
[0] => GH20
[1] => 1
[2] => 6
[3] => 10
)
[2] => Array (
[0] => GH20
[1] => 1
[2] => 7
[3] => 9
)
[3] => Array (
[0] => GH20
[1] => 1
[2] => 7
[3] => 10
)
[4] => Array (
[0] => GH20
[1] => 1
[2] => 8
[3] => 9
)
[5] => Array (
[0] => GH20
[1] => 1
[2] => 8
[3] => 10
)
[6] => Array (
[0] => GH20
[1] => 3
[2] => 6
[3] => 9
)
[7] => Array (
[0] => GH20
[1] => 3
[2] => 6
[3] => 10
)
[8] => Array (
[0] => GH20
[1] => 3
[2] => 7
[3] => 9
)
[9] => Array (
[0] => GH20
[1] => 3
[2] => 7
[3] => 10
)
[10] => Array (
[0] => GH20
[1] => 3
[2] => 8
[3] => 9
)
[11] => Array (
[0] => GH20
[1] => 3
[2] => 8
[3] => 10
)
)

Split an array into mutiple arrays based on a value - PHP

What I've tried so far : I have an array as shown below, what i wanted to do is, create multiple arrays based on the value BEGIN_DAY:
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
[6] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[7] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on ...
I want to split this array into multiple different arrays when ever it begin with BEGIN_DAY so it should look like this:
The first :
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
The second :
[0] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[1] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
What I've tried so far :
$days=array();
$split_by='BEGIN_DAY';
foreach ($day_all as $key => $value) {
if($day_all[$key][0]==$split_by){
$days=array_slice($day_all, 0,$split_by);
}
}
var_dump($days);
Much Appreciated!
$sk = "first";
foreach ($array as $key=>$value)
{
if(in_array(BEGIN_DAY, $value)&&$key!=0)
{
$sk = "second";
}
$result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];
Something like this :
$yourArray = ...; //is your array as described above
$finalArray = [];
$tempArray = [];
for($idx = 0; idx<$yourArray .length; idx++){
$tempArray = $yourArray[$idx]
if($yourArray[$idx][0] == 'BEGIN_DAY'){
$finalArray[] = $tempArray;
$tempArray = [];
}
}
The final array will contain the arrays. Each time the BEGIN_DAY is found, the array will be inserted into the finalArray and a new one is created.
To improve the code, check the existence of the cell too, to avoid index exception.

Categories