How to sort a PHP by date - php

Here is my array:
Array (
[0] => Array ( [0] => content here [1] => 2010-02-04 01:25:34 )
[1] => Array ( [0] => content here [1] => 2010-02-04 04:51:37 )
[2] => Array ( [0] => content here [1] => 2010-02-04 04:52:31 )
[3] => Array ( [0] => content here [1] => 2010-02-04 05:50:48 )
[4] => Array ( [0] => content here [1] => 2010-02-04 03:25:34 )
[5] => Array ( [0] => content here [1] => 2010-02-04 05:39:33 )
[6] => Array ( [0] => content here [1] => 2010-02-04 03:25:34 )
[7] => Array ( [0] => content here [1] => 2010-02-04 07:07:09 )
[8] => Array ( [0] => content here [1] => 2010-02-04 07:07:23 )
[9] => Array ( [0] => content here [1] => 2010-02-04 08:51:18 )
)
How can I sort it by the timestamp?

Or usort() with strtotime():
function compare($e1, $e2) {
$t1 = strtotime($e1[1]));
$t2 = strtotime($e2[1]));
if($t1 == t2) {
return 0;
}
return ($t1 > $t2) ? 1 : -1;
}
usort($array, 'compare');

Use usort() with a cmp_function that compares index 1 of each of the passed arguments.

Use array_multisort.

array_multisort() It's a nasty but powerful little function. Basically you'll have to iterate through your array, pulling out the date stamp into a new array - maintaining key association. Then, sort that new array, still maintaining key association. Then throw both your newly sorted array and the original array into array_multisort() and your original array will be sorted to have keys in the same order as your sorted array.
Clear as mud? The examples on that doc page should help.

How about Bubble sort?
That means looping through each date, check if the previous is bigger, etc.

Related

Sort a subArray by based on order of Array in php?

I have a big array as $orderArr:
$orderArr = array("IZQ", "AOH", "VNP", "ICW", "IOQ", "BXP", "SHH", "EAY", "ZAF", "CUW");
which looks like
Array ( [0] => IZQ [1] => AOH [2] => VNP [3] => ICW [4] => IOQ [5] => BXP [6] => SHH [7] => EAY [8] => ZAF [9] => CUW )
and I have two small arrays as $subArr1 and $subArr2:
$subArr1 = array("VNP", "BXP", "ICW", "IZQ");
$subArr2 = array("ZAF", "IZQ", "AOH");
looks like
Array ( [0] => VNP [1] => BXP [2] => ICW [3] => IZQ )
Array ( [0] => ZAF [1] => IZQ [2] => AOH )
Both small arrays (sub array) own elements belong to the big array.
I want to sort two small arrays according to the order of big array, as following:
Array ( [0] => IZQ [1] => VNP [2] => ICW [3] => BXP )
Array ( [0] => IZQ [1] => AOH [2] => ZAF )
I am looking for the simplest codes to do it in php. Any suggestions are welcome.
Probably the simplest would be to compute the intersection and it will return in the order of the first array:
$subArr1 = array_intersect($orderArr, $subArr1);
That will return with the keys of the first array; if you want to reindex instead:
$subArr1 = array_values(array_intersect($orderArr, $subArr1));
You could use usort to sort based on array position:
usort($subArr1, function ($a, $b) use ($orderArr) {
return (array_search($a, $orderArr) < array_search($b, $orderArr)) ? -1 : 1;
});
var_dump($subArr1);

Search multidimensional array for value

I wonder if there is better (faster) way to search for value in multidimensional array than looping through every item.
Lets say i have
$id_to_search = '16819976033';
And array which is pretty big
Array
(
[0] => Array
(
[id] => Array
(
[0] => 16771055710
[1] => 16776555710
[2] => 16819976033
)
[o] => 21566
[p] => 12597.66
)
[1] => Array
(
[id] => Array
(
[0] => 14089762
)
[o] => 12606
[p] => 1747.49
)
etc ...
)
I can find it if i loop through each item and than compare them but its very slow because array is big.
You can use by array_search function in PHP:
$key = array_search($id_to_search, array_column($YourArray, 'id'));

Multidimensional Array - Get unique values, but count all duplicates

I have a multi-dimensional array that I would like to get unique sub-values from, but also have a count of how many times those unique sub-values occurred.
For instance, this would be my starting array:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 3333333333333333
)
)
[1] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 5555555555555555
)
)
[2] => Array
(
[0] => Array
(
[id] => 1533438473619168
)
[1] => Array
(
[id] => 77777777777777777
)
)
In the end, I'd like to have an array that looks like this:
[0] => Array
(
[0] => Array
(
[id] => 1533438473619168
[count] => 3
)
[1] => Array
(
[id] => 3333333333333333
[count] => 1
)
[2] => Array
(
[id] => 5555555555555555
[count] => 1
)
[3] => Array
(
[id] => 77777777777777777
[count] => 1
)
)
Is there any general/easy way to do this without iterating through the first array for each value, comparing/storing the values in a temporary array, checking them, and adding to the count?
To get this exact format you may need to iterate thought your current array and do the counting manually, however php has the array_count_values() and array_unique() functions for this kind of thing:
http://php.net/manual/en/function.array-count-values.php
http://php.net/manual/en/function.array-unique.php
Because you are only concerned with the deepest values of the array, using array_walk_recursive seems suitable for this. Note that a reference to the output array $counted is used in the callback.
array_walk_recursive($ids, function($id, $k) use (&$counted) {
$counted[$id] = isset($counted[$id]) ? $counted[$id] + 1 : 1;
});
Using the id as the key in the $counted array will simplify the counting. The result of this will be somewhat different from your suggested output, but in my opinion it would actually be simpler to use. (e.g. foreach ($counted as $id => $count) {...).
$counted = array(
"1533438473619168" => 3
"3333333333333333" => 1
"5555555555555555" => 1
"77777777777777777" => 1);

How to unset last item in multidimensional array?

I have a array like below
Array
(
[0] => Array
(
[0] => Date
[1] => Name
[2] => Hours
)
[1] => Array
(
[0] => 2013-01-02
[1] => Test User
[2] => 7:59
)
[2] => Array
(
[0] => 2013-01-03
[1] => Test User
[2] => 7:53
)
[3] => Array
(
[0] => 2013-01-04
[1] => Test User
[2] => 8:12
)
.
.
.
.
[16] => Array
(
[0] =>
[1] => Total
[2] => 103:1
)
[17] => Array
(
[0] =>
)
)
And want to remove last item from array, I have tried array_pop but this is not working after passing above array to array_pop gives me output
Array
(
[0] =>
)
How can I achieve this with minimum code.
Try:
unset ($array_name[count($array_name)-1]);
$callback = function(&$array) { array_pop($array); };
array_walk($array, $callback);
This will pop the last element from each triplet.
Try like
$my_cnt = count($my_arr);
unset($my_arr[$my_cnt-1]);
if all your arrays are indexed by numbers from zero to max without any breaks, you can use
unset($ar[count($ar)-1]);
otherwise try
end($ar);
unset($ar[key($ar)]);
You are seeing the "popped" element instead of the modified array.
array_pop() returns the data in the element that it removes from the array.
This means that you wrote:
print_r(array_pop($array));
Instead, modify the array with array_pop(), then print the array.
array_pop($array);
print_r($array);

Sort MultiDimensional Array created By Object Class by multiple keys

What I'd like to do is sort this multidimensional array by multiple keys. I've attempted to use array_multisort() but haven't had luck because of the type of array.
A function that can have multiple keys to it to sort by (relevancy, date) would be best so that other arrays with the same structure could use it as well.
This is the $array I'm working with:
Array
(
[0] => DALQueryResult Object
(
[_results:DALQueryResult:private] => Array
(
[0] => 32048
[id] => 32048
[1] => 1
[relevancy] => 1
)
)
[1] => DALQueryResult Object
(
[_results:DALQueryResult:private] => Array
(
[0] => 32002
[id] => 32002
[1] => 1
[relevancy] => 1
)
)
[2] => DALQueryResult Object
(
[_results:DALQueryResult:private] => Array
(
[0] => 31921
[id] => 31921
[1] => 1
[relevancy] => 1
)
)
[3] => DALQueryResult Object
(
[_results:DALQueryResult:private] => Array
(
[0] => 31868
[id] => 31868
[1] => 1
[relevancy] => 1
)
)
[4] => DALQueryResult Object
(
[_results:DALQueryResult:private] => Array
(
[0] => 31811
[id] => 31811
[1] => 1
[relevancy] => 1
)
)
)
I've tried this function (found here), but haven't had luck:
$sort = array();
foreach($array as $k=>$v) {
$sort['relevancy'][$k] = $v['relevancy'];
$sort['date'][$k] = $v['date'];
}
# sort by event_type desc and then title asc
array_multisort($sort['relevancy'], SORT_DESC, $sort['date'], SORT_DESC, $array);
After running array_multisort function I see this:
Fatal error: Cannot use object of type DALQueryResult as array
I take it you are using code that can be found here. If so, just modify your SQL query with:
ORDER BY relevancy, date
In your case it doesn't work because DALQueryResult is not an array, so $v['relevancy'] will cause an error (it should be $v->relevancy). Even if you changed it, it wouldn't work because DALQueryResult is an object, not an array (and it doesn't implement ArrayAccess either).
I'd also suggest you use PDO instead of a random class found on the web (if that's the case, of course). :)

Categories