1

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"
      }
    }
10
  • 1
    We need to see more code here... Is the first code block inside a function? Can we see the function call? Looks like you have an array called $metas, and then suddenly another one called $twitter_posts. How did you get there? Commented Apr 14, 2016 at 23:26
  • return $twitter_posts[0]['metas']?? Where's this coming from? Commented Apr 14, 2016 at 23:27
  • @larsAnders ok i added the whole function. It's a method to parse S3 object metadata into an array along with the object's url for a laravel application Commented Apr 14, 2016 at 23:28
  • 1
    Can you post the exact var_dump of returned data? Your above result is not an array, it is a JSON string. It can not be the result of array_push. Commented Apr 14, 2016 at 23:30
  • 1
    It's json alright - you are best advised to use json_decode() to parse to a PHP data structure. Commented Apr 14, 2016 at 23:38

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

BOOM! we have a winner, thank you. Knew it was something like that
No problem. Do note the comments above about using json_decode() to parse JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.