2012-07-06 12 views
0

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>"; 

?> 

답변

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; 

?> 
+0

이것은 놀랍습니다! 배열을 평평하게하는 것에 대해 생각하지 않았습니다. 그리고 저는이 두 배열을 비교할 검색을 할 수 있는지 몰랐습니다. 감사. – Leoa

+0

나는 $ events_flat [$ event [0]] = $ event [1]과 약간 혼동스러워한다. $ event [2];'$ event [0]의 값은 $ event [1]의 값에 재 할당 (concation)됩니다. $ event [2] 코드에서 나중에 어떻게 값을 얻을 수 있습니까?'$ event = array_search ($ location. $ time, $ events_flat);'? array_search 문서에서 값이 아닌 인덱스를 생성합니다. 죄송 합니다만이 점에 대해서는 조금 혼란 스럽습니다. – Leoa

관련 문제