2

I have used array_combine to print out 2 different arrays, but the function use first array for index, and he supress values with same content. I need printout even when has a same value. Some tip about how resolve this?

function mostra_2com($valor, $valor2){
    echo "<table class='bs'><tr><td><b>Atividades</b></td><td><b>Opiniões</b></td></tr>";
    foreach (array_combine($valor, $valor2) as $val => $val2) 
    echo "<tr><td>".$val." </td><td> ".$val2."<br></td></tr>";
    echo"</table>";
}
enter code here
2
  • Do they have the same length?
    – MisterBla
    Aug 23, 2013 at 14:56
  • Can you show an example of your two arrays?
    – Mark Baker
    Aug 23, 2013 at 15:00

4 Answers 4

3

You probably want to use MultipleIterator:

$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);
$mi->attachIterator(new ArrayIterator($valor), 'val');
$mi->attachIterator(new ArrayIterator($valor2), 'val2');

foreach ($mi as $values) {
    extract($values);
    echo '<tr><td>', $val, '</td><td>', $val2, '<br></td></tr>';
}

It iterates over both arrays at the same time and for each iteration yields $values as an array like this:

array('val' => 1, 'val2' => 4);

In this example 1 and 4 would come from $valor and $valor2 respectively. I then use extract() inside the loop to bind those keys to actual variables.

5
  • I can put this in a function? Aug 23, 2013 at 15:01
  • @MarceloAymone Yeah, sure!
    – Ja͢ck
    Aug 23, 2013 at 15:03
  • If PHP > 5.5 you could use list() in the foreach as well
    – Mark Baker
    Aug 23, 2013 at 15:04
  • Tx guys, i using php 5.4, but i will try the @Jack mode. Aug 23, 2013 at 15:08
  • @Jack - I've just added it to my own answer, it's a pretty cool feature for when people finally do get PHP 5.5 on their hosts
    – Mark Baker
    Aug 23, 2013 at 15:11
0

You could use array_merge()

$a = array('1', '2');
$b = array('1', '4');
$c = array_merge($a,$b);
print_r($c);
// Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 4 );
0

Try this instead:

function my_array_combine($keys, $values) {
     $result = array();
     foreach ($keys as $i => $k) {
         $result[$k][] = $values[$i];
     }
     array_walk($result, create_function('&$v', '$v = (count($v) == 1)? array_pop($v): $v;'));
     return    $result;
 }

ripped from http://php.net/manual/en/function.array-combine.php It should be possible to make this code alot faster, if you are doing <1,000,000 items, not a problem as is.

0

Maybe a case for an SPL multipleiterator:

$valor = array(10, 20, 30);
$valor2 = array(15, 10, 15);

$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL | MultipleIterator::MIT_KEYS_ASSOC);
$mi->attachIterator(new ArrayIterator($valor), 'val');
$mi->attachIterator(new ArrayIterator($valor2), 'val2');
foreach($mi as $details) {
    echo "<tr><td>" . $details['val'] . "</td><td>" . $details['val2'] . "<br></td></tr>";
}

PHP > 5.5

$valor = array(10, 20, 30);
$valor2 = array(15, 10, 15);

$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($valor));
$mi->attachIterator(new ArrayIterator($valor2));
foreach($mi as list($val, $val2)) {
    echo "<tr><td>" . $val. "</td><td>" . $val2. "<br></td></tr>";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.