将字符串附加到现有数组键? PHP

时间:2011-12-05 13:47:05

标签: php arrays

我想将$ teasers中的键附加到每个$ teasers元素中包含的数组的键,然后创建一个关联数组,其中包含其键的附加键和其内容的子数组的内容。以下?我希望如此,如果不是这里是我期望的最终产品的一个例子:

这是我正在使用的多维数组的简化版本:

$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;
    }
}

我非常乐意尽我所能来使事情更清楚或帮助你。感谢您提前的时间!!

1 个答案:

答案 0 :(得分:2)

我认为你的代码应该有效。在循环之前添加$data = array();