0

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

2
  • Does this answer your question? PHP - Merging two arrays into one array (also Remove Duplicates) Commented Jan 22, 2021 at 12:57
  • @RobinSingh, in your link I find this $result = array_unique(array_merge($A,$B)); and with loop for ($i=0, $n=sizeof($result); $i<$n; $i++) { echo $result[$i] . ', ' . $B[$i] . '<br />'; }, but the result is not 100% that I need.thak you I appreciate your help Commented Jan 22, 2021 at 13:15

1 Answer 1

1

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>";
}   
?>
Sign up to request clarification or add additional context in comments.

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.