php array_multisort not sorting my multidimensional array - php

Array
(
[0] => Array
(
[id] => 7
[workorder_id] => 27
[truck_id] => 4
[event_type] => 1
[location_id] =>
[location_name] => Billing Address
[address_address] => 123 Main Street
[address_city] => Montreal
[address_state] => QC
[address_zip] => A1A1A1
[address_country_id] => 1
[contact] => bob
[phone] => 555-555-555
[fax] => 555-555-555
[po] => 123131
[notes] =>
[appointment_from] => 2013-03-30 12:30:00
[appointment_to] => 2013-03-30 14:30:00
[crossdock] => 0
[status] => 1
)
[1] => Array
(
[id] => 8
[workorder_id] => 27
[truck_id] => 4
[event_type] => -1
[location_id] =>
[location_name] => Billing Address
[address_address] => 123 Main Street
[address_city] => Montreal
[address_state] => QC
[address_zip] => A1A1A1
[address_country_id] => 1
[contact] =>
[phone] => 555-555-555
[fax] =>
[po] =>
[notes] =>
[appointment_from] => 2013-04-04 06:00:00
[appointment_to] => 2013-04-04 12:00:00
[crossdock] => 0
[status] => 1
)
[2] => Array
(
[id] => 9
[workorder_id] => 27
[truck_id] => 4
[event_type] => 1
[location_id] =>
[location_name] => Billing Address
[address_address] => 123 Main Street
[address_city] => Montreal
[address_state] => QC
[address_zip] => A1A1A1
[address_country_id] => 2
[contact] => Jim Smith
[phone] => 555-555-555
[fax] => 555-555-555
[po] =>
[notes] =>
[appointment_from] => 2013-04-16 10:00:00
[appointment_to] => 2013-04-16 12:00:00
[crossdock] => 0
[status] => 1
)
)
Okay, so I have this array, lets call it $array.
I now have a function to sort multidimensional arrays by multiple keys.
function sort_multiple_keys($array,$key1,$key1_sort = SORT_DESC,$key2,$key2_sort = SORT_ASC){
$sort = array();
if(count($array) > 0){
foreach($array as $k=>$v) {
$first[$k] = $v[$key1];
$second[$k] = $v[$key2];
}
array_multisort($first, $key1_sort, $second, $key2_sort, $array);
}
unset($sort);
}
So, I would to sort it by the EVENT TYPE and then APPOINTMENT_FROM date. So I run this function:
sort_multiple_keys($array,'event_type',SORT_DESC,'appointment_from',SORT_ASC);
But nothing??
Any help?

You're not actually returning a value from your sort function.
You can either return $array at the end; or pass it in as a reference, so the function works on the original instance instead of a copy; for that, you need to change the function definition to:
function sort_multiple_keys(&$array,$key1,$key1_sort = SORT_DESC,$key2,$key2_sort = SORT_ASC){
^ tells PHP that this variable is by reference

Related

PHP multidimensional array keep same keys with values

I'm trying to check a multidimensional array and keep only the keys they are match and has values (not same value).
So i want to check the whole array and get back the key's they have value, check them in all arrays and only keep the key's that all arrays contains.
So here is my array:
Array
(
[0] => Array
(
[id] => 21
[tstamp] => 1508482179
[firstname] => test1
[lastname] => test1
[dateOfBirth] =>
[gender] =>
[company] =>
[street] =>
[postal] =>
[city] =>
[state] =>
[country] =>
[phone] =>
[mobile] =>
[fax] =>
[email] => test1#test.com
[website] =>
[language] =>
[groups] => a:1:{i:0;s:1:"1";}
[login] => 1
[username] => test2
[assignDir] =>
[homeDir] =>
[disable] =>
[start] =>
[stop] =>
[dateAdded] => 1508482142
[lastLogin] => 0
[currentLogin] => 0
[loginCount] => 3
[locked] => 0
[session] =>
[createdOn] => 0
)
[1] => Array
(
[id] => 2
[tstamp] => 1508482189
[firstname] => test2
[lastname] => test2
[dateOfBirth] =>
[gender] =>
[company] =>
[street] =>
[postal] =>
[city] =>
[state] =>
[country] =>
[phone] =>
[mobile] =>
[fax] =>
[email] => test2#test.com
[website] =>
[language] =>
[groups] => a:1:{i:0;s:1:"1";}
[login] => 1
[username] => test2
[assignDir] =>
[homeDir] =>
[disable] =>
[start] =>
[stop] =>
[dateAdded] => 1508482142
[lastLogin] => 0
[currentLogin] => 0
[loginCount] => 3
[locked] => 0
[session] =>
[createdOn] => 0
)
)
Code:
$arrResultMap = array_map('array_filter', $arrResult);
$currencies = count($arrResultMap) > 1 ? call_user_func_array('array_intersect', $arrResultMap) : array_shift($arrResultMap);
print_r($currencies);
Output:
Array
(
[id] => 1
[groups] => a:1:{i:0;s:1:"1";}
[login] => 1
[loginCount] => 3
)
As you see there are some key's missing. Like email, firstname, lastname, tstamp, etc. I can't see or find what i have done wrong :)
UPDATE:
Found solution instead of using array_intersect i use array_intersect_key.
Now it works.
This code work:
$arrResultMap = array_map('array_filter', $arrResult);
$currencies = count($arrResultMap) > 1 ? call_user_func_array('array_intersect_key', $arrResultMap) : array_shift($arrResultMap);
print_r($currencies);

array undefined offset with php

<?php
$ar = Array (
[0] => Array ( [id] => 1 [social_id] => [role_id] => 0 [name] => [first_name] => Jimmy [last_name] => rathore [gender] => male [dob] => 06-02-1992 [language] => english [location] => xyz [address] => xyz [email] => xyz#xyz.com [phone] => 9876543210 )
[1] => Array ( [id] => 3 [social_id] => [role_id] => 0 [name] => [first_name] => Dev [last_name] => Smith [gender] => [dob] => [language] => [location] => xyz [address] => xyz [email] => yyy#sds.com [phone] => 7838344344 )
[2] => Array ( [id] => 4 [social_id] => [role_id] => 0 [name] => [first_name] => Col [last_name] => Manon [gender] => male [dob] => 02-02-2017 [language] => english [location] => London [address] => ABCD [email] => londs#an.com [phone] => 7894561230)
) ;
$array = Array (
[0] => Array ( [id] => 1 [sm_id] => 1 [field_name] => first_name [value] => G2 [created_at] => 2017-02-24 11:05:03 [updated_at] => 2017-02-24 11:05:03 [deleted_at] => )
);
$arr = array();
foreach($row=0; $row<count($ar); $row++){
foreach($col=0; $col<count($array); $col++){
if($ar['0']['id'] == $array['0']['id']){
$arr['first_name'] = $ar['0']['first_name'];
$arr['last_name'] = $ar['0']['last_name'];
$arr['email'] = $ar['0']['email'];
$arr['phone'] = $ar['0']['phone'];
}
}
}
print_r($arr);die;
?>
I want map data from one array to other array based on $ar['0]['id'] with $array['0']['sm_id']
if $ar's id matched with $array's sm_id push to one array with its value how can i achieve the same. please suggest
Expected result
Array( [id] => 1 [first_name] => Jimmy [last_name] => rathore [gender] => male [dob] => 06-02-1992 [language] => english [location] => xyz [address] => xyz [email] => xyz#xyz.com [phone] => 9876543210 );
You could do it like this, given the original array is $ar, and the array with sm_id values $array:
// Create a hash of all sm_id values to be found
$ids = array_flip(array_column($array, "sm_id"));
// filter the original array to get only those with an id value in that hash
$arr = array_filter($ar, function ($row) use ($ids) {
return isset($ids[$row["id"]]);
});
print_r ($arr);

PHP Accessing an item in an array

I have the following array:
Cart Object
(
[event_list] => Array
(
[15] => stdClass Object
(
[event_id] => 15
[event_name] => North Pole Express 2014
[event_date] => 2014-12-06
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
[14] => stdClass Object
(
[event_id] => 14
[event_name] => North Pole Express 2014
[event_date] => 2014-11-30
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
[13] => stdClass Object
(
[event_id] => 13
[event_name] => North Pole Express 2014
[event_date] => 2014-11-29
[event_time] => 10:40:00
[event_ort_id] => 1
[ort_name] => Tanfield Railway
[ort_city] => Newcastle upon Tyne
[event_order_limit] => 0
[event_use_alt] =>
)
)
[cat_list] => Array
(
[138] => stdClass Object
(
[cat_id] => 138
[category_event_id] => 15
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
[120] => stdClass Object
(
[cat_id] => 120
[category_event_id] => 14
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
[102] => stdClass Object
(
[cat_id] => 102
[category_event_id] => 13
[cat_name] => Child - 4:00 pm
[cat_price] => 12.00
[cat_numbering] => none
)
)
[disc_list] => Array
(
)
[items] => Array
(
[4] => PlaceItem Object
(
[id] => 4
[cart] => Cart Object
*RECURSION*
[event_id] => 14
[category_id] => 120
[seats] => Array
(
[26151] => stdClass Object
(
[seat_id] => 26151
[seat_row_nr] => 0
[seat_nr] => 0
[seat_ts] => 1388769219
[discount_id] => 0
)
)
[ts] =>
[created] => 2014-01-03T16:43:39+00:00
[expired] =>
)
)
[ts] => 1388769219
)
I need to be able to access the 'Items' Key.
However everything I have tried so far has failed. I suspect I am missing something really obvious.
What would be the best way to access these items?
Cart is not an array, it's an Object.
$cart = new Cart();
$cart->items; // <-- this is an array (of PlaceItems)
Since $cart is an object you need to use object notation to access it, the arrays are underneath that:
forach ($cart->items as $key=>$value) {
//...
}
Update from your comment:
$items=$_SESSION['_SMART_cart']->items;
foreach ($items as $key=>$value) {
//...
}

array_unique wrongly discarding values

I'm trying to use array_unique to discard of repeated values.
Array
(
[0] => Array
(
[book_id] => 1203910329
[author] => Gauci, Joe
[description] => Paperback. Very Good.
[isbn] => 9781907374067
[publisher] =>
[date] =>
[currency] => USD
[price] => 10.97
[bookseller] => xx
[bookseller_location] => GBR
[condition] => 3
[shipping_ground] => 4.73
[shipping_expedited] => 6.33
)
[1] => Array
(
[book_id] => 12312314556
[author] => Gauci, Joe
[description] => Paperback. GOOD.
[isbn] => 9781907374067
[publisher] =>
[date] =>
[currency] => USD
[price] => 1.84
[bookseller] => xx
[bookseller_location] => GBR
[condition] => 2.5
[shipping_ground] => 4.73
[shipping_expedited] => 6.33
)
[2] => Array
(
[book_id] => 12312314556
[author] => Gauci, Joe
[description] => Paperback. GOOD.
[isbn] => 9781907374067
[publisher] =>
[date] =>
[currency] => USD
[price] => 1.84
[bookseller] => xx
[bookseller_location] => GBR
[condition] => 2.5
[shipping_ground] => 4.73
[shipping_expedited] => 6.33
)
)
every time I use array_unique($results) only the first value is returned
Array
(
[0] => Array
(
[book_id] => 1203910329
[author] => Gauci, Joe
[description] => Paperback. Very Good.
[isbn] => 9781907374067
[publisher] =>
[date] =>
[currency] => USD
[price] => 10.97
[bookseller] => xx
[bookseller_location] => GBR
[condition] => 3
[shipping_ground] => 4.73
[shipping_expedited] => 6.33
)
)
Where am I going with array_unique?
You can't use array_unique with nested arrays. The comparison used is clearly documented:
(string) $elem1 === (string) $elem2

Getting the value of first array from the array

In php I have an array look like this.
Array ( [0] =>
[1] => Array ([id] => 9 [slot] => 2 [name] => Test Ad [alt] => Test Ad [dimension_width] => 300 [dimension_height] => 400 [clicks] => 1 [start_date] => 06/07/2013 [end_date] => 07/07/2013 [status] => 1 [target] => http://images.google.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-151553.jpeg [pre_exp_email] => 0 )
[2] => Array ( [id] => 12 [slot] => 1 [name] => Test Ad [alt] => Test Ad [dimension_width] => 200 [dimension_height] => 300 [clicks] => 0 [start_date] => 06/08/2013 [end_date] => 07/08/2013 [status] => 1 [target] => http://facebook.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-1515532.jpeg [pre_exp_email] => 0 )
[3] => Array ( [id] => 14 [slot] => 1 [name] => Test Ad [alt] => Test Ad [dimension_width] => 200 [dimension_height] => 300 [clicks] => 0 [start_date] => 06/08/2013 [end_date] => 07/08/2013 [status] => 1 [target] => http://facebook.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-1515532.jpeg [pre_exp_email] => 0 )
)
From here I want to get the first value of array. For example I want to get the value of first array
[1] => Array ([id] => 9 [slot] => 2 [name] => Test Ad [alt] => Test Ad [dimension_width] => 300 [dimension_height] => 400 [clicks] => 1 [start_date] => 06/07/2013 [end_date] => 07/07/2013 [status] => 1 [target] => http://images.google.com [image_url] => http://localhost/WebSites/coffee/wp-content/uploads/2013/06/uwp5-1-151553.jpeg [pre_exp_email] => 0 )
So can someone kindly tell me how to get the value of 1st array?Any help and suggestions will be really appreciable. Thanks
say all your array was in a variable $myArray, then
myArray[1]
will give you your first array

Categories