2011-12-05 67 views
2

我想將$ teasers中的鍵值追加到每個$ teasers元素中包含的數組的鍵值上,然後創建一個包含其鍵值的附加鍵和其內容的子數組內容的關聯數組。以下?我希望如此,如果不是這是我希望的最終產品的例子:將字符串追加到退出數組鍵? PHP

$teasers[0]=array('id'  => 4, 
        'title' => 'How to determine the speed of an african swallow', 
        'content'=> 'Its really quite simple...') 

$teasers[1]=array('id'  => 5, 
        'title' => 'Man alleged to be cereal killer after breakfast', 
        'content' => 'Turns out eating a delicious bowl of froste...') 

我正在努力把它變成這樣:

這是在多維數組的簡化版本,我的工作:

$data Array(
      '/*Key from subarray+$teasers key*/' => '/*content from subarray*/', 
      'id0'  => '4', 
      'title0' => 'How to determine the speed of an african swallow', 
      'content0' => 'Its really quite simple...', 
      'id1'  => '5', 
      'title1' => 'Man alleged to be cereal killer after breakfast', 
      'content1' => 'Turns out eating a delicious bowl of frosted...') 

還沒有關注?我不怪你。也許這會有所幫助。我原本是這樣做的,直到我意識到你不能將一個變量傳遞給foreach循環。 ):

foreach ($teasers as $key => $blogentry) { 

    foreach($blogentry as $entrykey => $content){ 

     //Basically I would like to append $key to the end of $entrykey 
     //so that I can essentially access all of the data from one array($data[]) 

     $data[$entrykey.$key] = $content; 
    } 
} 

我非常樂意盡一切努力使事情變得更加清晰,或幫助您幫助我。提前感謝您的時間!

+2

'我最初是這樣想的,直到我意識到你不能將一個變量傳遞給foreach循環。 ):'??? – ajreal

+1

你的代碼工作正常「原樣」 – anubhava

+0

哦,對不起人們,IDK之前我正在做的事情是讓我看起來好像我無法通過$ key進入下一個循環。看完回覆後,我只是複製粘貼我在這裏和POOF它的作品。我的大腦很脆弱。我對節目的世界非常陌生,並且對此付出了代價。 我應該刪除這篇文章嗎? – Austin

回答

2

我認爲你的代碼應該可以工作。在循環之前添加$data = array();