using the code:
$yourarray = $array('link' => 'text', 'link2' => 'text2');
foreach($yourArray as $key => $value) {
$keys[] = $key;
$items[] = $value;
echo $keys['link'];
}
In theory i thought this would work, however when you prin_r the keys seem to be numbers rather than link, link2 etc
Is thiere a way around this when i could pull the value from an array using the key?
thanks
Try this change and check:
$yourarray = array('link' => 'text', 'link2' => 'text2');
Your code consist of lots of typo
$yourarray = $array['link' => 'text', 'link2' => 'text2'];//This is not an array
$yourArray != $yourarray
foreach($yourArray as $key => $value) { // Undefined variable $yourArray
$keys[] = $key;
$items[] = $value;
echo $keys['link'];//it should be $keys[$key]
}
Working Code
$yourarray = ['link' => 'text', 'link2' => 'text2'];
foreach ($yourarray as $key => $value) {
$keys[$key] = $key;
$items[] = $value;
echo $keys[$key]."<br>";
}
print_r($keys);
print_r($items);
You are adding a variable to a standard non associative array when you use $keys[] = and $items[] =. Therefore you cannot reference an associative index with it. If you want to echo what was recently inserted into $keys, use:
echo end($keys);
You will not be able to grab any values from an array using a name index like you have, unless you assign it properly. To elaborate a little on that, when you use '$keys[] = ' you are saying add this value to the next integer index in this array (EG: if $keys has 2 values and you use '$keys[] = ' the following index reference would be $keys[2])