I have an array with a structure like this
$product_array sample=
[0] => Array
(
[title] => M-SPA Super Camaro B-150 6-Person Inflatable Bubble Spa
[image] => http://ecx.images-amazon.com/images/I/41F6FuitoYL._SL160_.jpg
[link] => http://www.amazon.com/stuff
[price] => 960.01
[merchant] => amazon
[discount] => 16
[brand] => M-SPA
)
[1] => Array
(
[title] => M Spa Model B-130 Camaro Hot Tub, 71 by 71 by 28-Inch, Gray
[image] => http://ecx.images-amazon.com/images/I/41zQwjaPqOL._SL160_.jpg
[link] => http://www.amazon.com/stuff
[price] => 695.01
[merchant] => amazon
[discount] =>
[brand] => M-SPA
)
[2] => Array
(
[title] => M-SPA B-121 4-Person Inflatable Bubble Spa, Castello
[image] => http://ecx.images-amazon.com/images/I/41gnYSSVD5L._SL160_.jpg
[link] => http://www.amazon.com/stuff
[price] => 1,016.25
[merchant] => amazon
[discount] =>
[brand] => M-SPA
)
I cannot order this array with the price lowest to highest..
This code is not working
function order_by_price($a, $b) {
if ($a['price'] == $b['price']) {
return 0;
}
return ($a['price'] < $b['price']) ? -1 : 1;
}
usort($product_array,order_by_price);
What am I missing
Thanks
You have a non-well formed numeric value in the last array element ... [price] => 1,016.25 .... It's a "money format" and should be presented as a string: [price] => "1,016.25".
Also, there's a certain caution with usort parameters:
Caution: Returning non-integer values from the comparison function,
such as float, will result in an internal cast to integer of the
callback's return value. So values such as 0.99 and 0.1 will both be
cast to an integer value of 0, which will compare such values as
equal.
After proper replacing(formatting) change your code as shown below:
function order_by_price($a, $b) {
$floats = str_replace(",", "", [$a['price'], $b['price']]);
return ($floats[0] == $floats[1])? 0 : (($floats[0] < $floats[1]) ? -1 : 1);
}
usort($product_array,"order_by_price");
The above will work as expected ...
Related
I have the following array (just a part of the complete array) and I want to extract the value of [cat_ID].
Array ([1] => stdClass Object ( [term_id] => 21 [name] => z_aflyst [slug] => z_aflyst
[term_group] => 0 [term_taxonomy_id] => 23 [taxonomy] => category
[description] => [parent] => 0 [count] => 1 [object_id] => 7 [cat_ID] => 21
[cat_name] => z_aflyst ))
So I need to extract the 21 in this case. However, I only want to extract the cat_ID if the cat_name equals z_aflyst.
Give that you have an array of objects:
Array (
[1] => stdClass Object
(
[term_id] => 21
[name] => z_aflyst
[slug] => z_aflyst
[term_group] => 0
[term_taxonomy_id] => 23
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 1
[object_id] => 7
[cat_ID] => 21
[cat_name] => z_aflyst )
)
foreach ($items as $item)
{
if($item->cat_name == 'z_aflyst')
{
//do stuff
}
}
You have an array of standard objects, which properties can be accessed using the arrow (object) operator ->.
You can use array_filter() to filter out unwanted elements of the array:
// Assuming your array is called $items
$filtered = array_filter($items, function($v) {
return $v->cat_name === 'z_aflyst';
});
After that, you can "flatten" the array using array_map() so that it only contains the cat_ID if you wish:
$filtered = array_map(function($v) {
return $v->cat_ID;
}, $filtered);
This will leave you with a one-dimension, indexed array of cat_IDs.
Your array is in this example a little more than a simple array, is a standard object. And you can take advantage of this. You can the properties of a standard object by this formula:
$object->property;
in your case the object is
$object = $array[1];
or by this formula as an array
$array[key];
in your case to get the value of cat_ID:
$object->cat_ID;
So your code will be something like:
if ($object->cat_name == 'z_aflyst') {
// do stuff with your cat_ID value
echo $object->cat_ID;
}
// will echo 21 only of the cat_name has value 'z_aflyst'
i need to sort an array like this in function of the price:
Array
(
[SJ] => Array
(
[desc] => Junior Suite
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 607.08
[status] => OK
[policy] => 1
[currency] => EU
)
[1] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 700.80
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
[MZ] => Array
(
[desc] => Doble Deluxe con hidromasaje
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 518.40
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
)
Can someone give me the right way to do that? :)
EXPECTED RESULT
Array
(
[MZ] => Array
(
[desc] => Doble Deluxe con hidromasaje
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 518.40
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
[SJ] => Array
(
[desc] => Junior Suite
[solutions] => Array
(
[0] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 607.08
[status] => OK
[policy] => 1
[currency] => EU
)
[1] => Array
(
[code] => BB
[desc] => Bed and Breakfast
[price] => 700.80
[status] => OK
[policy] => 1
[currency] => EU
)
)
)
)
What i need is to order that array in function of price.
So if i have many solutions, i need to take the one that have minor price
You can use the PHP function usort like this:
function sortInnerPrice($a, $b) {
if ($a['price'] == $b['price']) {
return 0;
}
return ($a['price'] < $b['price']) ? -1 : 1;
}
// First sort the inner prices
foreach ($test as $key => $val) {
usort($test[$key]['solutions'], 'sortInnerPrice');
}
function cmp($a, $b)
{
$aPrice = $a['solutions'][0]['price'];
$bPrice = $b['solutions'][0]['price'];
if ($aPrice == $bPrice) {
return 0;
}
return ($aPrice < $bPrice) ? -1 : 1;
}
// Then sort by lowest solution price
usort($yourArray, "cmp");
usort is a PHP function that allows you to sort an array based on whatever you want. You create a function that will return 0 if they are the same, -1 if you want $a before $b, and 1 if you want $a after $b.
Try to make another array containing all the prices and then use array_multisort for it
I have an array of data returned from the eventbrite.com api stored in a variable called $restrictedEvents which looks like the data below. This is representative of just one event for the purposes of pasting here but it has a about 80 stdClass Objects like this in the full array.
I want to sort this array alphabetically by the [title] key in each stdClass Object. I have tried using:
usort($restrictedEvents, "title");
However this returns the following error:
Warning: usort() [function.usort]: Invalid comparison function in model.php on line 109
My guess is it cannot find the title key as this is in the next level down. Any pointers on where I am going wrong and how I can sort by the title would be greatly appreciated. Many thanks.
Array
(
[4791063199] => stdClass Object
(
[box_header_text_color] => 393837
[link_color] => EE6600
[box_background_color] => FFFFFF
[box_border_color] => D9D4D0
[timezone] => Europe/London
[organizer] => stdClass Object
(
[url] => http://www.eventbrite.com/org/2866607767
[description] =>
[long_description] =>
[id] => 2866607767
[name] => B&Q Manifestival
)
[background_color] => E3DFDC
[id] => 4791063199
[category] =>
[box_header_background_color] => F0ECE9
[capacity] => 20
[num_attendee_rows] => 0
[title] => Closed Event Test
[start_date] => 2012-11-07 19:00:00
[status] => Live
[description] => Lorem ipsum
[end_date] => 2012-11-07 21:00:00
[tags] =>
[timezone_offset] => GMT+0000
[text_color] => 393837
[title_text_color] =>
[password] =>
[tickets] => Array
(
[0] => stdClass Object
(
[ticket] => stdClass Object
(
[description] =>
[end_date] => 2012-11-07 17:00:00
[min] => 1
[max] => 1
[price] => 0.00
[quantity_sold] => 0
[visible] => true
[currency] => GBP
[quantity_available] => 20
[type] => 0
[id] => 15940001
[name] => Manifestival Event
)
)
)
[created] => 2012-11-07 10:40:36
[url] => http://www.eventbrite.com/event/4791063199
[box_text_color] => 393837
[privacy] => Private
[venue] => stdClass Object
(
[city] =>
[name] => HR Training Room
[country] =>
[region] =>
[longitude] => 0
[postal_code] =>
[address_2] =>
[address] =>
[latitude] => 0
[country_code] =>
[id] => 2619469
[Lat-Long] => 0.0 / 0.0
)
[modified] => 2012-11-07 10:47:20
[repeats] => no
)
The second paramater to usort should be a function. See http://php.net/manual/en/function.usort.php. You would need to pass it a function like:
function cmp($a, $b)
{
return strcmp($a->title, $b->title);
}
I think you would then call it like usort($restrictedEvents, "cmp");.
You can do it with ouzo goodies:
$result = Arrays::sort($restrictedEvents, Comparator::compareBy('title'));
http://ouzo.readthedocs.org/en/latest/utils/comparators.html
The second parameter to usort is a function, not a key name. The function gets passed two elements of the array being sorted and returns a value indicating how those two elements should be ordered with respect to each other: -1 if the order in which they're passed to the function is correct, 1 if it's reversed, and 0 if it doesn't matter (the two elements are equal as far as the comparison goes). Here's an example for your case:
usort($restrictedEvents,
function($a, $b) { return strcmp($a->title, $b->title); });
If you're on an older PHP (before 5.3.0, which introduced anonymous functions), then you have to give the comparison function a name and pass that name as a string to usort:
function titlecmp($a, $b) {
return strcmp($a->title, $b->title);
}
usort($restrictedEvents, "titlecmp");
Usually something like this is achieved via a second array with $keyToSort => $id, sort this array via the standart sort functions or your own, then you have a conversion to your first array
using this, the depth of your array is limitless.
Basically I'm trying to sort a complex array of Objects within an array:
Array
(
[190515] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 190515
[occurrences] => 1
[this_weeks_occurrences] => 0
[end_date] => 1350853200
[end_date_end_time] => 1350853200
[is_ongoing] => 0
[title] => Wentz Concert Hall and Fine Arts Center
[times] => Array
(
[0] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 190515
[venue_title] => Wentz Concert Hall and Fine Arts Center
[datepart] => 20121021
[occurrences] => 1
[times] => Sun 4:00pm
[end_times] => Sun 4:00pm
[next_year] => 0
[next_month] => 0
[next_week] => 3
[occurrence_date] => 1350853200
)
)
[times_list] => Array
(
[0] => Oct 21 sun 4:00pm
)
)
[31403] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 31403
[occurrences] => 1
[this_weeks_occurrences] => 0
[end_date] => 1350176400
[end_date_end_time] => 1350176400
[is_ongoing] => 0
[title] => KAM Isaiah Israel
[times] => Array
(
[0] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 31403
[venue_title] => KAM Isaiah Israel
[datepart] => 20121014
[occurrences] => 1
[times] => Sat 8:00pm
[end_times] => Sat 8:00pm
[next_year] => 0
[next_month] => 0
[next_week] => 2
[occurrence_date] => 1350176400
)
)
[times_list] => Array
(
[0] => Oct 13 sat 8:00pm
)
)
[33861] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 33861
[occurrences] => 1
[this_weeks_occurrences] => 0
[end_date] => 1350781200
[end_date_end_time] => 1350781200
[is_ongoing] => 0
[title] => Music Institute of Chicago, Nichols Concert Hall
[times] => Array
(
[0] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 33861
[venue_title] => Music Institute of Chicago, Nichols Concert Hall
[datepart] => 20121021
[occurrences] => 1
[times] => Sat 8:00pm
[end_times] => Sat 8:00pm
[next_year] => 0
[next_month] => 0
[next_week] => 3
[occurrence_date] => 1350781200
)
)
[times_list] => Array
(
[0] => Oct 20 sat 8:00pm
)
)
)
I need to sort by occurrence_date and ensure that the data in the top Object (e.g.190515) doesn't become corrupted with other objects and to include the possibility that there could be a 2nd occurrence_date for the "times" [array] of Objects.
I've seen similar sort arrays of objects by field here, but not with the depth of the array/object. I tried using usort but I don't think my syntax to the value is correct.
Basically what I tried.
function cmp($x, $y) {
if ($x->occurrence_date > $y->occurrence_date) {
return 1; }
else {
return -1; }
}
usort($node->timeout_events_schedule->venues, 'cmp');
Thanks in advance
How about:
function compare($x,$y)
{
if($x->times[0]->occurrence_date == $y->times[0]->occurrence_date)
return 0;
elseif($x->times[0]->occurrence_date < $y->times[0]->occurrence_date)
return -1;
else
return 1;
}
uasort($your_array,'compare');
uasort() preserve your keys, unlike usort()
http://www.php.net/manual/en/function.uasort.php
Few tips to help you solve your problem:
you will need uasort function, to preserve associative keys
You can not access occurrence_date key directly from $x o r $y, you will need $x->times[0]->occurence_date
Before doing comparison, iterate through $x->times just to check if there are more entries, pick one that suits your needs.
since occurence_date is a number you don't have to use string comparison function
Good luck :)
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.