Unset a Value in a Multi-Dimensional Array Based on One of the Values

Delete element from multidimensional-array based on value

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);

Unset a value in a multi-dimensional array based on one of the values

I think this is what you want.

foreach ($_SESSION['cart_array'] as $key => $search)
{
if($pid == $search['item_id'])
{
echo $key;
}
}

Removing array from multidimensional array based off array values

It's not tested

foreach ($arr as $k => $item) {
if ($item['productID'] == $productID) {
unset($arr[$k]);
break;
}
}

If you want to restore indexes after removing item

$arr = array_values($arr);

How to remove value from Multidimensional Array based on Key and Value - PHP

So you know you are looking under the pecan key and you know you are looking for 288, so just search to return the key and unset():

$key = 'pecan';
$val = '288';

unset($array[0][$key][array_search($val, $array[0][$key])]);

To re-index:

$array[0][$key] = array_values($array[0][$key]);

Or to unwind it:

$found = array_search(288, $array[0]['pecan']);
unset($array[0]['pecan'][$found]);
$array[0]['pecan'] = array_values($array[0]['pecan']);

PHP remove value from multi dimensional array

foreach() made copy of elements. Then unseting the key is not enough, because you are destroying a local variable.

You could use references & in your foreach() loops and unset like :

 foreach ($arr as &$filters) {
foreach ($filters as &$filter) {
foreach ($filter as &$single_filter) {
foreach ($single_filter as $key => $value) {
if ($value == $remove) {
unset($single_filter[$key]);
}
}
}
}
}

Or using keys ($k1, $k2, ...) :

 foreach ($arr as $k1 => $filters) {
foreach ($filters as $k2 => $filter) {
foreach ($filter as $k3 => $single_filter) {
foreach ($single_filter as $key => $value) {
if ($value == $remove) {
unset($arr[$k1][$k2][$k3][$key]);
}
}
}
}
}

unset values null array multidimensional php

You should passing argument by reference.

private function arrayUnset( &$dataPayment )
{
foreach( $dataPayment as $key => $value )
{
if( is_array( $dataPayment[ $key ] ) )
{
$dataPayment[ $key ] = $this->arrayUnset($value);
}
if( $value === null || $value === "" )
{
unset( $dataPayment[ $key ] );
}
}
return $dataPayment;
}

Remove duplicates from a multi-dimensional array based on 2 values

Simple solution using foreach loop and array_values function:

$arr = array(
array("John","Smith","1"), array("Bob","Barker","2"),
array("Will","Smith","2"), array("Will","Smith","4")
);

$result = [];
foreach ($arr as $v) {
$k = $v[0] . $v[1]; // considering first 2 values as a unique key
if (!isset($result[$k])) $result[$k] = $v;
}

$result = array_values($result);
print_r($result);

The output:

Array
(
[0] => Array
(
[0] => John
[1] => Smith
[2] => 1
)

[1] => Array
(
[0] => Bob
[1] => Barker
[2] => 2
)

[2] => Array
(
[0] => Will
[1] => Smith
[2] => 2
)
)

Remove the first element from a multidimensional array with PHP

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]);

How to remove item from multidimensional, associative array in PHP

You want to recursively check it:

function removeItem(&$array, $menutype, $menuid)
{
foreach ($array as $key => &$value) {
if (isset($value['menutype'], $value['menuid']) && $value['menutype'] == $menutype && $value['menuid'] == $menuid) {
unset($array[$key]);
} elseif (isset($value['children']) && is_array($value['children'])) {
removeItem($value['children'], $menutype, $menuid);
}
}
}

removeItem($arr, 'posts_category', 13);

print_r($arr);


Related Topics



Leave a reply



Submit