Results 1 to 9 of 9

Thread: Array Values/Other Values

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default Array Values/Other Values

    Is there a way to output all values of a variable or request such as print_r in php? Thanks.
    Last edited by bluewalrus; 12-13-2010 at 12:59 AM.
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,074
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <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';
    }
    alert(r);
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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:

    Code:
    <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]);
    }
    </script>
    This will give you:

    name=Joe
    postcode=postcode
    tel=92721


    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

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,074
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    By no means exhaustive or perfect, this will do for a lot of things:

    Code:
    <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){
    			try{
    				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;
    }
    alert(print_r(false));
    var ar = ['Hello', ['one', 'World']];
    alert(print_r(ar));
    var obj = {everytime: function(){}, never: "I don't know"};
    alert(print_r(obj));
    alert(print_r(print_r));
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    bluewalrus (12-12-2010)

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,074
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    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.
    Last edited by jscheuer1; 12-12-2010 at 11:31 AM. Reason: clarity
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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).
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,074
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by djr33 View Post
    As far as I know, the exception is objects.
    The exception of what? From php.net's man page on print_r():

    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:

    PHP Code:
    $myFunc = function(){return whatever;}; 
    And that doing:

    PHP Code:
    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.
    Last edited by jscheuer1; 12-13-2010 at 12:08 AM. Reason: format - add detail
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    right, no anonymous functions in php. you have to do it in two steps:
    PHP Code:
    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)

    Edit:

    regarding making a function "into something that can be printed", you have to have (or write) a function that has a return value:
    PHP Code:
    function func_one(){ $var 'value'; }
    print 
    func_one();  // prints nothing (is NULL)

    function func_two(){ $var 'value'; return $var; }
    print 
    func_two(); // prints "value" 
    Last edited by traq; 12-13-2010 at 12:07 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •