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

时间:2014-10-22 14:12:11

标签: php arrays multidimensional-array

我有以下数组:

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"];

感谢!!!

1 个答案:

答案 0 :(得分:0)

你的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;

}