Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display array values in PHP

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.

like image 904
Thomas Avatar asked Apr 15 '11 05:04

Thomas


People also ask

How display all array values in PHP?

To display array structure and values in PHP, we can use two functions. We can use var_dump() or print_r() to display the values of an array in human-readable format or to see the output value of the program array.

What is array_keys () used for in PHP?

The array_keys() function returns an array containing the keys.


1 Answers

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; 
like image 109
Shakti Singh Avatar answered Sep 30 '22 18:09

Shakti Singh