合并数组的两个字段值

时间:2013-08-04 11:59:24

标签: php arrays

我有一个数组,我想在一个字段中合并它的两个字段值。 需要 link = link + footnote

  Array
    (
        [title] => CBS Blocks Time
        [link] => http://techcrunch.com/2013/08/02/cbs-blocks-time-warner-cable-subscribers-from-watching-full-episodes-on-cbs-com/
        [pubDate] => Fri, 02 Aug 2013 00:00:00 +0000
        [dc_creator] => Ryan Lawler
        [dc_language] => en
        [dc_format] => text/html
        [footnote] => Array
            (
                [0] => http://www.twcableuntangled.com/2013/08/twc-removes-cbs-programming/
                [1] => https://twitter.com/CBS
                [2] => https://twitter.com/TWC
                [3] => http://CBS.com
                [4] => https://twitter.com/TWC
                [5] => http://twitter.com/#!/brianstelter/status/363435685249687552
                [6] => http://www.techmeme.com/101016/p12#a101016p12
            )
    )

需要OutPut

Array
(
    [title] => CBS Blocks Time
    [link] => Array
        (
            [0] => http://techcrunch.com/2013/08/02/cbs-blocks-time-warner-cable-subscribers-from-watching-full-episodes-on-cbs-com/
            [1] => http://www.twcableuntangled.com/2013/08/twc-removes-cbs-programming/
            [2] => https://twitter.com/CBS
            [3] => https://twitter.com/TWC
            [4] => http://CBS.com
            [5] => https://twitter.com/TWC
            [6] => http://twitter.com/#!/brianstelter/status/363435685249687552
            [7] => http://www.techmeme.com/101016/p12#a101016p12
        )
    [pubDate] => Fri, 02 Aug 2013 00:00:00 +0000
    [dc_creator] => Ryan Lawler
    [dc_language] => en
    [dc_format] => text/html
)

这是我的方法

    $result = array();
foreach($item as $val){
    foreach($item['footnote'] as $val1){
        $result['link'] = $val1;
    }
}
pr($result);

3 个答案:

答案 0 :(得分:3)

$array['link'] = array_merge(array($array['link']), $array['footnote']);
unset($array['footnote']);
print_r($array);

答案 1 :(得分:2)

应该这么简单:

$array['footnote'][] = $array['link'];
$array['link'] = $array['footnote'];
unset($array['footnote']);

答案 2 :(得分:1)

foreach($array as $key=>$value) {
  if ($key == 'link') {
    $array[$key] = $array['footnote'];
    $array[$key][] = $value;  
    unset($array['footnote']);
    continue;
  }
}