TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-10-2009, 11:50 AM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default 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?
allworknoplay is offline  
Reply With Quote
Old 04-10-2009, 12:31 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

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....
allworknoplay is offline  
Reply With Quote
Old 04-10-2009, 03:08 PM   #3 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

There might be a premade function for it, but a simple for each loop would do the trick. Its hardly a wheel to reinvent.
__________________

Village Idiot is offline  
Reply With Quote
Old 04-10-2009, 03:10 PM   #4 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
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...
allworknoplay is offline  
Reply With Quote
Old 04-10-2009, 04:07 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

PHP Code:
function print_array($array)
{

     foreach(
$array as $element)
     {

          echo 
$element ' ';

     }

}

$array = array(123456);
print_array($array); //output: 1 2 3 4 5 6 
__________________
Tanax is offline  
Reply With Quote
Old 04-10-2009, 04:10 PM   #6 (permalink)
The Acquainted
Inquisitive 
 
Join Date: Jul 2005
Location: UK
Posts: 121
Thanks: 41
Brook is on a distinguished road
Default

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!
Brook is offline  
Reply With Quote
Old 04-10-2009, 04:19 PM   #7 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
PHP Code:
function print_array($array)
{

     foreach(
$array as $element)
     {

          echo 
$element ' ';

     }

}

$array = array(123456);
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 View Post
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..
allworknoplay is offline  
Reply With Quote
Old 04-10-2009, 04:17 PM   #8 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

although i don't see why a simple foreach would be a problem, you could do something like this:
PHP Code:
array_walk($arraycreate_function('$val''echo $val, \'<br />\';')); 
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 04-10-2009, 04:21 PM   #9 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
although i don't see why a simple foreach would be a problem, you could do something like this:
PHP Code:
array_walk($arraycreate_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...
allworknoplay is offline  
Reply With Quote
Old 04-10-2009, 10:58 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

PHP Code:
echo implode(' '$array); 
Salathe is offline  
Reply With Quote
Old 04-11-2009, 10:14 AM   #11 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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.
__________________
Tanax is offline  
Reply With Quote
Old 04-11-2009, 12:01 AM   #12 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

Quote:
PHP Code:
echo implode(' '$array); 
Darn, you beat me to it! lol
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 04-11-2009, 12:07 AM   #13 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by ETbyrne View Post
Darn, you beat me to it! lol
I know....he's very very good....

allworknoplay is offline  
Reply With Quote
Old 04-11-2009, 01:06 PM   #14 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Why would you want to extend echoing an array's values, with more functionality?!
Salathe is offline  
Reply With Quote
Old 04-12-2009, 12:09 AM   #15 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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.
__________________
Tanax is offline  
Reply With Quote
Old 04-16-2009, 02:01 PM   #16 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

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
allworknoplay is offline  
Reply With Quote
Old 04-17-2009, 02:07 PM   #17 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

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?
allworknoplay is offline  
Reply With Quote
Old 04-17-2009, 02:13 PM   #18 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

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];
	
}
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
lol, i made a never ending loop with arrays sarmenhb Tips & Tricks 28 10-22-2012 09:36 AM
config file arrays oop memcache MJ_ General 2 01-24-2009 06:53 PM
2-d arrays -- issues of printing and deleting Dave Absolute Beginners 3 06-05-2008 11:45 AM
Arrays + Pages blayne4k Script Giveaway 0 04-20-2008 10:35 PM
Problem with Arrays CoryMathews Absolute Beginners 6 02-27-2008 02:04 PM


All times are GMT. The time now is 04:45 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design