【问题标题】:How to compare nested array from collection and pure array?如何比较集合中的嵌套数组和纯数组?
【发布时间】:2020-11-18 15:37:53
【问题描述】:

比较两个数组后需要用标签更新帖子。如何在新数组中提取嵌套数组的“id”?

<pre>
//$post->tags->toArray() 
array:2 [▼
  0 => array:5 [▼
    "id" => 1
    "name" => "tag1"
    "created_at" => "2020-07-27T17:36:28.000000Z"
    "updated_at" => "2020-07-27T17:36:28.000000Z"
    "pivot" => array:2 [▶]
  ]
  1 => array:5 [▼
    "id" => 2
    "name" => "tag2"
    "created_at" => "2020-07-27T17:36:30.000000Z"
    "updated_at" => "2020-07-27T17:36:30.000000Z"
    "pivot" => array:2 [▶]
  ]
]

//Result of $request->tags
array:3 [▼
  0 => "1"
  1 => "2"
  2 => "3"
]

</pre>

【问题讨论】:

    标签: arrays laravel collections


    【解决方案1】:

    如果我理解正确的话,

    $tags = array();
    foreach($posts as $p)
    {
        array_push($tags, $p['id']);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      相关资源
      最近更新 更多