【问题标题】:Using a condition to match values in a multidimensional array使用条件匹配多维数组中的值
【发布时间】:2012-07-06 19:19:31
【问题描述】:

我正在尝试使用 if 语句来比较两个数组的值。我不是在比较索引。如果这些值相互匹配,那么我想从多维数组中打印出一个值。我尝试使用以下解决方案:

  1. How to compare two arrays and remove matching elements from one for the next loop?

  2. array values in multidimensional array

  3. Array_values from multidimensional array

但是,这些示例并不能完全回答我的问题。我不需要生成一个新数组,我想循环遍历这些值,如果有匹配项,则将一个值回显到屏幕上。

请看我下面的代码,问题在'///////////////////////'标记下面:

<?php


$str ="";


// events array
$events = array( array("PokeMon", "Hall D" , "1:00 AM"),
                 array("Sonic the Hedgehog", "WorkShop 1" , "11:00 AM"),
                 array("Tasbura", "Video 3" , "2:00 PM"),
                 array("Sailor Moon ", "Panel 4" , "6:00 PM") 
               );

// location array
$row=array(
"Arena","Hall D","Video 1","Video 2","Video 3","Video 4","Video 5","Video 6","HD Theater",
"Panel 1","Panel 2","Panel 3","Panel 4","WorkShop 1","WorkShop 2","WorkShop 3","WorkShop 4",
"Autograph 1","Autograph 2"
 );

// event start time array
$col=array(
"","9:00 AM","10:00 AM","11:00 AM","1:00 PM","2:00 PM","3:00 PM","4:00 PM","5:00 PM","6:00 PM",
"7:00 PM","8:00 PM","9:00 PM","10:00 PM","11:00 PM","12:00 AM","1:00 AM","2:00 AM",
 );

// length of  location and start time arrays 
$rowLength=count($row);
$colLength=count($col);


$str .= "<table><tr><td bgcolor='green'><table border='1' bordercolor='black'><tr>";

for($i = 0; $i < $colLength; $i++){
   $str .= "<td width='100' height='25'  bgcolor='yellow'>";
   $str .= $col[$i];
   $str .= "</td>";


}


   for ($j = 0; $j < $rowLength; $j++){
    $str .= "<tr><td width='100' height='25' bgcolor='pink'>";
    $str .= $row[$j];
    $str .= "</td> ";


        for ($k = 0; $k < $colLength-1; $k++){

            $str .= "<td width='100' height='25'  bgcolor=' #70DBDB'>";



//////////////////////Here is where I need to compare the arrays:

            if( ($row[$j]==$events[$j][1]) && ($col[$k+1]==$events[$k+1][2])){

                $str .= $events [$j][0];
///////////////////////////////////////////////////////////////////     


            }else{

            $str .= " ";

            } 

            $str .= "</td>";

        }

        $str .= " </tr>";

    }

 echo $str ."</table></td></tr></table>";

?>

这是我正在尝试创建的示例: http://leobee.com/android/push/so/stdt2.php

这是示例中的代码:

<?php
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

$str ="";
$row=array(
'Arena',"Hall D","Video 1","Video 2","Video 3","Video 4","Video 5","Video 6","HD Theater",
"Panel 1","Panel 2","Panel 3","Panel 4","WorkShop 1","WorkShop 2","WorkShop 3","WorkShop 4",
"Autograph 1","Autograph 2"
 );

$col=array(
"","9:00 AM","10:00 AM","11:00 AM","1:00 PM","2:00 PM","3:00 PM","4:00 PM","5:00 PM","6:00 PM",
"7:00 PM","8:00 PM","9:00 PM","10:00 PM","11:00 PM","12:00 AM","1:00 AM","2:00 AM",
 );


$rowLength=count($row);
$colLength=count($col);
//echo $rowLength." ".$colLength."<br>";

$str .= "<table><tr><td bgcolor='green'><table border='1' bordercolor='black'><tr>";

for($i = 0; $i < $colLength; $i++){
   $str .= "<td width='100' height='25'  bgcolor='yellow'>";
   $str .= $col[$i];
   $str .= "</td>";



}



   for ($j = 0; $j < $rowLength; $j++){
   $str .= "<tr><td width='100' height='25' bgcolor='pink'>";
   $str .= $row[$j];


   $str .= "</td> ";


    for ($k = 0; $k < $colLength-1; $k++){
   $str .= "<td width='100' height='25'  bgcolor=' #70DBDB'>";
////////////////////////////////////////////////////////////////////////////    
        if( ($row[$j]=="Arena") && ($col[$k+1]=="9:00 AM")){

             $str .= "Poketmon";

        }else{
            $str .= "";
        }

 ///////////////////////////////////////////////////////////////////////////
   $str .= "</td>";

    }

   $str .= " </tr>";

}

 echo $str ."</table></td></tr></table>";

?>

【问题讨论】:

    标签: php multidimensional-array indexing compare


    【解决方案1】:

    我一直在使用您的代码并得出以下解决方案。希望大家喜欢和理解:

    <?php
    
    // events array
    $events = array(
        array('PokeMon', 'Hall D' , '1:00 AM'),
        array('Sonic the Hedgehog', 'WorkShop 1' , '11:00 AM'),
        array('Tasbura', 'Video 3' , '2:00 PM'),
        array('Sailor Moon ', 'Panel 4' , '6:00 PM') 
    );
    
    $events_flat = array();
    
    foreach($events as $event)
    {
        $events_flat[$event[0]] = $event[1] . $event[2];
    }
    
    // location array
    $locations = array(
        'Arena', 'Hall D', 'Video 1', 'Video 2', 'Video 3',
        'Video 4', 'Video 5', 'Video 6', 'HD Theater', 'Panel 1', 
        'Panel 2', 'Panel 3', 'Panel 4', 'WorkShop 1', 'WorkShop 2',
        'WorkShop 3', 'WorkShop 4', 'Autograph 1', 'Autograph 2'
    );
    
    // event start time array
    $times = array(
        '9:00 AM', '10:00 AM', '11:00 AM', '1:00 PM', '2:00 PM',
        '3:00 PM', '4:00 PM', '5:00 PM', '6:00 PM', '7:00 PM',
        '8:00 PM', '9:00 PM', '10:00 PM', '11:00 PM', '12:00 AM',
        '1:00 AM', '2:00 AM'
    );
    
    $html = '<table><tr><td bgcolor="green"><table border="1" bordercolor="black"><tr><td></td>';
    
    foreach ($times as $time)
    {
        $html .= '<td width="100" height="25" bgcolor="yellow">';
        $html .= htmlspecialchars($time);
        $html .= '</td>';
    }
    
    foreach ($locations as $location)
    {
        $html .= '<tr><td width="100" height="25" bgcolor="pink">';
        $html .= htmlspecialchars($location);
        $html .= '</td>';
    
        foreach ($times as $time)
        {
            $html .= '<td width="100" height="25" bgcolor="#70DBDB">';
    
            $event = array_search($location . $time, $events_flat);
    
            if ($event === FALSE)
            {
                $html .= ' ';
            }
            else
            {
                $html .= htmlspecialchars($event);
            }
    
            $html .= '</td>';
        }
    
        $html .= ' </tr>';
    }
    
    $html .=  '</table></td></tr></table>';
    
    echo $html;
    
    ?>
    

    【讨论】:

    • 这太棒了!我没有考虑扁平化阵列。而且我不知道我可以通过搜索来比较这样的两个数组。谢谢。
    • 我对这一行有点困惑 $events_flat[$event[0]] = $event[1] . $event[2]; $event[0] 的值被重新分配(连接)到 $event[1] 的值。 $event[2] 你如何在后面的代码中取回值:$event = array_search($location . $time, $events_flat);?从文档中 array_search 产生一个索引,它不是值。抱歉,我在这一点上有点困惑。
    猜你喜欢
    • 1970-01-01
    • 2018-04-24
    • 2014-05-25
    • 1970-01-01
    • 2012-08-06
    • 2019-12-14
    • 2019-03-18
    • 1970-01-01
    • 2013-10-04
    相关资源
    最近更新 更多