The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Print_r array not quite like I want
Discuss Print_r array not quite like I want in the PHP Development forum on Dev Shed. Print_r array not quite like I want PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 13th, 2013, 10:00 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 1 h 18 m
Reputation 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 />';
}
?>
|

January 13th, 2013, 10:02 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 1 h 18 m
Reputation 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 ...
|

January 13th, 2013, 10:21 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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 />.
|

January 13th, 2013, 10:33 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 4
Time spent in forums: 1 h 18 m
Reputation 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
!
|

January 13th, 2013, 11:18 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | 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.
Quote: | 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|