简体   繁体   中英

php trouble with array key value pairs

I'm sure I'm missing something really simple but I'm trying to convert a feed into a nice clean associative array to reference in my views.

Here is the loop (with some text formatting to split them):

$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]);
        }

I get this result:

[{"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"}]

Which seems fine to me but either I'm retarded or I didn't format these nested arrays correctly.

This works:

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

When I try to get a specific variable by the key:

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

It teels me to %&$( off:

undefined index

EDIT: the entire function per request (maybe not relevant but here you go):

function call:

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

function:

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;
}

this call:

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

gets this result:

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"
      }
    }

Your arrays are nested one level too deep. Instead of array_push, you can use $array[$key] syntax to get the format you want, so instead of this:

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

you can do this:

$metas[$metaKey] = $metaVal;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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