php数组键值对的麻烦

时间:2016-04-14 23:19:57

标签: php arrays

我确定我错过了一些非常简单的内容,但我尝试将Feed转换为一个漂亮干净的关联数组,以便在我的视图中引用。

这是循环(用一些文本格式来分割它们):

$headers = get_headers('https://s3-us-west-1.amazonaws.com/sg-retail/' . $file);
        $metas = [];

foreach($headers as $meta) {
            $metaKey = strtok($meta, ':');
            $metaVal = ltrim(strstr($meta, ':'), ':');
            array_push($metas, [$metaKey => $metaVal]);
        }

我得到了这个结果:

[{"HTTP\/1.1 200 OK":""},{"x-amz-id-2":" sNsiGT+p8eZaFJ3RxHKLe\/vN4BfJ27Zp6baI+OvXr+9VqSosNfpSfj73b0XnQAEXKsNgTzBSaM4="},{"x-amz-request-id":" 50EE32CE562BDBE1"},{"Date":" Thu, 14 Apr 2016 23:15:03 GMT"},{"x-amz-meta-featured":" Featured Style: DR349"},{"x-amz-meta-postcopy":" Choose a ring as unique as the woman wearing it, with help from @SimonGJewelry!"},{"x-amz-meta-title":" Friday, April 1"},{"x-amz-meta-hashtags":" #Ring #Jewelry #JewelryGram #EngagementRing #Style #Diamonds #HeAsked #SheSaidYes #Love #Wedding #WeddingInspo #SimonG #SimonGJewelry"},{"Last-Modified":" Thu, 14 Apr 2016 18:55:03 GMT"},{"ETag":" \"7042f7d9383e180d9ed8516d2df0428f\""},{"Accept-Ranges":" bytes"},{"Content-Type":" image\/jpeg"},{"Content-Length":" 499591"},{"Server":" AmazonS3"},{"Connection":" close"}]

这对我来说似乎很好,但要么我迟钝了,要么我没有正确格式化这些嵌套数组。

这有效:

 return $twitter_posts[0]["metas"];

当我尝试通过键获取特定变量时:

 return $twitter_posts[0]["metas"]["x-amz-meta-postcopy"];

它让我想到%& $(关闭:

undefined index

编辑:每个请求的整个功能(可能不相关,但在这里你去):

函数调用:

$twitter_posts = \App\Asset::fetch('toolkit/social-media/twitter/post-images/');

功能:

public static function fetch($path)
{
    $files = Storage::files($path);
    $data = [];

    foreach($files as $file) {
        $headers = get_headers('https://s3-us-west-1.amazonaws.com/sg-retail/' . $file);
        $metas = [];

        foreach($headers as $meta) {
            $metaKey = strtok($meta, ':');
            $metaVal = ltrim(strstr($meta, ':'), ':');
            array_push($metas, [$metaKey => $metaVal]);
        }           

        $array = ['image' => 'https://s3-us-west-1.amazonaws.com/sg-retail/' . $file, 'metas' => $metas];
        array_push($data, $array);
    }

    return $data;
}

这个电话:

return var_dump($twitter_posts[0]["metas"]);

得到这个结果:

array(15) {
      [0]=>
      array(1) {
        ["HTTP/1.1 200 OK"]=>
        string(0) ""
      }
      [1]=>
      array(1) {
        ["x-amz-id-2"]=>
        string(77) " cJgthWyhsfIdX5zgNAmS6fp05iYv7gKt4dhThGtItV5QPv5MgLxYsRCfQ8uEwwuWmsSTWSULE5c="
      }
      [2]=>
      array(1) {
        ["x-amz-request-id"]=>
        string(17) " 2C043CB5EDF8F423"
      }
      [3]=>
      array(1) {
        ["Date"]=>
        string(30) " Thu, 14 Apr 2016 23:33:38 GMT"
      }
      [4]=>
      array(1) {
        ["x-amz-meta-featured"]=>
        string(22) " Featured Style: DR349"
      }
      [5]=>
      array(1) {
        ["x-amz-meta-postcopy"]=>
        string(80) " Choose a ring as unique as the woman wearing it, with help from @SimonGJewelry!"
      }
      [6]=>
      array(1) {
        ["x-amz-meta-title"]=>
        string(16) " Friday, April 1"
      }
      [7]=>
      array(1) {
        ["x-amz-meta-hashtags"]=>
        string(134) " #Ring #Jewelry #JewelryGram #EngagementRing #Style #Diamonds #HeAsked #SheSaidYes #Love #Wedding #WeddingInspo #SimonG #SimonGJewelry"
      }
      [8]=>
      array(1) {
        ["Last-Modified"]=>
        string(30) " Thu, 14 Apr 2016 18:55:03 GMT"
      }
      [9]=>
      array(1) {
        ["ETag"]=>
        string(35) " "7042f7d9383e180d9ed8516d2df0428f""
      }
      [10]=>
      array(1) {
        ["Accept-Ranges"]=>
        string(6) " bytes"
      }
      [11]=>
      array(1) {
        ["Content-Type"]=>
        string(11) " image/jpeg"
      }
      [12]=>
      array(1) {
        ["Content-Length"]=>
        string(7) " 499591"
      }
      [13]=>
      array(1) {
        ["Server"]=>
        string(9) " AmazonS3"
      }
      [14]=>
      array(1) {
        ["Connection"]=>
        string(6) " close"
      }
    }

1 个答案:

答案 0 :(得分:2)

您的数组嵌套了一层太深。您可以使用$array[$key]语法来获取所需的格式,而不是array_push,而不是:

array_push($metas, [$metaKey => $metaVal]);

你可以这样做:

$metas[$metaKey] = $metaVal;