I have an array that looks like this..
$breadcrumbs = [
['Home', 'homepage.html'],
['About', 'aboutpage.html'],
['Contact', 'contactpage.html']
];
$remove = ['Home'];
I am trying to use array_diff()
to remove the Home
entry from $breadcrumbs
like this:
return ( array_diff($breadcrumbs, $remove) );
It is not working and giving me "Array to string conversion" errors, where am I going wrong?
Using a bit of array gymnastics, you can convert the breadcrumbs array into an associative array, keyed by the first item, then use array_diff_keys()
with the $remove
array flipped (convert the values to keys).
$breadcrumbs = array_column($breadcrumbs, null, 0);
$breadcrumbs = array_diff_key($breadcrumbs, array_flip($remove));
print_r($breadcrumbs);
If you want the end array to be a 0 based array without string keys, add
$breadcrumbs = array_values($breadcrumbs);