array combine of unequal length of TWo arrays

前端 未结 3 1260
星月不相逢
星月不相逢 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:30

    Shortest solution

     $num) { 
        echo $num .' ';
        if (array_key_exists($key ,$letters)) // check if array element exists
        echo $letters[$key].'
    '; else echo "
    "; }

    Explainaition.
    First we foreach the number array or the longer array, and we echo it's element, while doing that, we check if the index exists in the letters array, if it does then we echo that element with the number.

提交回复
热议问题