View Full Version : Resolved Array Values/Other Values

12-11-2010, 05:41 PM
Is there a way to output all values of a variable or request such as print_r in php? Thanks.

12-11-2010, 05:59 PM
No. But the information can be gotten. It depends upon the type of object the variable refers to. A blanket function could be made to test that, with at least one caveat. It also depends upon how you want to see the result.

It would be easier to tell you how to get it for one type of object, where you already know the type. For an array:

<script type="text/javascript">
var ar = ['something', 'anotherthing'];
for(var r = 'array ar:\n', i = 0; i < ar.length; ++i){
r += i + ' is ' + ar[i] + '\n';

12-11-2010, 07:24 PM
If you're looking for something similar to an associative array in PHP. You can use a for loop to get all properties of an object. For example:

<script type="text/javascript">
var post = new Object;

post.name = 'Joe';
post.postcode = 'postcode';
post.tel = '92721';

for(i in post) {
alert(i + '=' + post[i]);

This will give you:


http://www.quirksmode.org/js/associative.html (http://www.quirksmode.org/js/associative.html) has a few good examples.

I found this method of doing things particularly useful for me, since I prefer associative arrays ;)

12-12-2010, 02:32 AM
By no means exhaustive or perfect, this will do for a lot of things:

<script type="text/javascript">
function print_r(obj){
function arrayFunc(){
var r = '', i = 0;
for(i; i < obj.length; ++i){
r += '[' + i + '] => ' + obj[i] + '\n';
return r + ')';
function objectFunc(){
var r = '', p;
for(p in obj){
r += '[' + p + '] => ' + obj[p] + '\n';
} catch(e){}
return r + ')';
var c = obj || obj === 0 || obj === false? obj.constructor : obj;
return c === Array? 'Array (\n' + arrayFunc():
c === String? 'String (\n"' + obj.valueOf() + '"\n)' :
c === Number? 'Number (\n' + obj.valueOf() +'\n)' :
c === Boolean? 'Boolean (\n' + obj + '\n)' :
c === Object? 'Object (\n' + objectFunc() :
c === Function? 'Function (\n' + obj + '\n)' : 'Object is ' + typeof obj;
var ar = ['Hello', ['one', 'World']];
var obj = {everytime: function(){}, never: "I don't know"};

12-12-2010, 02:57 AM
print_r() works for associative arrays in PHP, and basically considers numbered indices as associative keys of that number. I'm not sure if Javascript handles it differently, but what you've done in the first couple posts should cover it.
What you just wrote, John, is more like the var_dump() function in PHP that prints all the information contained in any variable, be it an object, array, whatever.

12-12-2010, 09:18 AM
What I've done is (as far as it goes, like I say - not perfect) almost exactly like PHP's print_r(), which does both associative arrays and those without keys, as well as objects, strings and numbers. It also does booleans - sort of - undocumented, 1 for true, an empty string for false. All I've really added are functions and a sort of catch all to at least try to give a type if javascript can't break down whatever is fed to this function using the function's methods. I only did those because javascript fairly easily can. I don't think PHP does functions at all in that manner. But I'm still just learning PHP, so I wouldn't want to make a negative claim. I can make a positive claim though as I have here about PHP's print_r() because it's all pretty much right there in the manual.

Note: There are no associative arrays in javascript. Well there are, but they're called and technically are objects and use the object syntax, so are constructed with the Object constructor.

12-12-2010, 05:35 PM
As far as I know, the exception is objects. I didn't mean that as a complaint, but just that you'd done a little more than is needed (at least if print_r() really is the goal). var_dump() which handles any type of variable is more useful (in general).

12-13-2010, 12:54 AM
As far as I know, the exception is objects.

The exception of what? From php.net's man page on print_r() (http://us.php.net/manual/en/function.print-r.php):

Return Values

If given a string, integer or float, the value itself will be printed. If given an array, values will be presented in a format that shows keys and elements. Similar notation is used for objects.

So it does objects (or claims to), and so does the javascript version. I think I have had some experience with PHP's print_r() where it didn't yield much useful information about a particular object. I believe that was because the object itself contained at least one nested object. In the javascript version that too will frustrate your efforts unless the javascript print_r() function is edited to recognize such a situation and deal with it, similar to walking down all descendant nodes in a DOM tree until there are no more child nodes.

I'm not sure how or if it's even possible to do that in PHP, though it probably is.

One thing I'm perhaps the least clear on with this concept in PHP is functions. I mean, it appears that you cannot do:

$myFunc = function(){return whatever;};

And that doing:

function someFunc(){whatever;}

doesn't seem to make someFunc into something that can be printed. It can be referenced and used in its scope, just not printed out. But I would want to do further testing on that before saying so definitively.

12-13-2010, 12:59 AM
right, no anonymous functions in php. you have to do it in two steps:

function whatever(){
$whatever = // do something
return $whatever;
$myFunc = whatever();
if you really want to "just do something", then you can just do it (procedurally; aside from being easier to read, there's no *real* advantage to defining a function in php if it's only used once)

regarding making a function "into something that can be printed", you have to have (or write) a function that has a return value:

function func_one(){ $var = 'value'; }
print func_one(); // prints nothing (is NULL)

function func_two(){ $var = 'value'; return $var; }
print func_two(); // prints "value"