$niz = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'watermelon',
'fruit5' => 'grapefruit'
);
$max = 'yellow';
$niz2 = array();
$niz3 = array();
foreach($niz as $k => $v){
if (strlen($v) <= strlen($max)) {
array_push($niz2, $v);
}
else {
$niz3[$niz[$k]]=$v;
}
}
print_r($niz3);
How can I get the appropriate key from the $niz array in my $niz3 associative array in the else statement?
I.e. Array( [fruit4] => watermelon [fruit5] => grapefruit )
I get: Array ( [watermelon] => watermelon [grapefruit] => grapefruit )
You need to change $niz3[$niz[$k]]=$v;
to $niz3[$k]=$v;
,
$k
is the "key", by passiing it into $niz
you are accessing the value, which you have already defined as $v
.