1

i have a multiple array that looks like this :

$name_array = [
            ['name' => 'mike', 'number' => (int)$a],
            ['name' => 'lisa', 'number' => (int)$b],
            ['name' => 'michelle','number' => (int)$c],
];

now I want to remove all the keys where 'number' is 0

i tried it like this:

 foreach ( $name_array as $key => $val )
        {
            if ( $val['number'] == 0 )
            {
                unset($name_array[$key][$val['number']]);
            }
        }

but this just dont work... could someone help me with this?

$a and $c are == 0 - so I want to delete them

thanks for any help :)

Rizier123
  • 58,877
  • 16
  • 101
  • 156
ItzMe42
  • 211
  • 2
  • 9
  • this might help you http://stackoverflow.com/questions/2448964/php-how-to-remove-specific-element-from-an-array – Chetan Naik Jan 13 '16 at 10:09

5 Answers5

0

You are not overwriting the same error. use this code :

$a = 0;
$b = 200;
$c = 0;

$name_array = [
    ['name' => 'mike', 'number' => (int) $a],
    ['name' => 'lisa', 'number' => (int) $b],
    ['name' => 'michelle', 'number' => (int) $c],
];

for ($i=0; $i<count($name_array); $i++){
    if(!$name_array[$i]['number']){
        unset($name_array[$i]['number']);
    }
}
echo '<pre>';
print_R($name_array);

//Sample output

Array
(
    [0] => Array
        (
            [name] => mike
        )

    [1] => Array
        (
            [name] => lisa
            [number] => 200
        )

    [2] => Array
        (
            [name] => michelle
        )

)
Maha Dev
  • 3,915
  • 2
  • 31
  • 50
  • i dont know why but this wont work :o i think its about the $name_array[$i]['number'] --- the first [] is just filled with names - so why there shoult be the $i? – ItzMe42 Jan 13 '16 at 10:16
  • Because without using indexes, how will you override the keys or unset the keys? i think foreach doesn't work. – Maha Dev Jan 13 '16 at 10:18
  • Can you provide me the values of $a,$b,$c. so i can test my code again – Maha Dev Jan 13 '16 at 10:18
  • or maybe because of the count($name_array) - it shouldnt count anything in there? it should just search for any key where 'number' is 0 :o – ItzMe42 Jan 13 '16 at 10:19
  • $a = 0, $b= 20, $c= 0 ---- $a and $b should be removed cause there 'number' - index is 0 :) – ItzMe42 Jan 13 '16 at 10:20
  • Wth the values provided, my code is working perfect for me. – Maha Dev Jan 13 '16 at 10:21
  • well this worked better.. the strange thing is, it havent removed both 0 - it just removed the first one :o ( the orginal code have about 30 times a 0 and your code have removed 29 of it :o ) – ItzMe42 Jan 13 '16 at 10:26
  • i mean that $a and $c are both 0.. and your code just removed $a - but $c is still in the output :) – ItzMe42 Jan 13 '16 at 10:36
  • See inside my output code in the answer. it also removed the $c. i think you are not executing my code properly. Thanks – Maha Dev Jan 13 '16 at 10:38
0

try this code:

<?php 

$a = 0;
$b = 0;
$c = 10;
$name_array = [
    ['name' => 'mike', 'number' => (int) $a],
    ['name' => 'lisa', 'number' => (int) $b],
    ['name' => 'michelle', 'number' => (int) $c],
];

for ($i=0; $i<count($name_array); $i++){
    if($name_array[$i]['number']==0){
        //unset($name_array[$i]['number']);
        unset($name_array[$i]);
    }
}
echo '<pre>';
print_R($name_array);

?>
rdn87
  • 739
  • 5
  • 18
0

You were very nearly there with your original effort but the syntax to access the number element of the array was incorrect. This does work as you expect.

$a=0; 
$b=99;
$c=0;


$name_array = array(
    array( 'name' => 'mike',    'number' => (int)$a ),
    array( 'name' => 'lisa',    'number' => (int)$b ),
    array( 'name' => 'michelle','number' => (int)$c ),
);

foreach ( $name_array as $key => $val ) {
    if ( $val['number'] === 0 ) unset( $name_array[$key]['number'] );
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

Removing unwanted items from arrays sounds like a good use of array_filter.

$name_array = array_filter($name_array, function($x){
    return $x['number'] != 0;
});
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
-1

change

unset($key);

to

unset($name_array[$key]['number']);
dev87
  • 154
  • 1
  • 9