[PHP] There is an array

$array = [список1,список1,список1,список2,список2,список3,список3,список3,список3]; 

It is necessary that the output in html is as follows:

 <td>список1</td> <td>список1</td> <td>список1</td> <td>Это был вывод значения из массива: список1 </td> <td>список2</td> <td>список2</td> <td>Это был вывод значения из массива: список2 </td> <td>список3</td> <td>список3</td> <td>список3</td> <td>список3</td> <td>Это был вывод значения из массива: список3</td> 
  • But how do you understand that one list has ended and another has begun? - splash58
  • I think it's easier to create several arrays than artificial intelligence - Blacknife
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

There may be several options. Here are some of them.

Option 1

  <?php $array = ["список1","список1","список1","список2","список2","список3","список3","список3","список3"]; $current = $array[0]; foreach($array as $row) { if($row==$current) { echo $row."<br>"; }else { echo "Это был вывод значения из массива:".$current."<br>"; echo $row."<br>"; $current=$row; } } echo "Это был вывод значения из массива:".$current."<br>"; ?> 

Option 2

  function array_group_by($arr) { $result=array(); foreach ($arr as $value ) { $result[$value][] = $value; } return $result; } $array = ["список1","список1","список1","список2","список2","список3","список3","список3","список3"]; $group = array_group_by($array); foreach($group as $gr) { foreach($gr as $row) { echo $row."<br>"; } echo "Это был вывод значения из массива:".$gr[0]."<br>"; } 
  • Thank you very much! I thought this code was enough for me to finish my code, but I was mistaken. Help me again. Given: There are values ​​in a single array $ array You need output in this form: <td> Name1 </ td> <td> Name2 </ td> <td> Name3 </ td> <td> This was the output value from a single array: Name </ td> <td> list1 </ td> <td> list2 </ td> <td> This was the output of a value from a single array: list </ td> <td> Therm111 </ td> <td> Therm222 </ td> <td> Therm333 </ td> <td> Therm444 </ td> <td> This was the output of a value from a single array: Therm </ td> - AI ljgbkb
  • If you need another answer, update your question. If your new question is very different from the already set, then it is better to ask a new question. - Stepan Kasyanenko

My intellect was not enough to understand that these are in fact identical elements of the array. Then you can do it

 $array = ["список1","список1","список1","список2","список2","список3","список3","список3","список3"]; $cnt = array_count_values($array); $out = []; foreach ($cnt as $k=>$v) { $out = array_merge($out, array_fill(0, $v, $k), [ "Это был вывод значения из массива: $k" ]); } echo implode('<br>', $out);