arrange arrays inside arrays on basis of a value - php

i have an array of arrays like below the length of the arrays will be unknown i want to sort the arrays in ascending order on basis of the third element in every array.
Array
(
[0] => Array
(
[0] => 30
[1] => AbdulA
[2] => TEST White Truck 7 am 9:30 am 12 pm 2:30 pm
[3] => 8
[4] => 12
)
[1] => Array
(
[0] => 31
[1] => AbdulAnn
[2] => TEST White Truck 7 am 9:30 am 12 pm 2:30 pm
[3] => 2
[4] => 8
)
[2] => Array
(
[0] => 32
[1] => pacha
[2] => TEST RED TRUCK
[3] => 0
[4] => 1
)
)

You can use usort for this:
function cmp($a, $b) {
return $a[3] - $b[3];
}
usort($array, "cmp");

Related

Sort big array based by matching small array in php

I have an array-
$arraydelivered =
Array
(
[0] => Array
(
[0] => 1
[1] => pending
[2] => January
)
[1] => Array
(
[0] => 4
[1] => pending
[2] => April
)
[2] => Array
(
[0] => 7
[1] => pending
[2] => July
)
[3] => Array
(
[0] => 10
[1] => pending
[2] => October
)
)
I want to resort this array dynamically to
Array
(
[0] => Array
(
[0] => 4
[1] => pending
[2] => April
)
[1] => Array
(
[0] => 7
[1] => pending
[2] => July
)
[2] => Array
(
[0] => 10
[1] => pending
[2] => October
)
[3] => Array
(
[0] => 1
[1] => pending
[2] => January
)
)
I have tried to find out the subarray based on which I know exactly from where to re-sort
foreach ($arraydelivered as $keyD => $valueD) {
if($valueD[0] == $cycle){
print_r($valueD);
}
}
This has given me the output -
Array ( [0] => 4 [1] => pending [2] => April )
Now I want to use this sub-array as the identifier to resort to the main array. Basically this sub-array will be the resort starting point for the big array.
Here is the solve array
$keyD1=0;
if(!empty($arraydelivered)){
foreach ($arraydelivered as $keyD => $valueD) {
if($valueD[0] == $cycle){
$keyD1= $keyD;
}
}
$outputsec = array_slice($arraydelivered, $keyD1);
$outputfirst = array_slice($arraydelivered, -4, $keyD1);
$finalarray= array_merge($outputsec,$outputfirst);
$arraydelivered=$finalarray;

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
)
)

Creating multi-dimensional array from multiple distinct arrays

I suspect this question has been answered before but i have dug and dug this great forum for an answer in vain.....
I have 3 arrays that looks like this:
Array
(
[1] => 19
[2] => 2
[3] => 2018
)
Array
(
[1] => 19
[2] => 1
[3] => 2017
)
Array
(
[1] => 18
[2] => 2
[3] => 2016
)
I would like to convert this 3 arrays into a multidimensional array to look something like this:
$mynewArray = Array(
[0] =>array(
[1] => 19
[2] => 2
[3] => 2018
)
[1] =>array(
[1] => 19
[2] => 1
[3] => 2017
)
[2] => array(
[1] => 18
[2] => 2
[3] => 2016
)
)
How do i achieve this in Php?
Demo Link.
You just need to add it in parent array as below,
$arr1 = [1 => 19, 2 => 2, 3 => 2018];
$arr2 = [1 => 19, 2 => 1, 3 => 2017];
$arr3 = [1 => 18, 2 => 2, 3 => 2016];
$mynewArray = [$arr1,$arr2,$arr3];
print_r($mynewArray);
Output
Array
(
[0] => Array
(
[1] => 19
[2] => 2
[3] => 2018
)
[1] => Array
(
[1] => 19
[2] => 1
[3] => 2017
)
[2] => Array
(
[1] => 18
[2] => 2
[3] => 2016
)
)
Also you can append your child arrays to the parent by
$array1 = array("1"=>"1","2"=>"2","3"=>"3");
$array2 = array("1"=>"1","2"=>"2","3"=>"3");
$newarray = array($array1,$array2);

Multiplying 2 array multidimensional and remove key

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
)
)

Get sum of multi-dimensional array in PHP

I need to get the sum of all numeric values in my array for each designated month. Ideally, it would return the following format.
April
total = 22
March
total = 'sum'
Array
(
[April] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] =>
[4] => 3
[5] =>
[6] => 2
[7] => 6
[8] => 3
[9] => 2
)
[March] => Array
(
[0] => 3.19198
[1] => 2.52219
[2] => 3.40053
[3] => 2.42639
[4] => 3.92301
[5] => 3.23758
[6] => 3.22457
[7] => 2.62855
)
Apply array_sum() on each of the sub-arrays using array_map():
$result = array_map('array_sum', $data);
Output:
Array
(
[April] => 22
[March] => 24.5548
)
Demo

Categories