So, I\'m working with PHP for the first time and I am trying to retrieve and display the values of an array. After a lot of googling, the only methods I can find for this ar
<?php $data = array('a'=>'apple','b'=>'banana','c'=>'orange');?>
<pre><?php print_r($data); ?></pre>
Result:
Array
(
[a] => apple
[b] => banana
[c] => orange
)
There is foreach loop in php. You have to traverse the array.
foreach($array as $key => $value)
{
echo $key." has the value". $value;
}
If you simply want to add commas between values, consider using implode
$string=implode(",",$array);
echo $string;
Other option:
$lijst=array(6,4,7,2,1,8,9,5,0,3);
for($i=0;$i<10;$i++){
echo $lijst[$i];
echo "<br>";
}
the join() function must work for you:
$array = array('apple','banana','ananas');
$string = join(',', $array);
echo $string;
Output :
apple,banana,ananas
Iterate over the array and do whatever you want with the individual values.
foreach ($array as $key => $value) {
echo $key . ' contains ' . $value . '<br/>';
}
use implode(',', $array);
for output as apple,banana,orange
Or
foreach($array as $key => $value)
{
echo $key." is ". $value;
}