0

Am working on a Laravel application whereby I have an associative array that am to pass to an API endpoint, Before posting to the API, I want to delete the img key together with its value . I have tried to use unset function but it is not removing the img key

Array where I want to remove the image property

 $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"
  ]
];

Unset method

$array = $a;
unset($array['img']);

//dd($a);
1

2 Answers 2

2

You can do something like this,

foreach ($array as $key => &$value) { // & defines changes will be made @ value itself
    unset($value['img']);
}

And Yes, I don't understand why you initialised $a as $a[]?

Sign up to request clarification or add additional context in comments.

Comments

0
$newarray = array_filter($a, function($k) {
    return $k != 'img';
}, ARRAY_FILTER_USE_KEY);

and pass this new array

1 Comment

Are you sure about this? Will this work in the nested array that is given in the question?

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.