Php在foreach循环中为关联数组添加一个键值

时间:2016-10-19 07:46:06

标签: php arrays json

我从api端点获取json数据,我想将一个键值添加到我得到的数组中。这是我的功能:

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

我从端点获取的数组如下所示:

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 [▶]
]

因此,在我的foreach循环中,我想为每个上面的'magazineId' => $magazine->id数组添加一个键值对'papers'。以后我可以使用$issue['papers'][0]['magazineId']来获取$magazine->id的值。不知道怎么做?

2 个答案:

答案 0 :(得分:3)

对yoiur代码的这种修正应该可以解决问题:

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

第二个版本只有一个magazineId密钥用于单个问题:

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

答案 1 :(得分:1)

你可以试试下面的代码吗?在第一个foreach中,我添加了一个数组到$ 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'],
    ]);
}