#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0

    Print_r array not quite like I want


    Hello everybody,

    I have a php script that generates an array with filenames. I want to echo/print these filenames, but when I do so, nor only the filenames get printed, but also the following:

    array( [0]=> [1]=> etc. )

    I am a total newbie in php, and I am allready glad that I converted the orignal script into something using an array, but I want just the filenames not the other stuff.

    What should I change? Thanks a lot in advance, I;m probably doing something very stupid here...

    here is the code:



    PHP Code:
    <?php
    $directory 
    'images/slideshow/';     
    try {        
        
    // Styling for images    
        
    echo "<div id=\"myslides\">";    
        
        
        foreach ( new 
    DirectoryIterator($directory) as $item ) {            
            if (
    $item->isFile()) {
                    
            
                        
    $path "\"".$directory.$item."\"";
                        
    $imgpath "<img src=".$path.">";
                        
    $imagearray[] = $imgpath;
        }}
            
    sort($imagearray);
            
    print_r($imagearray);
                
            
    //print_r($imagearray);
        
        
    echo "</div>";
    }    
    catch(
    Exception $e) {
        echo 
    'No images found for this slideshow.<br />';    
    }
    ?>
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0
    to be clear, this is what the code above shows:

    Array
    (
    [0] => <img src="images/slideshow/PICT1023.jpg">
    [1] => <img src="images/slideshow/PICT1024.jpg">
    [2] => <img src="images/slideshow/PICT1025.jpg">
    [3] => <img src="images/slideshow/PICT1026.jpg">
    [4] => <img src="images/slideshow/PICT1027.jpg">
    [5] => <img src="images/slideshow/PICT1028.jpg">
    [6] => <img src="images/slideshow/PICT1029.jpg">
    [7] => <img src="images/slideshow/PICT1030.jpg">
    [8] => <img src="images/slideshow/PICT1031.jpg">
    [9] => <img src="images/slideshow/PICT1032.jpg">
    [10] => <img src="images/slideshow/PICT1033.jpg">
    [11] => <img src="images/slideshow/PICT1034.jpg">
    [12] => <img src="images/slideshow/PICT1035.jpg">
    [13] => <img src="images/slideshow/PICT1036.jpg">
    [14] => <img src="images/slideshow/PICT1037.jpg">
    [15] => <img src="images/slideshow/PICT1038.jpg">
    )

    and I want just the image tags, not the word array and the () [] => stuff surrounding it ...
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,904
    Rep Power
    1045
    Hi,

    you should get used to the PHP manual. It clearly says that print_r() is a debugging function, which is used to analyze the content of a variable.

    What you want is something completely different. You want to generate an HTML list from an array. There is no "magical function" for this. You have to actually loop through the array and generate a <ul> or <ol> -- or if clean markup doesn't matter, simply end each name with a <br />.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    8
    Rep Power
    0
    Allright, that explains ...
    I found a lot of threads where print-r was suggested, and never any mentioning of that being a function to debug..
    I will try to make a loop with my two day php knowledge .. if anyone has suggestions on how to, I'd gladly hear about it
    Thanks
    !
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,904
    Rep Power
    1045
    Originally Posted by gametwodne
    I found a lot of threads where print-r was suggested, and never any mentioning of that being a function to debug..
    Yes. Much of the code you'll find on the Internet is plain nonsense and sometimes even dangerous, so it's really important to understand the PHP manual and use it for first-hand information. PHP is kind of infamous for bad code.

    I mean, as long as you're just playing around on your local computer, you don't need to write perfect code (and you can't, of course). But as soon as you want to, say, query a database and actually put the code online, you need to check the manual and not just rely on stuff you found somewhere.



    Originally Posted by gametwodne
    I will try to make a loop with my two day php knowledge .. if anyone has suggestions on how to, I'd gladly hear about it
    Well, just do what you already do in your other "foreach" loop. It's the exact same logic, only with different HTML elements.

IMN logo majestic logo threadwatch logo seochat tools logo