iam trying to sort this array by array[key]['premium']['Monthly'] and if there are two Monthly prices the same, then sort by quarterly, then semi-annual, then annual.
i search and couldnt figure out how to use array_multisort function.
Array (
[0] => Array (
[product_id] => 1
[rate] => 27.07
[premium] => Array (
[Annual] => 436.05
[Semi-Annual] => 226.75
[Quarterly] => 115.6
[Monthly] => 37.11
)
)
[1] => Array (
[product_id] => 2
[rate] => 35.00
[premium] => Array (
[Annual] => 565
[Semi-Annual] => 293.8
[Quarterly] => 149.73
[Monthly] => 50.85
)
)
[2] => Array (
[product_id] => 3
[rate] => 30.52
[premium] => Array (
[Annual] => 497.8
[Monthly] => 47.29
)
)
)
I think you want to use usort function, something like
function compare($a, $b){
$p1 = $a["premium"];
$p2 = $b["premium"];
if($p1["Monthly"] == $p2["Monthly"]){
// compare by quarterly
...
}else{
if($p1["Monthly"] < $p2["Monthly"])then return -1;
else return 1;
}
}
usort($prices, "compare");
where $prices is your array. The comparision function isn't implemented fully, just to show the idea. Also, since it looks like there might be missing items in the price array (ie last one misses Quarterly and Semi-Annual) you have to check first (before comparison) does the items exists and take appropriate action in case one or both are missing.
Related
I'm trying very simply to use in_array() to check a key is in an array and then echo its value.
$array = Array
(
[cart_item] => Array
(
[0] => Array
(
[product_name] => White Sakura Necktie
[id] => 11
[product_auto_id] => 556729685
[quantity] => 2
[product_regular_price] => 95
[product_sale_price] => 95
[product_image] => 556729680Black_Sakura_Necktie.jpg
)
[1] => Array
(
[product_name] => hhhad ba bhdbh
[id] => 10
[product_auto_id] => 951790801
[quantity] => 2
[product_regular_price] => 20
[product_sale_price] =>
[product_image] => 951790801hhhad_ba_bhdbh_.jpg
)
)
)
And I have value 556729685 which I want to check that this value exists or not? So I am using in_array() function for this.
in_array(556729685, array_keys($array));
in_array(556729685, array_values($array));
in_array(556729685, $array);
All above three i have used but result always showing NULL means blank.
I am really frustrated to find the solution. I don't understand what's happening.
You should use array_column() which will return the values from a single column in the input array as an array.
$product_auto_ids = array_column($array['cart_item'], 'product_auto_id');
In this case, it would return the following:
Array
(
[0] => 556729685
[1] => 951790801
)
Then you can use in_array() like you currently are.
in_array(556729685, $product_auto_ids);
I have an array of arrays that contains a date string.
I'd like to sort these arrays by this date.
What seems to be the monkeywrench in this is that some of the arrays share the same value for the date field as well as similar values for tid and/or thing and/or other_thing.
Array (
[0] => Array (
[tid] => 44
[date] => 1442905200
[thing] => 2J5265B
[other_thing] => Scoop
)
[1] => Array (
[tid] => 47
[date] => 1442905200
[thing] => 2J5265B
[other_thing] => Scoop
)
[2] => Array (
[tid] => 48
[date] => 1430031600
[thing] => 2E5116A
[other_thing] => shower
)
[3] => Array (
[tid] => 46
[date] => 1430031600
[thing] => 2E5116A
[other_thing] => shower
)
[4] => Array (
[tid] => 80
[date] => 1464246000
[thing] => 7J6147A
[other_thing] => shower
)
[5] => Array (
[tid] => 47
[date] => 1442905200
[thing] => 2J5265B
[other_thing] => TTT
)
[6] => Array (
[tid] => 44
[date] => 1442905200
[thing] => 2J5265B
[other_thing] => TTT
)
[7] => Array (
[tid] => 46
[date] => 1504594800
[thing] => 2J7248A
[other_thing] => shower
)
[8] => Array (
[tid] => 45
[date] => 1513238400
[thing] => 2J7348C
[other_thing] => TTT
)
)
That's what I'd like to do.
I'd like to sort this array.
You should consider using usort() (Docs). This function allows you to specify a comparator to have a user-defined sorting algorithm.
The resulting code might look like this:
function cmp($a, $b)
{
return $b['date'] - $a['date'];
}
usort($your_array, "cmp");
One fast forward solution would be to use the date as a key of the array while the array is being build and then you can simply sort keys using PHP ksort().
To avoid keys duplicity check if the key is set to handle such situations.
// building the data array from database or so
$array = array(); // the array to be sorted
$duplicity = array(); // track the duplicity date records
foreach ($data as $key => $value) {
$counter = 0;
#$duplicity[$value['date']]++; // suppressing notices
$array[$value['date'].'_'.$duplicity[$value['date']]] = $value;
}
ksort($array); // sort array by keys
print_r($array); // just check the sorted array
Demo: https://eval.in/978160
More sorting functions Sorting arrays
I have an array called $quotes and when I print_r an example of the results are like this:
Array ( [id] => advshipper [methods] => Array (
[0] => Array ( [id] => 2-0-0 [title] => Small Parcels [cost] => 4.5 [icon] => [shipping_ts] => [quote_i] => 0 )
[1] => Array ( [id] => 3-0-0 [title] => Large Parcels up to 1150mm long [cost] => 8.95 [icon] => [shipping_ts] => [quote_i] => 1 )
[2] => Array ( [id] => 4-0-0 [title] => Extra Large Parcels over 1150mm long [cost] => 15 [icon] => [shipping_ts] => [quote_i] => 2 ) ) [module] => Shipping )
What I need is a simple way to look at $quotes, find which [cost] has the lowest value and then record, in this example, the [0] so that they can be used in another code segment.
Short of exploding the array and then looping through all the content, is there a simple method to achieve what I want?
As you are using the version higher than 5.5, you can simply use array_column function:
echo min(array_column($quotes['id'],'cost'));
And if you want, you can retrieve the id of the row as well:
echo min(array_column($quotes['id'], 'cost', 'id'));
array_reduce($quotes, function($minimum, $current) {
return $current['cost'] < $minimum['cost'] ? $current : $minimum;
}, $quotes[0]);
This will return the row of $quotes that has the lowest value of cost.
If I understood that correctly, you need to find the lowest value for cost array.
You can do an array_map to get the values
$lowestcost = array_map(function($costval) {
return $costval['cost'];
}, $array);
Then you need to use
min($lowestcost)
Hope this helps
Given the following arrays how can I elegantly validate that option, price and cost arrays have matching key values?
Array
(
[option] => Array
(
[1] => C
[2] => M
[3] => G
)
[price] => Array
(
[1] => 100
[2] => 200
[3] => 300
)
[cost] => Array
(
[1] => 0
[2] => 0
[3] => 0
)
)
I thought of running a foreach(array as key => values) on each array and sending those values to another array, and then using if(!in_array), but theres got to be a better way to do it.
It sounds like you want the same keys as there is no correlation with the values in the array. If so, you can run a diff on the keys of each sub-array:
if(call_user_func_array('array_diff_key', $array)) {
// not the same keys
} else {
// same keys
}
call_user_func_array() takes the array as an array of arguments and passes each to array_diff_key()
If the result is not empty then there are differences
If the result is empty then there are no differences
I recommend using an array in this way:
Array
(
[option] => Array
(
[C] => Array
(
[price] => 100
[cost] => 0
)
[M] => Array
(
[price] => 200
[cost] => 0
)
[G] => Array
(
[price] => 300
[cost] => 0
)
)
)
PHP Code:
$product = array("option" => array("C" => array("price" => 100, "cost" => 0), "M" => array("price" => 200, "cost" => 0), "G" => array("price" => 300, "cost" => 0)));
This code builds an array:
$size = sizeof($include_quotes);
for ($i=0; $i<$size; $i++) {
$quotes = $GLOBALS[$include_quotes[$i]]->quote($method);
if (is_array($quotes)) $quotes_array[] = $quotes;
}
}
If i
print_r($quotes_array);
i get the following:
Array ( [0] => Array ( [id] => advshipper [methods] => Array ( [0] => Array ( [id] => 1-0-0 [title] => Trade Shipping [cost] => 20 [icon] => [shipping_ts] => [quote_i] => 0 ) [1] => Array ( [id] => 2-0-0 [title] => 1-2 working days [cost] => 3.2916666666667 [icon] => [shipping_ts] => [quote_i] => 1 ) [2] => Array ( [id] => 4-0-0 [title] => 2-3 working days [cost] => 2.4916666666667 [icon] => [shipping_ts] => [quote_i] => 2 ) [3] => Array ( [id] => 8-0-0 [title] => Click & Collect [cost] => 0 [icon] => [shipping_ts] => [quote_i] => 3 ) ) [module] => Shipping [tax] => 20 ) )
In some circumstances, I only want the data in field 0 to be passed onto the next part of the code. However, using
$sliced_quotes_array = array_slice($quotes_array,0,1);
Still returns all the results.
What is the correct method to get just:
Array ( [0] => Array ( [id] => advshipper [methods] => Array ( [0] => Array ( [id] => 1-0-0 [title] => Trade Shipping [cost] => 20 [icon] => [shipping_ts] => [quote_i] => 0 )
Any help greatly appreciated because i have tried numerous different ways and no luck yet.
Using the following still returns the same results
$testarray = array(0 => $quotes_array[0]);
print_r($testarray);
Why not just use the array constructor and explicitly include what you need:
array(0 => $quotes_array[0]);
Here is your array:
When you are saying " I only want the data in field 0 to be passed onto the next part of the code", you meant that you only want this data to be passed next, right? :
Array (
[0] => Array (
[id] => advshipper
[module] => Shipping
[tax] => 20
)
)
Is this what you want?
$newArray = array();
foreach ($quotes_array[0] as $items)
{
if (!is_array($items))
{
$newArray[] = $items;
}
}
$newArray will contain that data.
UPDATE
Okay, gotcha.
You can just use this:
$newArray = $quotes_array[0]['methods'][0];
Having done some reading on arrays and after a bit of trial, i found a solution to my problem.
I used:
unset($quotes_array[0]['methods'][1]);
By changing the index number after methods i was able to drop any shipping options i didn't require, whilst still maintaining the functionality.