I have an array as below:
Array
(
[time] => Array
(
[0] => 6:00 PM
[1] => 4:00 PM
)
[id] => Array
(
[0] => #1 Hits of the 60's
[1] => 3 Men and a Lady
)
[url] => Array
(
[0] => hits-of-the-60
[1] => 3-men-and-a-lady
)
[ev_evc_id] => Array
(
[0] => 2
[1] => 2
)
[address] => Array
(
[0] => Caravelle Theatre
[1] => God and Country Theatre
)
[phone] => Array
(
[0] => 1-800-4Branson (800-427-2676)
[1] => 1-800-4Branson (800-427-2676)
)
)
I want to sort the array based on time ascending.
Desired output is as below:
Array
(
[time] => Array
(
[0] => 4:00 PM
[1] => 6:00 PM
)
[id] => Array
(
[0] => 3 Men and a Lady
[1] => #1 Hits of the 60's
)
[url] => Array
(
[0] => 3-men-and-a-lady
[1] => hits-of-the-60
)
[ev_evc_id] => Array
(
[0] => 2
[1] => 2
)
[address] => Array
(
[0] => God and Country Theatre
[1] => Caravelle Theatre
)
[phone] => Array
(
[0] => 1-800-4Branson (800-427-2676)
[1] => 1-800-4Branson (800-427-2676)
)
)
I have tried asort, usort. But could not make it work. Any help / suggestion is appreciated.
You can do it with array_multisort, it will sort all arrays based on the first array given:
array_multisort(
$myArray["time"],
$myArray["id"],
$myArray["url"],
$myArray["ev_evc_id"],
$myArray["address"],
$myArray["phone"]
);
Your array is in the wrong order. Try if you can change it to this and usort will work
Array
(
[0] => Array
(
[time] => 6:00 PM
[id] => 1
...
)
[1] => Array
(
[time] => 4:00 PM
[id] => 2
...
)
)
Related
I have an array-
$arraydelivered =
Array
(
[0] => Array
(
[0] => 1
[1] => pending
[2] => January
)
[1] => Array
(
[0] => 4
[1] => pending
[2] => April
)
[2] => Array
(
[0] => 7
[1] => pending
[2] => July
)
[3] => Array
(
[0] => 10
[1] => pending
[2] => October
)
)
I want to resort this array dynamically to
Array
(
[0] => Array
(
[0] => 4
[1] => pending
[2] => April
)
[1] => Array
(
[0] => 7
[1] => pending
[2] => July
)
[2] => Array
(
[0] => 10
[1] => pending
[2] => October
)
[3] => Array
(
[0] => 1
[1] => pending
[2] => January
)
)
I have tried to find out the subarray based on which I know exactly from where to re-sort
foreach ($arraydelivered as $keyD => $valueD) {
if($valueD[0] == $cycle){
print_r($valueD);
}
}
This has given me the output -
Array ( [0] => 4 [1] => pending [2] => April )
Now I want to use this sub-array as the identifier to resort to the main array. Basically this sub-array will be the resort starting point for the big array.
Here is the solve array
$keyD1=0;
if(!empty($arraydelivered)){
foreach ($arraydelivered as $keyD => $valueD) {
if($valueD[0] == $cycle){
$keyD1= $keyD;
}
}
$outputsec = array_slice($arraydelivered, $keyD1);
$outputfirst = array_slice($arraydelivered, -4, $keyD1);
$finalarray= array_merge($outputsec,$outputfirst);
$arraydelivered=$finalarray;
I have 2 PHP arrays that look like this..
$array1
--------
Array
(
[0] => Array
(
[0] => 64
[1] => Apple
)
[1] => Array
(
[0] => 22
[1] => Pear
)
[2] => Array
(
[0] => 3
[1] => Raisin
)
[3] => Array
(
[0] => 15
[1] => Grape
)
[4] => Array
(
[0] => 11
[1] => Banana
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
$array2
--------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
)
I want to merge the arrays together but put the matching items from $array2 at the top so the result would look like this...
$array3
-------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
[2] => Array
(
[0] => 64
[1] => Apple
)
[3] => Array
(
[0] => 3
[1] => Raisin
)
[4] => Array
(
[0] => 15
[1] => Grape
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
I'm not sure how to approach, should I merge the two first and then try and do some ordering, or is there a more efficient approach?
Get the 2nd array and then a rest of the 1st array
array_merge($arr2, array_udiff($arr1, $arr2, function($i1, $i2) {return $i1[0]-$i2[0];}));
Sir,
I am confused what should be exact title of this issues. Below is my problem
I have a multidimensional array like below
Array
(
[0] => Array
(
[0] => 2017-11-01
[1] => 9 Am
)
[1] => Array
(
[0] => 2017-11-02
[1] => 07 Pm
)
[2] => Array
(
[0] => 2017-11-03
[1] => 11 Pm
)
[3] => Array
(
[0] => 2017-11-04
[1] => 03 Pm
)
[4] => Array
(
[0] => 2017-11-01
[1] => 11 Am
)
[5] => Array
(
[0] => 2017-11-02
[1] => 05 Pm
)
)
Now, I want to make unique date beside time should be into another array.
I want it as below...
Array
(
[0] => Array
(
[0] => 2017-11-01
[1] => Array
(
[0] => 9 Am
[1] => 11 Am
)
)
[1] => Array
(
[0] => 2017-11-02
[1] => Array
(
[0] => 07 Pm
[1] => 05 Pm
)
)
[2] => Array
(
[0] => 2017-11-03
[1] => Array
(
[0] => 11 Pm
)
)
[3] => Array
(
[0] => 2017-11-04
[1] => Array
(
[0] => 03 Pm
)
)
)
Thanks in advance..
Regards,
Anwar
Something of that fashion should do the trick...
Its nowhere near the fastest way to do so but it should get you going and it is pretty easy to read/understand
// putting the times in a new array where the date is the key
$byDate = [];
foreach ($firstArray as [$date, $time]) {
$byDate[$date][] = $time;
}
// going thru it again to have it the way you needed it
$newArray = [];
foreach ($byDate as $date => $times) {
$newArray[] = [$date, $times];
}
Suppose I had a excel xlsx document called names with a couple of entries and wanted to print out the names from a php file.
include 'simplexlsx.class.php';
$xlsx = new SimpleXLSX('names.xlsx');
print_r( $xlsx->rows() );
This would print:
Array ( [0] => Array ( [0] => John [1] => Smith ) [1] => Array ( [0]
=> Steven [1] => Brake ) [2] => Array ( [0] => Carter [1] => Firen ) [3] => Array ( [0] => Juan [1] => Lahyno ) [4] => Array ( [0] => Sarah
[1] => Parker ) [5] => Array ( [0] => Julie [1] => Roberts ) [6] =>
Array ( [0] => Will [1] => Smith ) [7] => Array ( [0] => Angelina [1]
=> Jolie ) )
Is there a way to print out the data in a readable way? Or does print_r not allow formatting of an array passed to it?
If you add
echo '<pre>';
print_r( $xlsx->rows() );
Then you see somethiink like this:
Array (
[0] => Array
(
[0] => John [1] => Smith
)
[1] => Array
(
[0] => Steven [1] => Brake
)
...
I hope this will help you
I have the following 2D-Array:
Array (
[ID] => Array
(
[0] => 150
[1] => 250
)
[Group] => Array
(
[0] => 120
[1] => 120
)
[Name] => Array
(
[0] => Name 1
[1] => Name 2
)
[Price] => Array
(
[0] => 9.99
[1] => 11.99
)
[Date] => Array
(
[0] => 12.04.2013
[1] => 11.04.2013
)
[SortIndex] => Array
(
[0] => 20
[1] => 10
)
)
and I want to sort them after the SortIndex-Array ascending. But the different Values from the Arrays are related to the others. So the association can't be changed.
How I could sort them?
Is there is any reason not to convert array to more suitable form?
Array (
[0] => Array
(
[ID] => 150
[Group] => 120
)
[1] => Array
(
[ID] => 250
[GROUP] => 120
)
....