Sort associative array by level 4 in php - php

Here is my sample array, I would like to sort this array based on ShipPrice value which is comes on level 4 of this array, how can we do this in PHP ?
Array
(
[0] => Array
(
[ShippingTypes] =>
[SelectedShipName] =>
[SelectedShipId] =>
[SelectedShipPrice] =>
[MethodName] => UPS
[MethodId] => 6
[SelectedDeliveryDays] =>
[PackageCount] => 1
)
[1] => Array
(
[ShippingTypes] => Array
(
[44] => Array
(
[ShipName] => Local pick up
[ShipPrice] => 12
[DeliveryDays] =>
)
)
[SelectedShipName] => Local pick up
[SelectedShipPrice] => 12
[SelectedShipId] => 44
[MethodName] => Quantity Based
[MethodId] => 3
[SelectedDeliveryDays] =>
)
[2] => Array
(
[ShippingTypes] => Array
(
[1] => Array
(
[ShipName] => KP Office
[ShipPrice] => 10
[DeliveryDays] => 0
)
[2] => Array
(
[ShipName] => Sachet
[ShipPrice] => 15
[DeliveryDays] => 5
)
)
[SelectedShipName] => KP Office
[SelectedShipPrice] => 10
[SelectedShipId] => 1
[MethodName] => Local Pickup From Store
[MethodId] => -1
[SelectedDeliveryDays] => 0
)
)

You can do it using usort and the max ShipPrice you get for all shipping types (supposing that you actually want to check against the max):
usort($results, static function(array $a, array $b) {
return price($a) <=> price($b);
});
function price(array $array): int
{
$shippingTypes = (array) $array['ShippingTypes'];
if (empty($shippingTypes)) {
return 0;
}
return max(array_column($shippingTypes, 'ShipPrice'));
}

Related

PHP Compare Arrays and clear all key if one in a array is empty

I'm looking for a solution to compare multiplay arrays with each of them i want to unset in all arrays if one key is empty. So as a example if [keywords] is empty i want to unset in all arrays [keywords]. Here are my arrays that i print_r.
Array
(
[0] => Array
(
[id] => 1
[pid] => 3
[sorting] => 128
[tstamp] => 1503039725
[title] => test
[alias] => test-3
[author] => 1
[inColumn] => main
[keywords] =>
[showTeaser] =>
[teaserCssID] =>
[teaser] =>
[printable] =>
[customTpl] =>
[protected] =>
[groups] =>
[guests] =>
[cssID] =>
[space] =>
[published] => 1
[start] =>
[stop] =>
)
[1] => Array
(
[id] => 2
[pid] => 3
[sorting] => 256
[tstamp] => 1503045056
[title] => test 2
[alias] => test-2
[author] => 1
[inColumn] => main
[keywords] =>
[showTeaser] =>
[teaserCssID] => a:2:{i:0;s:0:"";i:1;s:0:"";}
[teaser] =>
[printable] =>
[customTpl] =>
[protected] =>
[groups] =>
[guests] =>
[cssID] => a:2:{i:0;s:0:"";i:1;s:0:"";}
[space] => a:2:{i:0;s:0:"";i:1;s:0:"";}
[published] => 1
[start] =>
[stop] =>
)
)
What i have tried so far is
print_r($arrResult);
foreach($arrResult as $Result)
{
foreach ($Result as $arrKey => $arrField)
{
if(!empty($arrField))
{
$arrAllowedField[$arrKey] = $arrKey;
}
}
}
That build a array that contains the key which has values. But the problem is, that it also add empty fields of a other array.
Thanks
// remove empty entries in each array
$ar = array_map('array_filter', $ar);
// find keys having not empty value at least in one array
$temp = array_intersect_key(...$ar);
// save only keys from temp array
foreach($ar as &$item) {
$item = array_intersect_key($item, $temp);
}
demo
It removes the all the empty values from the array
$arrResult = array_map('array_filter', $arrResult);
$arrResult = array_filter( $arrResult );
echo "<pre>";
print_r($arrResult);
Output
Array
(
[0] => Array
(
[id] => 1
[pid] => 3
[sorting] => 128
[tstamp] => 1503039725
[title] => test
[alias] => test-3
[author] => 1
[inColumn] => main
[published] => 1
)
)

Merge 2 array same key

I'd like to merge two arrays on same key. I've tried my best to do it but unable to get success. I've added both the Arrays below. Kindly check both the Arrays in detail and help me how to merge it using same key.
Here's the 1st array :
Array
(
[1] => Array
(
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[costprice3] => 700
[margin3] => 25
)
)
Here's the 2 array :
Array
(
[1] => Array
(
[entityType1] => Products1
)
[2] => Array
(
[entityType2] => Products2
)
[3] => Array
(
[entityType3] => Products3
)
)
i want to need like that array please suggestion me
Array
(
[1] => Array
(
[entityType1] => Products1
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[entityType2] => Products2
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[entityType3] => Products3
[costprice3] => 700
[margin3] => 25
)
)
please help me how to merge two array
Try this :
foreach($array1 as $key => $value) {
$array1[$key]['entityType'.$key] = $array2[$key]['entityType'.$key];
}
print_r($array1);
<?php
$array1 = [
1 => [
'costprice1' => 500,
'margin1' => 20
],
2 => [
'costprice2' => 600,
'margin2' => 15
],
3 => [
'costprice2' => 700,
'margin2' => 25
],
];
$array2 = [
1 => ['entityType1' => 'Products1'],
2 => ['entityType2' => 'Products2'],
3 => ['entityType3' => 'Products3'],
];
array_walk($array2, function(&$v, $k)use($array1){
$v = array_merge($v, $array1[$k]);
});
print_r($array2);
Output:
Array
(
[1] => Array
(
[entityType1] => Products1
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[entityType2] => Products2
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[entityType3] => Products3
[costprice2] => 700
[margin2] => 25
)
)
https://eval.in/618561

Filter a multidimensional array - filter_array (PHP)

How do you filter this multidimensional array with array_filter() based on [channel]?
Array
(
[0] => Array
(
[268a9d2d25fc2b9765c7cd7b8a768d3e] => Array
(
[dj_name] => Emilian
[show_name] => TechnoShow
[channel] => techno
[show_image] => http://www.digitalark.ro/dieselfm/wp-content/uploads/2016/01/avatar.jpg
[time] => 0
[time_end] => 1
[sun1] => 1
[sun2] => 1
[sun3] => 1
[sun4] => 1
[sun5] => 1
)
)
[1] => Array
(
[e13268de7c56db42f8aeab2ab4c607f2] => Array
(
[dj_name] => John Doe
[show_name] => John Doe`s Trance Show
[channel] => trance
[show_image] => http://www.digitalark.ro/dieselfm/wp-content/uploads/2016/01/dummy.jpg
[time] => 11
[time_end] => 11
[mon1] => 1
[mon2] => 1
[tue2] => 1
[mon3] => 1
[fri3] => 1
[mon4] => 1
[mon5] => 1
)
)
)
Results should have only the arrays that have the value "techno", for example:
Array
(
[0] => Array
(
[268a9d2d25fc2b9765c7cd7b8a768d3e] => Array
(
[dj_name] => Emilian
[show_name] => TechnoShow
[channel] => techno
[show_image] => http://www.digitalark.ro/dieselfm/wp-content/uploads/2016/01/avatar.jpg
[time] => 0
[time_end] => 1
[sun1] => 1
[sun2] => 1
[sun3] => 1
[sun4] => 1
[sun5] => 1
)
))
I have tried using:
$data = array_filter($dataraw, function($fs) use ($genre) {return $fs['channel'] == $genre});
EDIT:
$djs = get_posts($args);
foreach ($djs as $dj) {
$temp = maybe_unserialize(get_post_meta($dj->ID, 'show_data',true));
if ($temp) $show_data[] = maybe_unserialize(get_post_meta($dj->ID, 'show_data',true));
}
$datax = array_filter($show_data, function($fs) use ($genre) {
return array_values($fs)[0]['channel'] == $genre;
});
print_r($datax);
I'll assume the parse error in your code was a copy/paste mistake. The problem is that the element passed into the function is a deeper array. Try:
return current($fs)['channel'] === $genre;
Also you might want to use === so the results are as expected.

How to Add a new Key and Merge the array values in multidimemsional array PHP

here's the result of my first function:
Array
(
[0] => Array
(
[MaidID] => 13
[Stores] => Array
(
[0] => 14
[1] => 5
)
)
[1] => Array
(
[MaidID] => 3
[Stores] => Array
(
[0] => 4
)
)
[2] => Array
(
[MaidID] => 41
[Stores] => Array
(
[0] => 14
)
)
)
Then, here's the result of my second function:
Array
(
[1] => Array
(
[MaidID] => 14
[Cash] => 10000
[Debit] => 0
[Credit] => 0
)
)
and here's should be the result:
Array ([0] => Array (
[MaidID] => 14
[Cash] => 10000.00
[Debit] => 0
[Credit] => 0
[MaidsID] => Array(
[0] => 13
[1] => 41
)
)
)
Is it possible to make it?I need to a new key name MaidsID pointing to the list of MaidID owned by more than one Stores.Please help me, Please be patience in my question, im just a beginner.Thank you so much.
this code work ok. $a is your first array $b is the second, $c is result
$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);`
I tested it here and it was ok. (Just replace $a with your first array and $b with seccond ).
Are you sure that the structure of your arrays is exactly as you have written above?? Maybe there is any problem with that.
You have puted array inside another array? (its not needed i think)
Howerver: For this code:
`$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
print_r($a);
echo "<br><br>================================================<br><br>";
print_r($b);
echo "<br><br>================================================<br><br>";
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);
print_r($c);
echo "<br><br>================================================<br><br>";`
The output is:
Array ( [0] => Array ( [Maid] => 1 [Stores] => Array ( [0] => 1 [1] => 5 ) ) [1] => Array ( [Maid] => 3 [Stores] => Array ( [0] => 4 ) ) [2] => Array ( [Maid] => 4 [Stores] => Array ( [0] => 1 ) ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) [MaidsID] => Array ( [0] => 1 [1] => 3 [2] => 4 ) )
================================================
Isn't this how you want the result?
Have a look at this. Maybe this can help you.
$result = array_merge($array1, $array2);

How do i merge the arrays in a particular format?

I have following arrays:
1) for total placed
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalplaced] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalplaced] => 1
)
)
)
2) for total working
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalworking] => 4
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalworking] => 1
)
)
)
3) for total trained
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totaltrained] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totaltrained] => 1
)
)
)
I wanted to merge these arrays so that the resultant array should look like this
[newarray] => Array(
[0] => Array (
[centers] => Array
(
[name] => delhi
[id] => 1
[totalplaced] => 8
[totalworking] => 4
[totaltrained] => 8
)
)
[1]=> Array(
[centers] => Array
(
[name] => mumbai
[id] => 2
[totalplaced] => 1
[totalworking] => 1
[totaltrained] => 1
)
)
)
This is the tabular representation of the above data which i want to display
centername totalplaced totalworking totaltrained
delhi 8 4 8
mumbai 1 1 1
Please help me on this.
Thanks
Pankaj Khurana
The difficulty here is that PHP's functions such as array_merge() and array_merge_recursive() will not merge data into numeric keys, but rather will re-key any duplicate numeric key. So for example given two arrays:
array(
'test' => 'abc',
0 => 'xyz'
);
array(
'test' => 'def',
0 => 'uvw'
);
Merging them together with array_merge() will produce an array like:
array(
'test' => 'def',
0 => 'xyz',
1 => 'uvw'
);
So, you need a custom function to be "additive" on any key, regardless of whether it is a string or numeric key. Try this:
function mixed_key_array_merge() {
$args = func_get_args();
$result = array();
foreach ($args as $arg) {
// discard non-array arguments; maybe this could be better handled
if (!is_array($arg)) {
continue;
}
foreach ($arg as $key => $value) {
if (!isset($result[$key])) {
$result[$key] = $value;
} else if (is_array($result[$key])) {
$result[$key] = call_user_func_array('mixed_key_array_merge',array($result[$key],$value));
}
}
}
return $result;
}

Categories