0

I need a clarification regarding the difference between data objects and arrays in PHP+mySQL and something I think is related: representing "array-type data" with { } vs [ ]

I am using PHP, mySQL and RedBean ORM.


I pulled out a record (table row) from a table, and stored it in PHP in a variable named $record

When I run the following command:

echo $record;

I get the following output:

{"id":"1","name":"Frankie Johnnie & Luigo Too","address":"939 W El Camino Real, Mountain View, CA","lat":"37.386429","lng":"-122.085838","fee":"0.00"}


Next, I pulled out column names from a table and stored them in a variable named $fields

When I run the following command:

echo $fields;

I get the following output:

Notice: Array to string conversion in /Applications/XAMPP/xamppfiles/htdocs/devserver/crud/show.php on line 18 Array

When I run the following command:

print_r($fields);

I get the following output:

Array ( [id] => int(11) [name] => varchar(60) [address] => varchar(80) [lat] => float(10,6) [lng] => float(10,6) [fee] => decimal(5,2) )


Why does echo work for the first, but print_r is needed for the second? What do I need to learn to brush up on what's happening here?

thanks_in_advance
  • 2,603
  • 6
  • 28
  • 44
  • possible duplicate of [What's the difference between echo, print, and print\_r in PHP?](http://stackoverflow.com/questions/1647322/whats-the-difference-between-echo-print-and-print-r-in-php) – Abhik Chakraborty Mar 10 '15 at 19:17
  • 3
    $record is a json_encoded object; $fields is an array – Mark Baker Mar 10 '15 at 19:18
  • 3
    the first return is a json string, the second is an array, echo doesn't work on an array, but echo does on a json string, since its a valid string, on $record, do a json_decode($record,true);, it'll now be an array just as your second return :) –  Mar 10 '15 at 19:18

1 Answers1

0

Koen Hoejimakers (and Mark Baker) answered the question correctly in the comments:

"The first return is a json string, the second is an array, echo doesn't work on an array, but echo does on a json string, since its a valid string, on $record, do a json_decode($record,true);, it'll now be an array just as your second return :) – Koen Hoeijmakers"

I will mark this as correct when SO lets me in a couple of days.

thanks_in_advance
  • 2,603
  • 6
  • 28
  • 44