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
Videos
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);
You can use array_shift for this:
while (($num = array_shift($arr)) !== NULL) {
// use $num
}
You might try using foreach/unset, instead of array_shift.
$array = array(0, 1, 2, 3, 4, 5);
foreach($array as $value)
{
// with each pass get the value
// use method to doSomethingWithValue($value);
echo $value;
// and then remove that from the array
unset($array[$value]);
}
//so at the end of 6 rounds the array will be empty
assert('empty($array) /* Array must be empty. */');
?>
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.
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]);
}
}
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);