简体   繁体   English

从数组php创建数组

[英]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.: 假设我有50个条目..(从EE通道条目中拉出..)Day_value的取值范围是0-6,如果Day的值是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,: 第二个问题是否可以对第一个数组$ horaire进行排序,因为它的排序范围是0-6(day_value),但在其中还可以排序hour_value(不更改Day_value的排序。)例如:

[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 http://www.php.net/manual/zh/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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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