2014-04-18 63 views
0

任何人都可以請幫助我,這...如何動態地向多維關聯數組插入值?

我要像

Array (
    [1st Apr 2014][0] => Array ([daylink] => 1st Apr 2014 [text] => Report of 1st Apr 2014 - View [link] => link1.html) 
    [2nd Apr 2014][0] => Array ([daylink] => 2nd Apr 2014 [text] => Report of 2nd Apr 2014 - View [link] => link2.html) 
    [3rd Apr 2014][0] => Array ([daylink] => 3rd Apr 2014 [text] => Report of 3rd Apr 2014 - View [link] => link2.html) 
    [4th Apr 2014][0] => Array ([daylink] => 4th Apr 2014 [text] => Report of 4th Apr 2014 - View [link] => link2.html) 
) 

所有日期「2014年4月1日」是一個數組$天[文字]和[鏈接]輸出內容是在foreach循環中生成的。

我的代碼是這樣的:

include('simple_html_dom.php'); 

$html = file_get_html('Link.html'); 

$today_date = $month_name = date('jS M Y'); 

$monday = date('jS M Y',strtotime('last monday')); 
$tuesday = date('jS M Y',strtotime('last tuesday')); 
$wednesday = date('jS M Y',strtotime('last wednesday')); 
$thursday = date('jS M Y',strtotime('last thursday')); 

$days = array($monday, $tuesday, $wednesday, $thursday); 

foreach ($days as $day) { 

$tds = $html->find('table',1)->find('span'); 

$num = 0; 
foreach($tds as $td){ 

    if (strpos($td->plaintext, $day) !== false){ 
      echo $day . "<br>"; 
      $next_td = $td->next_sibling(); 

      if (!empty($next_td)) 
      $linka = $next_td->find('a'); 
       foreach($linka as $elm) { 
       $num = $elm->href; 
       } 

       //echo $td->plaintext. "<br />"; 
       //echo $num . "<br />"; 
      $day_url = array($day => array(daylink => $day, text => $td->plaintext, link => $num)); 




     } 


    } 
} 

print_r($day_url); 

和link.html是這樣

<table> 
    <tbody> 
     <tr class="c0"> 
      <td class="c11"> 
       <td class="c8"> 
        <ul class="c2 lst-kix_h6z8amo254ry-0 start"> 
         <li class="c1"> 
          <span>1st Apr 2013 - </span> 
          <span>1st Apr 2014 - </span> 
          <span class="c6"> 
           <a class="c4" href="/link.html">View</a> 
          </span> 
          <span>1st Apr 2015 - </span> 
         </li> 
        </ul> 
       </td> 
      </td> 
     </tr> 
    </tbody> 
</table> 

回答

0

請試試這個:

$dayArray=array('1st Apr 2014','2nd Apr 2014','3rd Apr 2014','4th Apr 2014'); 
$resultArray=array(); 
$count=1; 
foreach($dayArray as $value){ 
    $resultArray[$value][]=array(
       'daylink' => $value, 
       'text'  => 'report for '.$value, 
       'link'  => 'link'.$count.'.html' 
      ); 

    $count++; 
} 

print_r($resultArray); 
+0

謝謝你這麼多它解決了我的問題。 .......... :) – Lipsa