简体   繁体   中英

create array from array php

There's my array :

$horaire[] = array(
        'Title' => 'Title_value',
        'Day' => 'Day_value',
        'Hour' => 'Hour_value',
        '1erDiff' => 'yes'
);

Let's say i have 50 entries.. (pull from EE channel entries.. ) Day_value are from 0 - 6 I want to create new arrays from this array if the value of Day is ex.:

$Sunday[] = array ('Title' => 'Title_value',
        'Day' => 'Day_value', /**ALL VALUE HERE WILL BE 0 */
        'Hour' => 'Hour_value',
        '1erDiff' => 'yes');

$Monday[] = array ('Title' => 'Title_value',
        'Day' => 'Day_value', /**ALL VALUE HERE WILL BE 1 */
        'Hour' => 'Hour_value',
        '1erDiff' => 'yes');

and so on...

OR

Second question Is it possible to sort the first array $horaire as it's sort from 0-6 (day_value) but inside this also sort hour_value (without changing the Day_value sorting.. ) ex,:

[0] = array (Day => 0, Hour => 5)
[1] = array (Day => 0, Hour => 9)
[2] = array (Day => 1, Hour => 3)
[3] = array (Day => 1, Hour => 10)

and so on...

Ex.: of data

Array ( 
[0] => Array ( [Nom] => Femmes entrepreneures [Jour] => 2 [Heure] => 18h30 [Min] => 30 [1erDiff] => oui ) 
[1] => Array ( [Nom] => Ma santé au quotidien [Jour] => [Heure] => [Min] => [1erDiff] => oui ) 
[2] => Array ( [Nom] => Noël j’magasine [Jour] => 2 [Heure] => 22h53 [Min] => 53 [1erDiff] => oui ) 
[3] => Array ( [Nom] => Noël j’magasine [Jour] => 1 [Heure] => 22h30 [Min] => 30 [1erDiff] => non )
 [4] => Array ( [Nom] => Lancement de la programmation [Jour] => 2 [Heure] => 20h40 [Min] => 40 [1erDiff] => oui ) 
 [5] => Array ( [Nom] => Voyages magazine [Jour] => [Heure] => [Min] => [1erDiff] => oui ) 
 [6] => Array ( [Nom] => AMS Moto [Jour] => 4 [Heure] => 20h00 [Min] => 00 [1erDiff] => oui ) 
 [7] => Array ( [Nom] => AMS Moto [Jour] => 4 [Heure] => 22h30 [Min] => 30 [1erDiff] => non ) 
 [8] => Array ( [Nom] => AMS Moto [Jour] => 6 [Heure] => 05h00 [Min] => 00 [1erDiff] => non ) 
 [9] => Array ( [Nom] => AMS VTT / Motoneige [Jour] => 3 [Heure] => 17h00 [Min] => 00 [1erDiff] => oui ) 
 [10] => Array ( [Nom] => AMS VTT / Motoneige [Jour] => 3 [Heure] => 16h35 [Min] => 35 [1erDiff] => non ) 
 [11] => Array ( [Nom] => AMS VTT / Motoneige [Jour] => 4 [Heure] => 19h28 [Min] => 28 [1erDiff] => non ) 
 [12] => Array ( [Nom] => AMS VTT / Motoneige [Jour] => 5 [Heure] => 21h28 [Min] => 28 [1erDiff] => non ) 
 [13] => Array ( [Nom] => AMS VTT / Motoneige [Jour] => 1 [Heure] => 05h28 [Min] => 28 [1erDiff] => non )
  [14] => Array ( [Nom] => AMS VTT / Motoneige [Jour] => 4 [Heure] => 19h47 [Min] => 47 [1erDiff] => non ) 
  [15] => Array ( [Nom] => Virage plus [Jour] => 3 [Heure] => 17h39 [Min] => 39 [1erDiff] => oui ) 
  )

Second part: You can define your own sort function that compares two entries by Day and Hour, then sort using that function.

http://www.php.net/manual/en/function.uasort.php

To put the values into their own array you could try something like this:

$days = array();
foreach ($horaire as $value) {
    if (!isset($days[$value['Day']])) {
        $days[$value['Day']] = array();
    }
    array_push($days[$value['Day']], $value);
}

Then at the end you would have

$days[0] = all values where Day = 0
$days[1] = all values where Day = 1
...
$days[6] =  all values where Day = 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM