If you want to remove the first items in the array for the key name Provinces and the numerical keys do not have to be preserved, you could also use array_splice:
$arr = [
"Country" => "Canada",
"Provinces" => [
"Quebec",
"Ontario",
"British Columbia"
]
];
array_splice($arr["Provinces"], 0, 1);
Php demo
Or using unset to keep the numerical keys:
unset($arr['Provinces'][0]);
Answer from The fourth bird on Stack OverflowIf you want to remove the first items in the array for the key name Provinces and the numerical keys do not have to be preserved, you could also use array_splice:
$arr = [
"Country" => "Canada",
"Provinces" => [
"Quebec",
"Ontario",
"British Columbia"
]
];
array_splice($arr["Provinces"], 0, 1);
Php demo
Or using unset to keep the numerical keys:
unset($arr['Provinces'][0]);
You can do that with unset():
unset( $myArray['Cities'][0] )
https://www.php.net/manual/en/function.unset.php
In case you do not know what the first item's key is:
// Make sure to reset the array's current index
reset($array);
$key = key($array);
unset($array[$key]);
$array=array("a"=>"123","b"=>"234","c"=>"345");
unset($array["a"]) ;
var_dump($array) ;
Also, what version of PHP do you use?
array_shift works fine for me with string-indexed arrays and I get the expected result.
Try this:
function removeElementWithValue($array, $key, $value){
foreach ($array as $subKey => $subArray) {
if ($subArray[$key] == $value) {
unset($array[$subKey]);
}
}
return $array;
}
Then you would call it like this:
$array = removeElementWithValue($array, "year", 2011);
Try this:
function remove_element_by_value($arr, $val) {
$return = array();
foreach($arr as $k => $v) {
if(is_array($v)) {
$return[$k] = remove_element_by_value($v, $val); //recursion
continue;
}
if($v == $val) continue;
$return[$k] = $v;
}
return $return;
}
Use array_shift
$new_array = array_shift($array);
This is a bit trivial. Putting answer in different format than original answer above to not hide logic of what needs to happen behind a function call (though other answer is totally valid).
$new_array = $old_array[0];
var_dump($new_array);
At it's simplest (I don't have time for a non loop one):
foreach($array as $k1 => $inner) {
foreach($inner as $k2 => $value) {
unset($array[$k1][$k2][0]);
}
}
This is terrible but should work for you. It uses the level 2 key as a value of the element to delete on the next level
$array = array(
array('A' => array('A', 100, 200)),
array('B' => array('B', 100, 200)),
);
foreach($array as $key => $subarray){
foreach($subarray as $letter_key => $target_array){
$index_to_delete = array_search($letter_key, $target_array);
unset($array[$key][$letter_key][$index_to_delete]);
}
}
print_r($array);
The following code will do what you want:
<?php
$a = 1;
$b = 2;
$c = 3;
$d = 4;
$arr = array(
array ( $b, $d, $c, $a, $b),
array ($c, $a),
array ( $b, $d, $c ),
array( $c, $d, $a, $b, $b)
);
echo "before:\n";
print_r($arr);
foreach($arr as $k1=>$q) {
foreach($q as $k2=>$r) {
if($r == $c) {
unset($arr[$k1][$k2]);
}
}
}
echo "after:\n";
print_r($arr);
?>
Output:
before:
Array
(
[0] => Array
(
[0] => 2
[1] => 4
[2] => 3
[3] => 1
[4] => 2
)
[1] => Array
(
[0] => 3
[1] => 1
)
[2] => Array
(
[0] => 2
[1] => 4
[2] => 3
)
[3] => Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 2
[4] => 2
)
)
after:
Array
(
[0] => Array
(
[0] => 2
[1] => 4
[3] => 1
[4] => 2
)
[1] => Array
(
[1] => 1
)
[2] => Array
(
[0] => 2
[1] => 4
)
[3] => Array
(
[1] => 4
[2] => 1
[3] => 2
[4] => 2
)
)
As you can see, all the 3's have gone...
Search the value in the sub array then unset it.
$search = 'c';
$result = array_map(function ($value) use ($search) {
if(($key = array_search($search, $value)) !== false) {
unset($value[$key]);
}
return $value;
}, $your_array);
Or you could use a loop too:
// this way change your original array
foreach ($your_array as &$sub_array) {
if(($key = array_search($search, $sub_array)) !== false) {
unset($sub_array[$key]);
}
}
var_dump($your_array);
foreach ($_SESSION["addedToCart"] as &$arr)
& turns your variable into a reference instead of a copy. Normally this would be sufficient. unset() only works on data within the current scope (so your foreach loop) leaving the original unchanged (See unset() for details).
Instead you can do:
foreach ($_SESSION["addedToCart"] as $key => $val)
{
if ($val["stoeltjes"] == $stoeltje && $val['film_id'] == $id) {
unset($_SESSION["addedToCart"][$key]);
}
}
Even if the suggested way with the reference should work normally, here's an example without it:
foreach ($_SESSION["addedToCart"] as $key => $arr) {
if ($arr["stoeltjes"] == $stoeltje && $arr['film_id'] == $id) {
unset($_SESSION["addedToCart"][$key]);
}
}