打印标题并按数组值排序

时间:2016-08-15 19:32:30

标签: php arrays sorting grouping

我有一个包含可能值的数组:

$auto_types = array(CHEVY, FORD, DODGE);

我有包含这些值的数组:

$my_cars = array(
  array("make" => "FORD", model => "Thunderbird", "year" => 1983),
  array("make" => "FORD", model => "Mustang", "year" => 1971),
  array("make" => "CHEVY", model => "Impala", "year" => 1995),
  array("make" => "DODGE", model => "Charger", "year" => 2015),
  array("make" => "CHEVY", model => "Corvette", "year" => 2011),
  array("make" => "CHEVY", model => "Camaro", "year" => 2012)
)

我想返回一个列表,提供每个品牌的标题,然后通过Make对每辆车进行排序。所以输出将是:

CHEVY:
1995年Impala
2012 Corvette
2011年Camaro

DODGE:
2015充电器

FORD:
1971年Mustang
1983年雷鸟

我知道我可以运行foreach()循环并按make排序,但关键是我需要为每个品牌添加标题,而不是为每辆车列出。任何人有一个很好的方法来做到这一点? - 不确定是否需要第一个阵列,但是当时创建它似乎是一个好主意。

3 个答案:

答案 0 :(得分:1)

你不需要第一个阵列。您可以使用一个循环按照以下方式对汽车进行分组:

foreach ($my_cars as $car) {
    $makes[$car['make']][] = $car;
}

如果您希望品牌按字母顺序显示,则可以ksort($makes)

然后循环制作并打印每辆车。

foreach ($makes as $make => $cars) {
    echo "$make: ";                       // Header prints once
    foreach ($cars as $car) {
        echo "$car[model] $car[year] ";   // Multiple cars print after each header
    }
    echo '<br>';
}

答案 1 :(得分:0)

<?php

$my_cars = array(
  array("make" => "FORD", "model" => "Thunderbird", "year" => 1983),
  array("make" => "FORD", "model" => "Mustang", "year" => 1971),
  array("make" => "CHEVY", "model" => "Impala", "year" => 1995),
  array("make" => "DODGE", "model" => "Charger", "year" => 2015),
  array("make" => "CHEVY", "model" => "Corvette", "year" => 2011),
  array("make" => "CHEVY", "model" => "Camaro", "year" => 2012)
);

// defining empty arrays for each models
$ford = [];
$chevy = [];
$dodge = [];

// loop through every car and push to the right array model
foreach($my_cars as $array) {
  if($array['make'] == "FORD") {
    array_push($ford, $array);
  } elseif($array['make'] == "CHEVY") {
    array_push($chevy, $array);
  } else {
    array_push($dodge, $array);
  }
}

// echo the result by looping inside each array model
foreach($dodge as $car) {
  echo $car['year'] . ':' . $car['model'] . '<br />';
}

echo '<br />';

foreach($ford as $car) {
  echo $car['year'] . ':' . $car['model'] . '<br />';
}

echo '<br />';

foreach($chevy as $car) {
  echo $car['year'] . ':' . $car['model'] . '<br />';
}

答案 2 :(得分:-1)

嵌套循环在这里是理想的。

    <?php
        $auto_types = array("CHEVY", "FORD", "DODGE");
        $my_cars    = array(
            array("make" => "FORD",  "model" => "Thunderbird",  "year" => 1983),
            array("make" => "FORD",  "model" => "Mustang",      "year" => 1971),
            array("make" => "CHEVY", "model" => "Impala",       "year" => 1995),
            array("make" => "DODGE", "model" => "Charger",      "year" => 2015),
            array("make" => "CHEVY", "model" => "Corvette",     "year" => 2011),
            array("make" => "CHEVY", "model" => "Camaro",       "year" => 2012)
        );

        $arr = [];
        foreach($my_cars as $my_car){
            foreach($auto_types as $auto_type){
                if($make = array_search($auto_type, $my_car)){
                    $desc   = $my_car['make'] . " " . $my_car['year'] . " " . $my_car['model'];
                    if(!array_key_exists($auto_type, $arr)){
                        $arr[$auto_type] = $desc;
                    }else{
                        $arr[$auto_type] = $arr[$auto_type] .  "    " . $my_car['year'] . " " . $my_car['model'] . "";
                    }
                }
            }
        }
        ksort($arr);

        var_dump ($arr);
        // PRODUCES::           
        array (size=3)
          'CHEVY' => string 'CHEVY 1995 Impala    2011 Corvette    2012 Camaro' (length=49)
          'DODGE' => string 'DODGE 2015 Charger' (length=18)
          'FORD' => string 'FORD 1983 Thunderbird    1971 Mustang' (length=37)

        $output = implode("<br />", $arr);
        echo $output;

        // PRODUCES::
        CHEVY 1995 Impala 2011 Corvette 2012 Camaro
        DODGE 2015 Charger
        FORD 1983 Thunderbird 1971 Mustang

有效代码

    <?php
        $arr = [];
        foreach($my_cars as $my_car){
            foreach($auto_types as $auto_type){
                if($make = array_search($auto_type, $my_car)){
                    $desc   = $my_car['make'] . " " . $my_car['year'] . " " . $my_car['model'];
                    if(!array_key_exists($auto_type, $arr)){
                        $arr[$auto_type] = $desc;
                    }else{
                        $arr[$auto_type] = $arr[$auto_type] .  "    " . $my_car['year'] . " " . $my_car['model'] . "";
                    }
                }
            }
        }
        ksort($arr);
        $output = implode("<br />", $arr);  //<== GET STRING VALUE...
        echo $output;