1

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?

1
  • Please edit the dump from the endpoint because it is not correct. This is dump of $issues. Dump of the endpoint would be something like this: array (size=1) 'papers' => array (size=1) 0 => array (size=11) 'title' => string 'Mamma- 2014-10-22' (length=17) 'date' => string '2014-10-22' (length=10) 'expires' => string '' (length=0) 'catalog' => int 24 ... Commented Oct 19, 2016 at 8:48

2 Answers 2

3

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

3 Comments

I get Undefined index: papers for the first part of your answer, and for the second part I magazine->id is not inside of the papers array
Can you please provide var_dump of $issue right after $issue = json_decode($result, true);
array (size=1) 'papers' => array (size=1) 0 => array (size=11) 'title' => string 'Mamma- 2014-10-22' (length=17) 'date' => string '2014-10-22' (length=10) 'expires' => string '' (length=0) 'catalog' => int 24 ...
1

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

2 Comments

Then I get this kind of an array 0 => array:2 [▼ "magazineId" => 6 0 => array:1 [▼ "papers" => array:1 [▼ 0 => array:11 [▼ ...rest of the array ] ] ] I would like to put magazine id inside of papers array if possible
Hi @Marco; @krasipenkov's first code block may be fine for you. Have you tried it?

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.