Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用php在多维数组中搜索和打印输出_Php_Arrays - Fatal编程技术网

用php在多维数组中搜索和打印输出

用php在多维数组中搜索和打印输出,php,arrays,Php,Arrays,我正在学习PHP中的数组。我正在研究多维数组,其中我面临一个问题 以下是示例多维数组的图示: Array ( [0] => Array ( [venue] => chicago [course] => science [time] => 2pm ) [1] => Array ( [venue] =>

我正在学习PHP中的数组。我正在研究多维数组,其中我面临一个问题

以下是示例多维数组的图示:

Array
(
    [0] => Array
        (
            [venue] => chicago
            [course] => science
            [time] => 2pm
        )
    [1] => Array
        (
             [venue] => New York
             [course] => Robotics
             [time] => 3 p.m.
        )
    [2] => Array
        (
             [venue] => New York
             [course] => Science
             [time] => 4 p.m.
        )
    [3] => Array
        (
             [venue] => Chicago
             [course] => Robotics
             [time] => 4 p.m.
        )
)
我希望从上述阵列打印的所需输出为:

Chicago
Science  2 p.m.
Robotics  4 p.m. 

New York 
Science 4 p.m.
Robotics 3 p.m. 

有谁能指导我如何解决这个问题吗?

您可以使用foreach获得以下结果

foreach ($rows as $row) {
    echo $row['venue']."<br>";
    echo $row['firstname']."  ".$row['lastname'];
}
foreach($rows作为$row){
echo$row['vention']。“
”; echo$row['firstname']。“”.$row['lastname']; }
您可以使用foreach获得以下结果

foreach ($rows as $row) {
    echo $row['venue']."<br>";
    echo $row['firstname']."  ".$row['lastname'];
}
foreach($rows作为$row){
echo$row['vention']。“
”; echo$row['firstname']。“”.$row['lastname']; }
为每个场馆创建一个新阵列,并将课程和时间分别存储为键和值

// define an array
$new_array = array();
// loop the array
foreach($array as $key=>$value){
    // create an array with each venue and  course and time as key and values
    $new_array[$value['venue']][$value['course']] = $value['time'];
}
循环新数组并根据需要显示结果

// loop the new array
foreach($new_array as $key=>$value){
    echo $key."\n";  // echo the venue
    krsort($value);  // Sort an array by key in reverse order
    foreach($value as $k=>$v){
        echo $k . ' ' . $v."\n"; // echo the course and time
    }
    echo "\n";
}
发出

chicago
science 2pm
robtics 4pm

newyork
science 4pm
robotics 3pm

为每个场地创建一个新数组,并将课程和时间分别存储为键和值

// define an array
$new_array = array();
// loop the array
foreach($array as $key=>$value){
    // create an array with each venue and  course and time as key and values
    $new_array[$value['venue']][$value['course']] = $value['time'];
}
循环新数组并根据需要显示结果

// loop the new array
foreach($new_array as $key=>$value){
    echo $key."\n";  // echo the venue
    krsort($value);  // Sort an array by key in reverse order
    foreach($value as $k=>$v){
        echo $k . ' ' . $v."\n"; // echo the course and time
    }
    echo "\n";
}
发出

chicago
science 2pm
robtics 4pm

newyork
science 4pm
robotics 3pm

一种方法是首先按地点对阵列进行排序
usort
可用于此

usort($events, function($a, $b) {
    return strcmp($a['venue'], $b['venue']);
});
数组排序后,您可以对其进行迭代,并在每次到达新地点时输出一个标题

$previousVenue = null;
foreach ($events as $event) {

    // check if this venue is the same as the previous, output header if not
    if ($event['venue'] != $previousVenue) {
        echo "<h3>$event[venue]</h3>";
    }
    // always output course and time
    echo "$event[course] $event[time]<br>";

    // current venue becomes previous venue
    $previousVenue = $event['venue'];
}
$previousvention=null;
foreach($events作为$event){
//检查此地点是否与前一地点相同,如果不相同,则输出标题
如果($event['vention']!=$previousvention){
回声“$事件[地点]”;
}
//始终输出课程和时间
回显“$event[课程]$event[时间]
”; //当前场地变为上一个场地 $PreviousVincement=$event['Vincement']; }
一种方法是首先按地点对阵列进行排序
usort
可用于此

usort($events, function($a, $b) {
    return strcmp($a['venue'], $b['venue']);
});
数组排序后,您可以对其进行迭代,并在每次到达新地点时输出一个标题

$previousVenue = null;
foreach ($events as $event) {

    // check if this venue is the same as the previous, output header if not
    if ($event['venue'] != $previousVenue) {
        echo "<h3>$event[venue]</h3>";
    }
    // always output course and time
    echo "$event[course] $event[time]<br>";

    // current venue becomes previous venue
    $previousVenue = $event['venue'];
}
$previousvention=null;
foreach($events作为$event){
//检查此地点是否与前一地点相同,如果不相同,则输出标题
如果($event['vention']!=$previousvention){
回声“$事件[地点]”;
}
//始终输出课程和时间
回显“$event[课程]$event[时间]
”; //当前场地变为上一个场地 $PreviousVincement=$event['Vincement']; }