 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
04-10-2009, 11:50 AM
|
#1 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
arrays......
Is there an easy way to print the items of an array without using print_r or var_dump and without using any loops?
I don't want to show key to value relationship like var_dump and print_r shows.... just the items in the array. And no loops...
For example:
$array = array(1,2,3,4,5);
Let's say there's a function called: print_array
Then I would do this:
print_array($array);
And it would spit out: 1 2 3 4 5
I checked out PHP.net but didn't see anything under the print_r function...maybe one of you guys know of some other function?
|
|
|
|
04-10-2009, 12:31 PM
|
#2 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
I just realized I can just make my own function to do this, so I think I will do just that.
But if anyone knows of any quick function call that can do this let me know because if PHP has it already built in, there's no need to reinvent the wheel....
|
|
|
|
04-10-2009, 03:08 PM
|
#3 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
There might be a premade function for it, but a simple for each loop would do the trick. Its hardly a wheel to reinvent.
|
|
|
|
04-10-2009, 03:10 PM
|
#4 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Village Idiot
There might be a premade function for it, but a simple for each loop would do the trick. Its hardly a wheel to reinvent.
|
right, I know, I want to avoid any loops, I'm working on a pagination script and I have an idea about how to do something but I didn't want to use loops because I wanted to keep it clean and straightforward.
In any event, I'll have to just make my own function that should be able to spit it right out the way I wanted...
|
|
|
|
04-10-2009, 04:07 PM
|
#5 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
PHP Code:
function print_array($array) {
foreach($array as $element) {
echo $element . ' ';
}
}
$array = array(1, 2, 3, 4, 5, 6); print_array($array); //output: 1 2 3 4 5 6
__________________
|
|
|
|
04-10-2009, 04:10 PM
|
#6 (permalink)
|
|
The Acquainted
Join Date: Jul 2005
Location: UK
Posts: 121
Thanks: 41
|
print_r($array);
or
<pre><?php print_r($array); ?></pre>
:)
Edit: Sorry didn't read your post properly!
__________________
PS3 Forums on GameSlurp - the site for gaming fans!
|
|
|
|
04-10-2009, 04:17 PM
|
#7 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
although i don't see why a simple foreach would be a problem, you could do something like this:
PHP Code:
array_walk($array, create_function('$val', 'echo $val, \'<br />\';'));
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
04-10-2009, 04:19 PM
|
#8 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Tanax
PHP Code:
function print_array($array)
{
foreach($array as $element)
{
echo $element . ' ';
}
}
$array = array(1, 2, 3, 4, 5, 6);
print_array($array); //output: 1 2 3 4 5 6
|
lol, that is exactly what I was looking to do, hoping PHP had a simple built in function but guess not. I will need to incorporate this into my paginate function, then you'll see what I was trying to do...
Quote:
Originally Posted by Brook
print_r($array);
or
<pre><?php print_r($array); ?></pre>
:)
|
No, print_r and var_dump provides a very un-elegant printout.
Look at tanax's example, that is what I was looking for but wanted to make sure that PHP didn't have something like this already built in..
|
|
|
|
04-10-2009, 04:21 PM
|
#9 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by sketchMedia
although i don't see why a simple foreach would be a problem, you could do something like this:
PHP Code:
array_walk($array, create_function('$val', 'echo $val, \'<br />\';'));
|
I know, wait till I finish my pagination code and you guys will see why I was looking for a one function, quick straight simple printout...
It's not just simply using a small simple loop to print it out, it's the way in which I would like to use it...
|
|
|
|
04-10-2009, 10:58 PM
|
#10 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
PHP Code:
echo implode(' ', $array);
|
|
|
|
04-11-2009, 12:01 AM
|
#11 (permalink)
|
|
how quixotic are you?
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
|
Quote:
PHP Code:
echo implode(' ', $array);
|
Darn, you beat me to it! lol
|
|
|
|
04-11-2009, 12:07 AM
|
#12 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by ETbyrne
Darn, you beat me to it! lol
|
I know....he's very very good....

|
|
|
|
04-11-2009, 10:14 AM
|
#13 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by Salathe
PHP Code:
echo implode(' ', $array);
|
Though making a custom function with a foreach loop provides more functionality for future extensions, this will work quite alright with what he wanted.
__________________
|
|
|
|
04-11-2009, 01:06 PM
|
#14 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Why would you want to extend echoing an array's values, with more functionality?!
|
|
|
|
04-12-2009, 12:09 AM
|
#15 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by Salathe
Why would you want to extend echoing an array's values, with more functionality?!
|
I don't know.
For instance, he might want to run an if-statement to check something on each element. Add something around each element. Well.. you get the point. But since he just wanted to echo out the array(at least right now) - imploding the array will work great.
__________________
|
|
|
|
04-16-2009, 02:01 PM
|
#16 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Here's the link to my thread where I use the implode function. It will explain why I was looking to "echo" out the items in the array in one clean shot...
Pagination Function
|
|
|
|
04-17-2009, 02:07 PM
|
#17 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Hey guys, I believe I got this part working. It's for my pagination script. The purpose is to show the first 2 and last 2 links with the current page in the middle like so:
current page is: 7
5 6 7 8 9
current page is: 2
1 2 3 4
current page is: 10
8 9 10 11 12
So what I do is use a FOR loop and only take what's in the conditions and put into an array.
Here's the code...
Code:
/* CURRENT PAGE */
$get_page = 13;
/* SIMULATE AVAILABLE PAGES BY USING ARRAY FOR TESTING */
$array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14);
$count = count($array);
/* WE WANT THE FIRST 2 AND LAST 2 LINKS */
for($i=0;$i<=$count;$i++) {
if((($get_page - 2) == $array[$i]) || (($get_page - 1) == $array[$i]) || ($get_page == $array[$i]) || (($get_page + 1) == $array[$i]) || (($get_page + 2) == $array[$i])) {
$array2[] = $array[$i];
}
}
This returns:
11 12 13 14
Since 14 is the last page, there is no page 15....
Comments/criticisms?
|
|
|
|
04-17-2009, 02:13 PM
|
#18 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
I made my FOR LOOP a little cleaner...it works this way too...
I am open to suggestions to make it even better...
Maybe using SWITCH might be a little faster?
Code:
for($i=0;$i<=$count;$i++) {
if(($get_page - 2) == $array[$i]) $array2[] = $array[$i];
if(($get_page - 1) == $array[$i]) $array2[] = $array[$i];
if($get_page == $array[$i]) $array2[] = $array[$i];
if(($get_page + 1) == $array[$i]) $array2[] = $array[$i];
if(($get_page + 2) == $array[$i]) $array2[] = $array[$i];
}
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
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
|
|
|
|