I’m not 100% sure about the title (something is missing), but I’m 100% sure what output I need.
$A = array('0' => '1451', '1' => '1451', '2' => '1452', '3' => '1452', '4' => '1453', '5' => '1453', '6' => '1457', '7' => '1460'); $B = array('0' => '22', '1' => '23', '2' => '22', '3' => '23', '4' => '22', '5' => '23', '6' => '', '7' => ''); for ($i=0, $n=sizeof($A); $i<$n; $i++) { echo $A[$i] . ' = ' . $B[$i] . '<br />'; } echo '<hr></hr>'; echo 'I need this output, is possible: 1451 = 22, 23<br /> 1452 = 22, 23<br /> 1453 = 22, 23<br /> 1457 =<br /> 1460 = ';
You can run the code here: https://extendsclass.com/php-bin/3baf302
Advertisement
Answer
With a for
loop, you need a change in your code:
<?php // Input $A = array('0' => '1451', '1' => '1451', '2' => '1452', '3' => '1452', '4' => '1453', '5' => '1453', '6' => '1457', '7' => '1460'); $B = array('0' => '22', '1' => '23', '2' => '22', '3' => '23', '4' => '22', '5' => '23', '6' => '', '7' => ''); // Output array $O = array(); for ($i = 0, $n = count($A); $i < $n; $i++) { $O[$A[$i]][] = $B[$i]; } // Print the output foreach ($O as $key => $items) { echo $key." = ".implode(',', $items)."<br>"; } ?>