0

I would like to combine these two foreach statements together. I've seen a few solutions around here, but nothing really works for me.

This is my username list from database.

$digits = [1,2,3,4];
$results = $db->table($usernames)
    ->where('memberID', $mID)->limit(10)
    ->getAll();

foreach ($results as $result) {
echo $result->userName;
}

I tried this:

$combined = array_merge($digits, $results);

foreach (array_unique($dogrularVeSiklar) as $single) : { ?>
    {
        echo $single.'<br>';
        echo $results->userName;
    },
}
Prox
  • 11
  • 4

2 Answers2

0

You don't show what $dogrularVeSiklar is or where you get it, but as an example; combine into $key => $value pairs and foreach exposing the key and value:

$combined = array_combine($digits, $results);

foreach ($combined as $digit => $result) {
        echo $digit . '<br>' . $result;
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Thank you, I updated my question - I have added some information – Prox Mar 20 '18 at 15:41
  • This assumes that both arrays are of the same size ... in which case PDO would achieve the same with its PDO::FETCH_KEY_PAIR mode. – Dormilich Mar 20 '18 at 15:43
-2

foreach operates on only one array at a time.

The way your array is structured, you can use array_combine() function to combine them into an array of key-value pairs then foreach that single array