3

私は次の配列を持っています...

[0] => Array
(
  [Name] => Steve Richard
  [Car_Colour] => Red
)

[1] => Array
(
  [Name] => Scott Lucas
  [Car_Colour] => Orange
)

[2] => Array
(
  [Name] => Danny Sisum
  [Car_Colour] => Red
)

[3] => Array
(
  [Name] => Alice Small
  [Car_Colour] => Green
)

配列内のデータを出力する方法はありますが、データを車の色でグループ化して、2 台の赤い車を持っている人が赤のヘッダーの下に表示され、他の人はオレンジと緑のヘッダーの下に表示されるようにするにはどうすればよいですか?

例:

People with **Red** cars

    Name:** Steve Richard
    Car Colour:** Red

    Name:** Danny Sisum
    Car Colour: Red

People with **Orange** cars

    Name:** Scott Lucas
    Car Colour:** Orange

People with **Green** cars

    Name:** Alice Small
    Car Colour:** Green
4

4 に答える 4

2

あなたが試すことができます

$array = array(
        "0" => Array("Name" => "Steve Richard","Car_Colour" => "Red"),
        "1" => Array("Name" => "Scott Lucas","Car_Colour" => "Orange"),
        "2" => Array("Name" => "Danny Sisum","Car_Colour" => "Red"),
        "3" => Array("Name" => "Alice Small","Car_Colour" => "Green"));

$list = array();
foreach($array as $data)
{
    $list[$data['Car_Colour']][] = $data;
}

var_dump($list);

上記のように表示するには

echo "<pre>";
foreach($list as $color => $data)
{
    printf("People with **%s** cars\n",$color);

    foreach($data as $info)
    {
        printf("\tName:** %s\n",$info['Name']);
        printf("\t Car Colour:** %s\n",$info['Car_Colour']);
    }
    print(PHP_EOL);
}

出力

People with **Red** cars
    Name:** Steve Richard
     Car Colour:** Red
    Name:** Danny Sisum
     Car Colour:** Red

People with **Orange** cars
    Name:** Scott Lucas
     Car Colour:** Orange

People with **Green** cars
    Name:** Alice Small
     Car Colour:** Green
于 2012-10-18T18:56:39.470 に答える
1
$array = array(); // The array you currently have
// Grouping
$grouped = array();
foreach ($array as $item) {
    $colour = $item['Car_Colour'];
    if (!isset($grouped[$colour])) {
        $grouped[$colour] = array();
    }
    $grouped[$colour][] = $item['Name'];
}
// Output
$output = '';
foreach ($grouped as $colour => $names) {
    $output .= 'People with **' . $colour . '** cars' . "\n\n";
    foreach ($names as $name) {
        $output .= "\t" . 'Name:** ' . $name . "\n";
        $output .= "\t" . 'Car Colour:** ' . $colour . "\n";
    }
}
echo $output;
于 2012-10-18T18:54:26.477 に答える
1

何かのようなもの:

$grouped_array = array();
foreach($array as $person){
   if(!isset($grouped_array[$person['car_colour'])){
         $grouped_array[$person['car_colour']] = array();
   }
   $grouped_array[$person['car_colour'][] = $person['name']; 
}
于 2012-10-18T18:56:14.953 に答える
0
$arr_color = array();  // This line is only truly neccessary if $arr_data can be empty
foreach ($arr_data as $arr) 
    $arr_color[$arr["Car_Colour"]][] = $arr["Name"];

// Display
foreach ($arr_color as $color => $lst_persons) {
    echo "<p>People with *** $color *** cars</p>";
    foreach ($lst_persons as $person) {
        echo "Name: " . $person . "<br>";
    }
}

入力:

  $arr_data = array(
  0 => array(
         "Name" => "Steve Richard",
         "Car_Colour" => "Red"
       ),
  1 => array(
         "Name" => "Scott Lucas",
         "Car_Colour" => "Orange"
       ),
  2 => array(
         "Name" => "Danny Sisum",
         "Car_Colour" => "Red"
       ),
  3 => array(
         "Name" => "Alice Small",
         "Car_Colour" => "Green"
       )
  );

出力:

People with *** Red *** cars
Name: Steve Richard
Name: Danny Sisum

People with *** Orange *** cars
Name: Scott Lucas

People with *** Green *** cars
Name: Alice Small
于 2012-10-18T18:54:41.100 に答える