PHP sort subgroup only of a multidimensional array - php

not sure I'm missing something obvious, but I can't find in the other answers a way to sort a multidimensional array subgroups, without touching the main order.
I have an array like this
Array
(
[1] => Array
(
[name] => Apples
[type] => 1
[sales] => 10
)
[2] => Array
(
[name] => Apples
[type] => 2
[sales] => 30
)
[3] => Array
(
[name] => Apples
[type] => 3
[sales] => 20
)
[4] => Array
(
[name] => Oranges
[type] => 1
[sales] => 30
)
[5] => Array
(
[name] => Oranges
[type] => 2
[sales] => 10
)
[6] => Array
(
[name] => Oranges
[type] => 3
[sales] => 20
)
[7] => Array
(
[name] => Lemons
[type] => 1
[sales] => 10
)
[8] => Array
(
[name] => Lemons
[type] => 2
[sales] => 30
)
[9] => Array
(
[name] => Lemons
[type] => 3
[sales] => 20
)
)
What I need to do is sorting each 'name' group (Apples, Oranges and Lemons) by the key 'sales', leaving the order of keys 'name' Apples, Oranges and Lemons unaltered, so result should be like this:
Array
(
[1] => Array
(
[name] => Apples
[type] => 1
[sales] => 10
)
[2] => Array
(
[name] => Apples
[type] => 3
[sales] => 20
)
[3] => Array
(
[name] => Apples
[type] => 2
[sales] => 30
)
[4] => Array
(
[name] => Oranges
[type] => 2
[sales] => 10
)
[5] => Array
(
[name] => Oranges
[type] => 3
[sales] => 20
)
[6] => Array
(
[name] => Oranges
[type] => 1
[sales] => 30
)
[7] => Array
(
[name] => Lemons
[type] => 1
[sales] => 10
)
[8] => Array
(
[name] => Lemons
[type] => 3
[sales] => 20
)
[9] => Array
(
[name] => Lemons
[type] => 2
[sales] => 30
)
)
I can't find any way to do this, anyone can give me a hint about this?
Thanks!

#gege try this:
<?php
$arr = array(
array(
"name" => "Apples",
"type" => 1,
"sales" => 10
),
array(
"name" => "Apples",
"type" => 2,
"sales" => 30
),
array(
"name" => "Apples",
"type" => 3,
"sales" => 20
),
array(
"name" => "Oranges",
"type" => 1,
"sales" => 30
),
array(
"name" => "Oranges",
"type" => 2,
"sales" => 10
),
array(
"name" => "Oranges",
"type" => 3,
"sales" => 20
),
array(
"name" => "Lemons",
"type" => 1,
"sales" => 10
),
array(
"name" => "Lemons",
"type" => 2,
"sales" => 30
),
array(
"name" => "Lemons",
"type" => 3,
"sales" => 20
)
);
echo "<pre>";
print_r($arr); // array before
function sortArr($a, $b){
if($a["name"] == $b["name"]){
if($a["sales"] == $b["sales"]){
return 0;
}
return($a["sales"] < $b["sales"] ? -1 : 1);
}
else{
return($a["name"] < $b["name"] ? -1 : 1);
}
}
usort($arr, "sortArr");
echo "<pre>";
print_r($arr); // array after

For the sake of readability, I reduced your array (it should still work for yours though).
<?php
// The array structured as assumed according to your example
$groceries = array(
array(
"name" => "Apples",
"type" => 1,
"sales" => 10
),
array(
"name" => "Apples",
"type" => 2,
"sales" => 30
),
array(
"name" => "Apples",
"type" => 3,
"sales" => 20
),
);
/**
* See the PHP docs for more information: http://php.net/manual/en/function.usort.php
*/
$sortBySales = function ($a, $b){
return $a['sales'] - $b['sales'];
};
echo "<h1>Unsorted</h1>";
echo "<pre>";
var_dump($groceries);
echo "</pre>";
usort($groceries, $sortBySales);
echo "<h1>Sorted</h1>";
echo "<pre>";
var_dump($groceries);
echo "</pre>";
EDIT
I forgot that the array should have the names grouped as well. Now, #BunkerBoy alread posted the solution, but for the sake of completness, here is the updated sorting function:
$sortBySales = function ($a, $b){
if($a["name"] == $b["name"]){
return $a['sales'] - $b['sales'];
}else{
return($a["name"] < $b["name"] ? -1 : 1);
}
};
Note that the only difference to #BunkerBoy's function is that I reduced the comparison to a calculation.

Related

How to change indexes in array with sub arrays with subarray property value in PHP

I need help. I have an array of items like this one:
[7646] => Array
(
[0] => Array
(
[id] => 156153
[tmplvarid] => 5
[value] => 2
)
[1] => Array
(
[id] => 56795
[tmplvarid] => 7
[value] => 430
)
[2] => Array
(
[id] => 56798
[tmplvarid] => 19
[value] => rate_08
)
),
[7647] => Array ()
And I need to change array indexes to value of property tmplvarid in sub array to transform array like this:
`[7646] => Array
(
[5] => Array
(
[id] => 156153
[tmplvarid] => 5
[value] => 2
)
[7] => Array
(
[id] => 56795
[tmplvarid] => 7
[value] => 430
)
[19] => Array
(
[id] => 56798
[tmplvarid] => 19
[value] => rate_08
)
)
How can I transform it in assosiative array ?
set index value from array value using foreach loop
Code
<?PHP
$arr = [
"7646" => array
(
[
"id"=> 156153,
"tmplvarid" => 5,
"value" => 2
],
[
"id"=> 56795,
"tmplvarid" => 7,
"value" => 430
],
[
"id"=> 56798,
"tmplvarid" => 19,
"value" => "rate_08"
]
)
];
echo "<pre>";
print_r($arr);
$newarr= [];
foreach($arr as $key => $value)
{
foreach($value as $key1 => $value1)
{
$newarr[$key][$value1['tmplvarid']] = $value1;
}
}
print_r($newarr);
?>
Output
Array
(
[7646] => Array(
[5] => Array
(
[id] => 156153
[tmplvarid] => 5
[value] => 2
)
[7] => Array
(
[id] => 56795
[tmplvarid] => 7
[value] => 430
)
[19] => Array
(
[id] => 56798
[tmplvarid] => 19
[value] => rate_08
)
)
)

How to convert array to Multidimensional array and push a value

I have two multiple select input result that i want to join. Its about delivery destination and delivery fee.
here is my array result :
Array
(
[destination] => Array
(
[0] => London
[1] => Liverpool
[2] => Nottingham
[3] => Oxford
)
[fee] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
)
)
And I want to push these value to each array :
$status = "1";
Result I expected is :
Array
(
[0] => Array
(
[destination] => London
[fee] => 10
[status] => 1
)
[1] => Array
(
[destination] => Liverpool
[fee] => 15
[status] => 1
)
[2] => Array
(
[destination] => Nottingham
[fee] => 20
[status] => 1
)
[3] => Array
(
[destination] => Oxford
[fee] => 25
[status] => 1
)
)
Thanks for helping me.
$array = ['destination' => ['London', 'Liverpool', 'Nottingham', 'Oxford'], 'fee' => [10, 15, 20, 25]];
$result = [];
foreach ($array['destination'] as $index => $value)
{
$result[] = ['destination' => $value, 'fee' => $array['fee'][$index], 'status' => 1];
}
Try this: https://3v4l.org/004PF
<?php
$givenArray = [
'destination' => [
'London',
'Liverpool',
'Nottingham',
'Oxford',
],
'fee' => [
10,
15,
20,
25
],
];
$output = [];
foreach ($givenArray['destination'] as $key => $destination) {
$fee = $givenArray['fee'][$key];
$output[] = [
'destination' => $destination,
'fee' => $fee,
'status' => 1,
];
}
print_r($output);
Output is:
Array
(
[0] => Array
(
[destination] => London
[fee] => 10
[status] => 1
)
[1] => Array
(
[destination] => Liverpool
[fee] => 15
[status] => 1
)
[2] => Array
(
[destination] => Nottingham
[fee] => 20
[status] => 1
)
[3] => Array
(
[destination] => Oxford
[fee] => 25
[status] => 1
)
)

Php merge two elements of an array based on some common value and add other values

I have a below array:
[product] => Array
(
[0] => Array
(
[qty] => 1
[code] => 147818
[price] => 11
[name] => Product1
)
[1] => Array
(
[qty] => 2
[code] => 147818
[price] => 11
[name] => Product1
)
[2] => Array
(
[qty] => 1
[code] => 567432
[price] => 31
[name] => Product2
)
)
I want to add quantities if the code is same. That is, I want the resulting array to be:
[product] => Array
(
[0] => Array
(
[qty] => 3
[code] => 147818
[price] => 11
[name] => Product1
)
[1] => Array
(
[qty] => 1
[code] => 567432
[price] => 31
[name] => Product2
)
)
It should merge the elements only if the code is same. How can I achieve this?
Try this code, it merge and sum the qty by code
$products = [
[
'qty' => 1,
'code' => 147818,
'price' => 11,
'name' => 'Product1'
],
[
'qty' => 2,
'code' => 147818,
'price' => 11,
'name' => 'Product1'
],
[
'qty' => 1,
'code' => 567432,
'price' => 31,
'name' => 'Product2'
],
];
$output = [];
for ($i=0; $i<count($products); $i++) {
if ($output[$products[$i]['code']]['code'] == $products[$i]['code']) {
$output[$products[$i]['code']]['qty'] += $products[$i]['qty'];
}else{
$output[$products[$i]['code']] = $products[$i];
}
}
$output = array_values($output);
print_r($output);

How to compare two different multidimensional array?

I have two array as below
Array 1
Array
(
[0] => Array
(
[ps_id] => 5
[product_id] => 2
[supplier_id] => 25
[cost] => 789.00
[name] => Mahesh
)
[1] => Array
(
[ps_id] => 6
[product_id] => 2
[supplier_id] => 2
[cost] => 12345.00
[name] => mayank
)
[2] => Array
(
[ps_id] => 7
[product_id] => 2
[supplier_id] => 1
[cost] => 123456.00
[name] => abc
)
[3] => Array
(
[ps_id] => 10
[product_id] => 2
[supplier_id] => 8
[cost] => 12000.00
[name] => mayank1
)
)
Array 2
Array
(
[0] => Array
(
[suppliers] => Mahesh
[suppliers_cost] => 789.00
)
[1] => Array
(
[suppliers] => mayank
[suppliers_cost] => 12345.00
)
[2] => Array
(
[suppliers] => mayank1
[suppliers_cost] => 12000.00
)
[3] => Array
(
[suppliers] => testtetstet
[suppliers_cost] => 123123
)
)
I want to compare above array by their suppliers and name key,
Means if this both key have same values than it will store into one new array and if those key are not match then they will store new different array.
Or might be it possible that both array will have different number of keys
I had tried like below
foreach ($existsProductSupplier as $key => $value) {
if (isset($supplier_data[$key])) {
}else{
$supplier_data[$key]['suppliers']='';
$supplier_data[$key]['suppliers_cost']='';
}
}
foreach ($supplier_data as $key => $value) {
if(in_array($value['suppliers_cost'],$existsProductSupplier[$key])){
//echo "string";
// print_r($value);
}else{
echo "string";
//print_r($value);
}
}
try this code it will help you
<?php
$arr1=Array
(
0 => Array
(
"ps_id" => 5,
"product_id" => 2,
"supplier_id" => 25,
"cost" => 789.00,
"name" => "Mahesh"
),
1 => Array
(
"ps_id" => 6,
"product_id" => 2,
"supplier_id" => 2,
"cost" => 12345.00,
"name" => "mayank"
),
2 => Array
(
"ps_id" => 7,
"product_id" => 2,
"supplier_id" => 1,
"cost" => 123456.00,
"name" => "abc"
),
3 => Array
(
"ps_id" => 10,
"product_id" => 2,
"supplier_id" => 8,
"cost" => 12000.00,
"name" => "mayank1"
),
4 => Array
(
"ps_id" => 10,
"product_id" => 2,
"supplier_id" => 8,
"cost" => 12000.00,
"name" => "mayank2"
)
);
$arr2=Array
(
0 => Array
(
"suppliers" => "Mahesh",
"suppliers_cost" => 789.00
),
1 => Array
(
"suppliers" => "mayank",
"suppliers_cost" => 12345.00
),
2 => Array
(
"suppliers" => "mayank1",
"suppliers_cost" => 12000.00
),
3 => Array
(
"suppliers" => "testtetstet",
"suppliers_cost" => 123123
)
);
foreach($arr1 as $key=>$value){
if(isset($arr2[$key])){
if($value['name']==$arr2[$key]['suppliers']){
$arrnew1[]=$value['name'];
}else{
$arrnew2[]=$value['name'];
}
}else{
$arrnew2[]=$value['name'];
}
}
print_r($arrnew1);
print_r($arrnew2);

how to make an all arrays (including associative array) to single level of array using php?

Hope you can help me with this. Because I'm trying to reorder them but i need first to make the be at a single level of array. From associative array to single array.
$MY_ASSOC_ARRAY
Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)
[1] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)
)
)
[1] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4
)
)
)
)
)
)
Alright now the array should not be in that multi level (associative) array instead it will look like this:
Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
)
[1] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
)
[2] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
)
[3] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)
[4] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)
[5] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4
)
)
I'm no sure how will that be possible in the most effecient way without using too much memory that will affect the speed with the use of Php Codeigniter. Thanks!
[UPDATE # 1]
here are the code that I have tried but the order is different
foreach($tree as $key => $value) {
$single[] = $value;
}
And this is the output for this failed attemp...
Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
)
[1] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
)
[2] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
)
[3] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4
)
[4] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)
[5] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)
)
The [NAME] => companion should be at the last array not on 4th ([3] => Array)
UPDATE # 2:
Feel bad about the down votes... if this question or problem is not useful on your end
<?php
$array = Array(
0 => Array
(
'MAIN_ID' => 1,
'ORDER' => 1,
'NAME' => 'Animal',
'PARENT_ID' => 0,
'childs' => Array
(
0 => Array
(
'MAIN_ID' => 4,
'ORDER' => 4,
'NAME' => 'doggie',
'PARENT_ID' => 1,
'childs' => Array
(
0 => Array
(
'MAIN_ID' => 18,
'ORDER' => 18,
'NAME' => 'hunting',
'PARENT_ID' => 4,
'childs' => Array
(
0 => Array
(
'MAIN_ID' => 21,
'ORDER' => 21,
'NAME' => 'setter',
'PARENT_ID' => 18,
),
1 => Array
(
'MAIN_ID' => 22,
'ORDER' => 22,
'NAME' => 'pointer',
'PARENT_ID' => 18,
)
)
),
1 => Array
(
'MAIN_ID' => 19,
'ORDER' => 19,
'NAME' => 'companion',
'PARENT_ID' => 4,
)
)
)
)
)
);
$out = [];
$out = generateArray($array, $out);
print_r($out);
function generateArray($in, $out){
foreach($in as $value){
$childs = false;
if(isset($value['childs'])){
$childs = $value['childs'];
unset($value['childs']);
}
$out[] = $value;
if($childs)
$out = generateArray($childs, $out);
}
return $out;
}
?>

Categories