结合php数组,匹配值

时间:2017-08-24 18:58:20

标签: php arrays

我有一组php数组

$arrayOne = (
    0 => new,
    1 => old,
    2 => fresh,
    3 => new,
    4 => old,
    5 => fresh,
    6 => new,
    7 => old,
    8 => fresh,

    )
$arrayTwo = (
   0 => yellow,
   1 => green,
   2 => red,
   3 => blue,
   4 => grey,
   5 => orange,
   6 => purple,
   7=> pink,
   8 => brown
   )
$arrayThree = (
  0 => Monday
  1 => Tuesday
  2 => Wednesday
  3 => Thursday
  4 => Friday
  5 => Saturday
  6 => Sunday
  7 => Monday2
  8 => Monday3
  )

这些数组正在循环并放在表格中

for($index = 0; index < 100; $index++){

 $returnVariable .= '<td>'.$ArrayOne[$index].'</td>';
 $returnVariable .= '<td>'.$ArrayTwo[$index].'</td>';
 $returnVariable .= '<td>'.$ArrayThree[$index].'</td>';

}

当返回并显示在页面上时,表格的工作方式与预期的一致,

new    yellow    monday
old    green     tuesday
fresh  red       wednesday

等等,我想将第一列分组,以便列出所有新的&#39;,然后列出所有&#39;旧&#39;,然后全部新鲜,同时保持意图匹配前,

new   yellow     monday
new   blue       thursday
new   purple     sunday
old   green      tuesday
old   grey       friday
old   pink       Monday2 

等等

4 个答案:

答案 0 :(得分:2)

首先,将三个数组合并为一个。然后,按第一个值(新的第一个,然后是旧的,然后是新的)对新数组进行排序:

<?php    
$arrayOne = [
    0 => "new",
    1 => "old",
    2 => "fresh",
    3 => "new",
    4 => "old",
    5 => "fresh",
    6 => "new",
    7 => "old",
    8 => "fresh",

];
$arrayTwo = [
   0 => "yellow",
   1 => "green",
   2 => "red",
   3 => "blue",
   4 => "grey",
   5 => "orange",
   6 => "purple",
   7=> "pink",
   8 => "brow"
];
$arrayThree = [
  0 => "Monday",
  1 => "Tuesday",
  2 => "Wednesday",
  3 => "Thursday",
  4 => "Friday",
  5 => "Saturday",
  6 => "Sunday",
  7 => "Monday2",
  8 => "Monday3",
];
echo "<pre>";
for ($i = 0; $i < count($arrayOne); $i++) {
    $array[] = [
        $arrayOne[$i],
        $arrayTwo[$i],
        $arrayThree[$i],
    ];
}
$values = [ // give these strings a numeric value to compare them
    "new" => 0,
    "old" => 1,
    "fresh" => 2,
];
usort($array, function($a, $b) use ($values) {
    return $values[$a[0]] - $values[$b[0]];
});

Demo

答案 1 :(得分:0)

 Create new array to hold all then sort it and implode it into a string 
   <?php
    $arrayOne = array(
        0 => "new",
        1 => "old",
        2 => "fresh",
        3 => "new",
        4 => "old",
        5 => "fresh",
        6 => "new",
        7 => "old",
        8 => "fresh",

        );
    $arrayTwo = array(
       0 => "yellow",
       1 => "green",
       2 => "red",
       3 => "blue",
       4 => "grey",
       5 => "orange",
       6 => "purple",
       7=> "pink",
       8 => "brown"
       );
    $arrayThree =array(
      0 => "Monday",
      1 => "Tuesday",
      2 => "Wednesday",
      3 => "Thursday",
      4 => "Friday",
      5 => "Saturday",
      6 => "Sunday",
      7 => "Monday2",
      8 => "Monday3"
      );
    $returnVariable=array();
    for($index = 0; $index<count($arrayOne); $index++){

     $returnVariable[$index][0]= '<td>'.$arrayOne[$index].'</td>';
     $returnVariable[$index][1]= '<td>'.$arrayTwo[$index].'</td>';
     $returnVariable[$index][2]= '<td>'.$arrayThree[$index].'</td>';

    }
    sort($returnVariable);
    echo "<table>";
     for ($i=0; $i<count($returnVariable); $i++) {
            if (@is_array($returnVariable[$i])) 
                $returnVariable[$i] = implode($returnVariable[$i]," ");
    echo "<tr>";

        print $returnVariable[$i];
        echo "</tr>";

    }
    echo "</table>";

答案 2 :(得分:0)

直接解决方案:

$arrayOne = array(0 => "new",1 => "old",2 => "fresh",3 => "new",4 => "old",5 => "fresh",6 => "new",7 => "old",8 => "fresh",);
$arrayTwo = array(0 => "yellow",1 => "green",2 => "red",3 => "blue",4 => "grey",5 => "orange",6 => "purple",7=> "pink",8 => "brown");
$arrayThree = array(0 => "Monday",1 => "Tuesday",2 => "Wednesday",3 => "Thursday",4 => "Friday",5 => "Saturday",6 => "Sunday",7 => "Monday2",8 => "Monday3");

$result = [];
foreach($arrayOne as $k => $v){
    $result[$v][] = "<tr><td>$v</td><td>{$arrayTwo[$k]}</td><td>{$arrayThree[$k]}</td></tr>";
}

echo '<table>';
foreach(['new', 'old', 'fresh'] as $k){
    echo implode("", $result[$k]);
}
echo '</table>';

输出(推送&#34; 运行代码段&#34;):

&#13;
&#13;
<table border="1"><tr><td>new</td><td>yellow</td><td>Monday</td></tr><tr><td>new</td><td>blue</td><td>Thursday</td></tr><tr><td>new</td><td>purple</td><td>Sunday</td></tr><tr><td>old</td><td>green</td><td>Tuesday</td></tr><tr><td>old</td><td>grey</td><td>Friday</td></tr><tr><td>old</td><td>pink</td><td>Monday2</td></tr><tr><td>fresh</td><td>red</td><td>Wednesday</td></tr><tr><td>fresh</td><td>orange</td><td>Saturday</td></tr><tr><td>fresh</td><td>brown</td><td>Monday3</td></tr></table>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

另一种方法是

 $row ="";
 $temp = array();
 foreach($arrayOne as $key => $value){
  $temp[$value][] = $key;
 }
foreach($temp as $value){
   foreach($value as $value2){
     $row .= ' '.$arrayOne[$value2].'';
     $row .= ' '.$arrayTwo[$value2].'';
     $row .= ' '.$arrayThree[$value2]."\n";
   }
}
echo $row;

现场演示:https://eval.in/850007