array combine of unequal length of TWo arrays

前端 未结 3 1174
星月不相逢
星月不相逢 2021-01-20 15:45

I need help of combination of two arrays in php.

What I have:

numbers = array(1, 2,3,4,5,6);
letters = array(\'q\', \'w\', \'e\', \'r\');
         


        
3条回答
  •  余生分开走
    2021-01-20 16:26

    $numbers = [1, 2,3,4,5,6];  
    $letters = ['q', 'w', 'e', 'r'];
    $count = 0;
    $combination = array();
    foreach ($numbers as $num) 
    {
        $val = '';
        if (isset ($letters[$count]))
        {
            $val = $letters[$count];
        }
        $combination += array($num => $val);    
        $count ++;
    }
    
    foreach($combination as $n => $l)
    {
        echo $n . $l . "
    "; }

提交回复
热议问题