How to loop through multiple arrays at the same time (parallel)

后端 未结 2 447
梦如初夏
梦如初夏 2021-01-27 06:54

Oke so I don\'t know why this is so hard, all information I find is only for two arrays like array_combine.

I have 3 arrays that I get from dynamically created input fie

2条回答
  •  没有蜡笔的小新
    2021-01-27 07:39

    Since you said that all arrays match by their keys, I will assume you have something like this:

    $article_ids = [10, 22, 13];
    $article_descriptions = ["this is one", "this is two", "this is three"];
    $article_amounts = [20, 10, 40];
    

    Therefore in order to obtain their information in an orderly manner, you would first need to found how many elements there are. We can use the total of the first array, by using count(), then using a for loop to iterate and obtain each array's information.

    //get the number of articles
    $num = count($article_ids);
    
    //iterate through each article count
    for ($i = 0; $i < $num; $i++){
        echo 'Article id: '.$article_ids[$i].'
    '; echo 'Article description: '.$article_descriptions[$i].'
    '; echo 'Article amount: '.$article_amounts[$i] .'
    '; }

提交回复
热议问题