循环数组,在PHP中首先使用类似的项目

时间:2016-06-11 07:40:49

标签: php arrays

Array
(
[0] => Array
    (
        [intID] => 3
        [intVolumeType] => 1
        [intVolume] => 10
        [totalVolumes] => 10
        [intTrack] => 10
        [intTrackType] => 2
        [totalTracks] => 10
    )

[1] => Array
    (
        [intID] => 4
        [intVolumeType] => 2
        [intVolume] => 100
        [totalVolumes] => 200
        [intTrack] => 111
        [intTrackType] => 3
        [totalTracks] => 222
    )

我想将上面的数组打印为:
intVolumeType = 1
totalVolumes = 10
intVolumeType = 2
totalVolumes = 200

<小时/> intTrackType = 2
totalTracks = 10
intTrackType = 3
totalTracks = 222

如何遍历此PHP数组。数组中总共有六个元素。哪个可能会增加到20个。打印第一个卷信息,而不是跟踪,依此类推。请帮助

1 个答案:

答案 0 :(得分:0)

在此处尝试:http://phpfiddle.org/

<?php
$a = Array
    (
    0 => [
        'intID' => 3,
        "intVolumeType" => 1,
        "intVolume" => 10,
        "totalVolumes" => 10,
        "intTrack" => 10,
        "intTrackType" => 2,
        "totalTracks" => 10,
    ],
    1 => [
        "intID" => 4,
        "intVolumeType" => 2,
        "intVolume" => 100,
        "totalVolumes" => 200,
        "intTrack" => 111,
        "intTrackType" => 3,
        "totalTracks" => 222,
    ]
);
/*echo "<pre>";
print_r( $a );
echo "</pre>";
*/
// you have to do it manually
$volume = "";
$track = "";
foreach ( $a as $item ) {
    // preparing for volumes types
    $volume .= "intVolumeType = " . $item['intVolumeType'] . "<br/>";
    $volume .= "TotalVolumes = " . $item['totalVolumes'] . "<br/>";
    // you can add other variables too
    $volume .= "<br/>";
    // preparing for track types
    $track .= "intTrackType = " . $item['intTrackType'] . "<br/>";
    $track .= "TotalTracks = " . $item['totalTracks'] . "<br/>";
    // you can add other variables too  
    $track .= "<br/>";
}
echo $volume;
echo "===================<br/>";
echo $track;
?>