繁体   English   中英

PHP:展平数组以直接访问其键

[英]PHP: flattening array to access its keys directly

我有以下数组:

foreach($items as $item)
{   
    //data coming from a feed     
    $published_on = $item->pubDate;
    $title = (string)($item->title);
    $link = (string)$item->link;

    $arrayFeed [] = array(
    date("j", strtotime((string)$published_on))."-".date("m", strtotime((string)$published_on))."-".date("Y", strtotime((string)$published_on)) => 
    $link."|".$title);

}

该商店:

    $arrayFeed
    : array = 
      0: array = 
        21-10-2014: string = http://myweb.com/21102014/|This is title 21-10-2014
      1: array = 
        25-09-2014: string = http://myweb.com/25092014/|This is title 25-09-2014
     .
     .

我通过执行以下操作访问链接和标题信息:

$arrayFeed[0]["21-10-2014"];
$arrayFeed[1]["25-09-2014"];

我必须更改什么才能以这种方式访问​​数据?:

  $arrayFeed["21-10-2014"];
  $arrayFeed["25-09-2014"];

谢谢!!!

您的foreach基本上应该如下所示:

foreach($items as $item)
{   
    //data coming from a feed     
    $published_on = $item->pubDate;
    $title = (string)($item->title);
    $link = (string)$item->link;
    $date = date("j", strtotime((string)$published_on))."-".date("m", strtotime((string)$published_on))."-".date("Y", strtotime((string)$published_on));

    $arrayFeed[$date] = $link."|".$title;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM