Ludwig
Ludwig

Reputation: 1811

Php add a key value to associative array in foreach loop

I am getting json data from an api endpoint, I would like to add a key value to an array that I get. This is my function:

        $magazines = Magazine::all();

        foreach ($magazines as $magazine) {
          $result = file_get_contents('http://customer.pages.com/?customer=' . $magazine->visio_link_prefix . '&action=latest');
          $issues[] = json_decode($result, true);
        }

        foreach ($issues as $issue) {
            Issue::create([
              ''
              'title' => $issue['papers'][0]['title'],
              'date' => $issue['papers'][0]['date'],
              'foldername' => $issue['papers'][0]['foldername'],
              'thumb' => $issue['papers'][0]['thumb'],
              'thumbmedium' => $issue['papers'][0]['thumbmedium'],
            ]);
        }

The array that I get from the endpoint looks like this:

array:24 [▼
  0 => array:1 [▼
    "papers" => array:1 [▼
      0 => array:11 [▼
        "title" => "News- 2014-10-22"
        "date" => "2014-10-22"
        "expires" => ""
        "catalog" => 24
        "foldername" => "News"
        "folder" => 4965
        "pages" => 132
        "sectionstarts" => "1"
        "sectioncount" => 1
        "thumb" => "www.customer.pages.com/news/24/teasers/small.jpg"
        "thumb_medium" => "www.customer.pages.com/news/24/teasers/medium.jpg"
      ]
    ]
  ]
  1 => array:1 [▶]
  2 => array:1 [▶]
  3 => array:1 [▶]
  4 => array:1 [▶]
  5 => array:1 [▶]
  6 => array:1 [▶]
  7 => array:1 [▶]
  8 => array:1 [▶]
  9 => array:1 [▶]
  10 => array:1 [▶]
  11 => array:1 [▶]
  12 => array:1 [▶]
  13 => array:1 [▶]
  14 => array:1 [▶]
  15 => array:1 [▶]
  16 => array:1 [▶]
  17 => array:1 [▶]
  18 => array:1 [▶]
  19 => array:1 [▶]
  20 => array:1 [▶]
  21 => array:1 [▶]
  22 => array:1 [▶]
  23 => array:1 [▶]
]

So, in my foreach loop I would like to add a key value pair 'magazineId' => $magazine->id to each of the above 'papers' arrays. So that later I can use $issue['papers'][0]['magazineId'] to get the value of the $magazine->id. Not sure how to do that?

Upvotes: 1

Views: 3860

Answers (2)

krasipenkov
krasipenkov

Reputation: 2029

This correction to yoiur code should do the trick:

    foreach ($magazines as $magazine) {
        $result = file_get_contents('http://customer.pages.com/?customer=' . $magazine->visio_link_prefix . '&action=latest');
        $issue = json_decode($result, true);

        foreach($issue['papers'] as $paperKey => $paper) {
            $issue['papers'][$paperKey]['magazineId'] = $magazine->id;
        }
        $issues[] = $issue;
    }

    foreach ($issues as $issue) {
        Issue::create([
          'magazineId' => $issue['papers'][0]['magazineId'],
          'title' => $issue['papers'][0]['title'],
          'date' => $issue['papers'][0]['date'],
          'foldername' => $issue['papers'][0]['foldername'],
          'thumb' => $issue['papers'][0]['thumb'],
          'thumbmedium' => $issue['papers'][0]['thumbmedium'],
        ]);
    }

And the second version where you have only one magazineId key for a single issue:

foreach ($magazines as $magazine) {
    $result = file_get_contents('http://customer.pages.com/?customer=' . $magazine->visio_link_prefix . '&action=latest');
    $issue = json_decode($result, true);
    $issue['magazineId'] = $magazine->id;
    $issues[] = $issue;
}

Upvotes: 3

vural
vural

Reputation: 391

Could you please try code below? In first foreach I added an array to $issue.

$magazines = Magazine::all();

foreach ($magazines as $magazine) {
   $result = file_get_contents('http://customer.pages.com/?customer=' . $magazine->visio_link_prefix . '&action=latest');
   $issues[] = array('magazine_id' => $magazine->id, 'result' => json_decode($result, true);
}

foreach ($issues as $issue) {
    Issue::create([
      'magazine_id' => $issue['magazine_id'],
      'title' => $issue['result']['papers'][0]['title'],
      'date' => $issue['result']['papers'][0]['date'],
      'foldername' => $issue['result']['papers'][0]['foldername'],
      'thumb' => $issue['result']['papers'][0]['thumb'],
      'thumbmedium' => $issue['result']['papers'][0]['thumbmedium'],
    ]);
}

Upvotes: 1

Related Questions