从PHP中的嵌套数组中删除元素

我在Laravel应用程序上工作,因此我有一个关联数组要传递给API端点。在发布到API之前,我想删除img键及其值。我尝试使用未设置的功能,但未删除img键


我要删除图像属性的数组


 $a[] = [

  0 => array:4 [

    "name" => "Martoo nnn"

    "relationship" => "Spouse"

    "dob" => "2001-02-03"

    "img" => "img.png"

  ]

  1 => array:4 [

    "name" => "sdsdsd sdsdsd"

    "relationship" => "Child"

    "dob" => "2019-04-04"

    "img" => "img1.png"

  ]

  2 => array:4 [

    "name" => "sdsdsd sddds"

    "relationship" => "Child"

    "dob" => "2019-04-05"

    "img" => "img2.png"

  ]

  3 => array:4 [

    "name" => "dssdsd dsdsd"

    "relationship" => "Child"

    "dob" => "2019-04-02"

    "img" => "img3.png"

  ]

  4 => array:4 [

    "name" => "dssdsd dssdsd"

    "relationship" => "Child"

    "dob" => "2019-04-04"

    "img" => "img4.png"

  ]

];

取消设定方法


$array = $a;

unset($array['img']);


//dd($a);


富国沪深
浏览 174回答 2
2回答

宝慕林4294392

你可以做这样的事情,foreach ($array as $key => &$value) { // & defines changes will be made @ value itself    unset($value['img']);}是的,我不明白您为什么要初始化$a为$a[]?

人到中年有点甜

$newarray = array_filter($a, function($k) {    return $k != 'img';}, ARRAY_FILTER_USE_KEY);并传递这个新数组
打开App,查看更多内容
随时随地看视频慕课网APP