Question

I'm simply looking to unset an array either within the foreach or not; I just don't seem to understand how I can get the keys and use them to unset the rows I want.

$people['Australian'][] = array('name' => 'Jack', 'cash' => 40800);
$people['Australian'][] = array('name' => 'Bob', 'cash' => 575000);
$people['Australian'][] = array('name' => 'Joey', 'cash' => 200);
$people['Australian'][] = array('name' => 'Max', 'cash' => 8000);
$people['Australian'][] = array('name' => 'Robert', 'cash' => 100000);
$people['Italian'][] = array('name' => 'Gregorio', 'cash' => 22000);
$people['Italian'][] = array('name' => 'Nicola', 'cash' => 4000);
$people['Italian'][] = array('name' => 'Melania', 'cash' => 6200);
$people['Italian'][] = array('name' => 'Filippa', 'cash' => 52500);
$people['Japanese'][] = array('name' => 'Optimus Prime', 'cash' => 57331);


foreach($people as $country)
{
    foreach($country as $person)
    {
        if($person['cash']<50000)
        {
            //This seems to unset the variable used by the foreach loop.
            unset($person);
        }
    }
}
Was it helpful?

Solution

Try this one:

foreach($people as $peopleKey => $country)
{
    foreach($country as $countryKey => $person)
    {
        if($person['cash']<50000)
        {
            //This seems to unset the variable used by the foreach loop.
            unset($people[$peopleKey][$countryKey ]);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top