Print two arrays side by side

Asked

Viewed 582 times

4

How to join two arrays being the index of the array 1 follow the index of the array 2.

Data for joining:

descricao

array1= datacol1, datacol2, datacol3, datacol4

array2= collinha1, collinha2, collinha3 ,collinha4 

Print like this:

Description, datacol1, collinha1

Description, datacol2, collinha2

Description, datacol3, collinha3

Description, datacol4, collinha4

<?php
$desc1 = 'Listar=';
$nome['24-01-2016'] = 'João';
$nome['25-01-2016'] = 'Telma';
$nome['26-01-2016'] = 'Joana';
$nome['27-01-2016'] = 'Thiago';
$nome['28-01-2016'] = 'Marcio';
$nome['29-01-2016'] = 'Juliana';
$nome['30-01-2016'] = 'Marcos';
$nome['31-01-2016'] = 'Mariana';

foreach($nome as $indice => $valor){

print $desc1;
print '=>>>';
print $indice;
print '-';
print $valor;
print'<br>';

}
?>

  • Where does the description come from? And do you really want to put the two together (three?) or just print together? They are different things. To put two together arrays "side by side" can be used array_combine ( array $keys , array $values ), but your question needs to define better what you want. It has numerous ways to mess with arrays in PHP. I recommend [Edit] the question and clarify it better, before the staff start answering in "kick".

  • i edited the post put the code I managed to do I took the Dice and put the dates see so.

  • tried to merge an array and then a Sorting ?

  • got here i changed made the ivcs scheme gave me more right way. but this way it also looks good.

  • I ask you to take a look at my answer, just to know how to treat cases of iterating two arrays, but the same with different sizes.

2 answers

8


The simplest way to do that would be with a for (that wouldn’t bring the arrays, but as I realized you just want to print them together), it would look like this:

<?php

// your code goes here
$arr1 = array ('datacol1', 'datacol2', 'datacol3', 'datacol4');

$arr2= array ('collinha1', 'collinha2', 'collinha3', 'collinha4');

for($i = 0; $i < count($arr1); $i ++)
{
    echo 'descricao, '.$arr1[$i].', '.$arr2[$i].'<br>\n';
}

In Ideone

  • 1

    That really want thank you helped a lot.

4

I would do with a array_map to generate an array "side by side" and then iterate it with foreach.

$array1 = [1, 2, 3];

$array2 = [1, 2, 3, 4];

foreach (array_map(null, $array1, $array2) as $key => $value) {

    echo $value[0], ' e ', $value[1];

}

Utilise array_map with more than one array and with the null in the first parameter, instead of a callback, causes you to generate an array (based on the example given in that answer):

[
   [1,1], [2, 2], [3,3], [null, 4]
]

Optionally, if you are using a version equal to or higher than PHP 5.5, you can use foreach together with the list.

foreach(array_map(null, $array1, $array2) as $key => list($a, $b)) {

    echo $a, ' e ', $b;
}
  • thank you very much, I prefer with for, the scheme that the Ivcs passed above.

  • 1

    In fact, the answer was just a complement. It was just for you to understand, in an easy way, how to join two arrays, even if they do not contain the same amount of :D

  • haha Valew greatly improved my knowledge.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.