i'm trying to get individual values from an php foreach loop.
My code is
$foo = array('a' => 'apple', 'b' => 'ball', 'c' => 'coke');
foreach($foo as $key => $item) {
// Need to display only one array value any key like below,
// First entry Key : a
// First entry value: apple
}
Help me to do this ?
$foo = array('a' => 'apple', 'b' => 'ball', 'c' => 'coke');
foreach($foo as $key => $item) {
echo $key;
echo $item;
}
will output
a apple b ball c coke
or if you want the first item only, use
echo $foo['a'];
will output
apple
or to get the first array item without knowing the key
$keys = array_keys($foo);
$values = array_values($foo);
echo $keys[0];
echo $values[0];
I don't understand you only have to do
foreach($foo as $key => $item) {
echo $key;
echo $item;
}
for example
Use if
with in_array
. This will only print a
and apple
foreach($foo as $key => $item) {
if(!in_array($key,array('b','c'))) {
echo $key;
echo $item;
}
}
a
apple
I understand you're only interested in the first key and value.
First value:
echo current($foo);
First key:
echo key($foo);
foreach($data as $value)
if($value['email']==$value) {
$selected='selected';
} else {
$selected='';
}