循环遍历数组并按顺序显示每个子数组

时间:2015-10-28 13:43:17

标签: php arrays loops multidimensional-array foreach

我有一个包含3个不同数组的数组。

<form id='userForm'>
    <input type="text" name="url">
    <input type="submit" value="Get link!">
</form>

<!-- where the response will be displayed -->
<div id='response'></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>
<script>
$(document).ready(function(){
    $('#userForm').submit(function(){

        // show that something is loading
        $('#response').html("<b>Loading response...</b>");

        /*
         * 'post_receiver.php' - where you will pass the form data
         * $(this).serialize() - to easily read form data
         * function(data){... - data contains the response from post_receiver.php
         */
        $.ajax({
            type: 'POST',
            url: 'api/generate', 
            data: $(this).serialize()
        })
        .done(function(data){

            // show the response
            $('#response').html(data);

        })
        .fail(function() {

            // just in case posting your form failed
            alert( "Posting failed." );

        });

        // to prevent refreshing the whole page page
        return false;

    });
});
</script>

</body>
</html>

如何遍历$ arrayAll,在每次迭代时显示每个子数组(array1,array2,array3)的第一个元素?

因此,输出将是:

{"error":{"code":"1000","message":"A URL is required."}}

依此类推......直到取得所有的分段。

编辑:

$arrayAll = (

    0 => $array1,
    1 => $array2,
    2 => $array3
);

1 个答案:

答案 0 :(得分:2)

$maxLength = 0;

foreach($arrayAll as $value)
    {
        if(count($value)>$maxLength)
        {
            $maxLength = count($value);
        }
    }

for($i=0; $i<$maxLength; $i++)
{
    foreach($arrayAll as $value)
    {
        if(isset($value[$i]))
        {
            echo $value[$i];    
        }

    }
}