3

我有一个数组,我想将它的两个字段值合并到一个字段中。需要 链接=链接+脚注

  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
            )
    )

需要输出

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);
4

3 回答 3

3
$array['link'] = array_merge(array($array['link']), $array['footnote']);
unset($array['footnote']);
print_r($array);
于 2013-08-04T12:09:53.533 回答
2

它应该像这样简单:

$array['footnote'][] = $array['link'];
$array['link'] = $array['footnote'];
unset($array['footnote']);
于 2013-08-04T12:10:22.357 回答
1
foreach($array as $key=>$value) {
  if ($key == 'link') {
    $array[$key] = $array['footnote'];
    $array[$key][] = $value;  
    unset($array['footnote']);
    continue;
  }
}
于 2013-08-04T12:07:27.847 回答