0

I have a set of php arrays

$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
  )

These array's are being looped though and placed in a table

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

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

}

When returned and displayed on the page the table works just as intended with everything matched how they are supposed to be

new    yellow    monday
old    green     tuesday
fresh  red       wednesday

etc,etc, I would like to group the first column so that it list all the 'new', then all the 'old', then all the fresh, while keeping the intended matching ex,

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

etc etc

1
  • How did these related sets of data end up in separate arrays to begin with? Commented Aug 24, 2017 at 19:17

5 Answers 5

2

First, join the three arrays into one. Then, sort the new array by the first value (new first, then old, then fresh):

<?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

Sign up to request clarification or add additional context in comments.

Comments

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>";

Comments

0

Straight-forward solution:

$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>';

The output (push "Run code snippet"):

<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>

Comments

0

Another way to do it is

 $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;

Live demo : https://eval.in/850007

Comments

0

Using array_multisort() with a custom sorting order might seem the most intuitive solution, but this risks alphabetizing subsequently nominated arrays to "break ties" while sorting within each group. Demo

$priority = array_flip($arrayOne);
array_multisort(
    array_map(fn($v) => $priority[$v], $arrayOne),
    $arrayOne,
    $arrayTwo,
    $arrayThree
);

Instead, sort the first array and retain its original indexes to preserve relationships to the other arrays. Demo

$priority = array_flip($arrayOne);
uasort(
    $arrayOne,
    fn($a, $b) => $priority[$a] <=> $priority[$b]
);

foreach ($arrayOne as $i => $v) {
    printf("%s, %s, %s\n", $v, $arrayTwo[$i], $arrayThree[$i]);
}

As previously mentioned, it is curious why you have these 3 separate arrays in the first place. Coding would be simpler if you had a single 2d array containing these payloads. Then you could use: Sorting a php array of arrays by custom order

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.