46

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 are print_r, var_dump or var_export. However, all of these methods return something that looks like this:

[a] => apple
[b] => banana
[c] => orange

I can't figure out how to style this outout. I need to strip away the [a] => part and add commas. I know this must be a pretty straightforward process but I haven't been able to track down any documentation that demonstrates how to do it.

aksu
  • 5,221
  • 5
  • 24
  • 39
Thomas
  • 5,030
  • 20
  • 67
  • 100

9 Answers9

81

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;
Daze
  • 478
  • 5
  • 17
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
20

You can use implode to return your array with a string separator.

$withComma = implode(",", $array);

echo $withComma;
// Will display apple,banana,orange
j_freyre
  • 4,623
  • 2
  • 30
  • 47
14
<?php $data = array('a'=>'apple','b'=>'banana','c'=>'orange');?>
<pre><?php print_r($data); ?></pre>

Result:
Array
(
          [a] => apple
          [b] => banana
          [c] => orange
)


Sorry for the above, I read it too quickly and misunderstood.

<?php
$fruites = array('apple', 'banana', 'orange');
echo implode(',',$fruites);
?>

Result:
apple,banana,orange

Atomix
  • 172
  • 1
  • 4
  • 2
    This result is exactly what he doesn't want, he wants to output the plain values without array formatting. – Ken Oct 06 '20 at 12:03
  • Sorry, I read it wrong. So I didn't answer correctly as he asked. I fixed it now. – Atomix Feb 14 '22 at 07:11
6

the join() function must work for you:

$array = array('apple','banana','ananas');
$string = join(',', $array);
echo $string;

Output :

apple,banana,ananas

JazZ
  • 4,469
  • 2
  • 20
  • 40
5

a simple code snippet that i prepared, hope it will be usefull for you;

$ages = array("Kerem"=>"35","Ahmet"=>"65","Talip"=>"62","Kamil"=>"60");

reset($ages);

for ($i=0; $i < count($ages); $i++){
echo "Key : " . key($ages) . " Value : " . current($ages) . "<br>";
next($ages);
}
reset($ages);
4

use implode(',', $array); for output as apple,banana,orange

Or

foreach($array as $key => $value)
{
   echo $key." is ". $value;
}
Gaurav
  • 28,447
  • 8
  • 50
  • 80
3

Iterate over the array and do whatever you want with the individual values.

foreach ($array as $key => $value) {
    echo $key . ' contains ' . $value . '<br/>';
}
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
2

you can easily use join()

$fruits = array("apple", "banana", "orange");
print join(" ".$fruits);
  • and you can replace the space by a comma if you want (notice `join` is an alias of `implode`) – Asenar Jan 28 '14 at 10:13
2

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>";
}