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 03-19-2008, 04:54 AM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default Foreach

Just another method I guess :P
PHP Code:
<?php
error_reporting
(E_ALL);
$_GET = array();

$_GET['a'] = "Hello World!";

if (
$_GET['a'])
{
    
extract($_GET);
    
$t [] = $a;
    foreach(
array_keys($t) As $val)
    {
        echo 
$t[$val];
    }
}
?>
Instead of $key => $val xD

This can be simplified but I was just having fun. :]
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
The Following User Says Thank You to Orc For This Useful Post:
nullbyte (03-19-2008)
Old 03-20-2008, 08:22 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

...Or you can do:

php Code:
$aItems = array
(
    'Wildhoney' => 'Adam',
    'Salathe' => 'Peter',
    'Bluesaga' => 'Will'
);

while(list($szKey, $szValue) = each($aItems))
{
    printf('Key = "%s" and Value = "%s"<br />', $szKey, $szValue);
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 03-20-2008, 08:35 PM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
...Or you can do:

php Code:
$aItems = array
(
    'Wildhoney' => 'Adam',
    'Salathe' => 'Peter',
    'Bluesaga' => 'Will'
);

while(list($szKey, $szValue) = each($aItems))
{
    printf('Key = "%s" and Value = "%s"<br />', $szKey, $szValue);
}
Erm.. Mine is only
PHP Code:

foreach(array_keys($array) As $key)
{
   echo 
$array[$key];

You're just making it complicated xD
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-20-2008, 08:25 PM   #4 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

Anyone care to benchmark? I'm almost sure the foreach is faster but it's nice to know none-the-less. (I'd do it but I'm shamlessly browsing whilst I'm supposed to be working)
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
Old 03-20-2008, 08:39 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

It'll be the $key => $value way by a long way, in terms of speed. We're just being silly
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 03-20-2008, 08:45 PM   #6 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
It'll be the $key => $value way by a long way, in terms of speed. We're just being silly
lol alright.

PHP Code:

foreach ($array As $key => $val)
{
   
// Wildhoney >:o
   
printf('%s => %s',$key,$val);

__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-20-2008, 09:02 PM   #7 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Default

Actually, there is a faster way for doing a foreach with key and value:
PHP Code:
$key array_keys($aHash);
$size count($key);
for (
$i=0$i<$size$i++) $aHash[$key[$i]] .= "a"
source
sjaq is offline  
Reply With Quote
Old 03-20-2008, 09:13 PM   #8 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
Actually, there is a faster way for doing a foreach with key and value:
PHP Code:
$key array_keys($aHash);
$size count($key);
for (
$i=0$i<$size$i++) $aHash[$key[$i]] .= "a"
source
I can never tell why there is a 'faster' way of doing anything in php, I mean.. The page renders fast for me, and it does so every time I refresh the browser, so meh, fast?
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-29-2008, 06:41 PM   #9 (permalink)
The Contributor
 
Join Date: Nov 2007
Posts: 32
Thanks: 5
Morishani is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
Actually, there is a faster way for doing a foreach with key and value:
PHP Code:
$key array_keys($aHash);
$size count($key);
for (
$i=0$i<$size$i++) $aHash[$key[$i]] .= "a"
source
Hi, Thanks for the benchmarks
__________________
מטבחים (hebrew)
Send a message via ICQ to Morishani Send a message via MSN to Morishani
Morishani is offline  
Reply With Quote
Old 03-20-2008, 09:50 PM   #10 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Readability/Organisation > Speed.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
Nor (03-29-2008), Orc (03-20-2008)
Old 03-20-2008, 10:26 PM   #11 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

What about array_walk()?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 03-20-2008, 10:58 PM   #12 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

Not sure what are you doing here. Why don't you like foreach ?? :S

Well, if you want some weird stuff:

PHP Code:
$ar = array();
$num 0;

go();

function 
go()
{
    echo 
$ar[$num];
    
$num ++;
    if (
$num count($ar))
        
go();

lol :)
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 03-20-2008, 10:59 PM   #13 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

PHP Code:
$ar = array();
$num 0;

go();

function 
go()
{
    echo 
$ar[$num];
    ++ 
$num;
    if (
$num count($ar))
        
go();


Figure out what I changed xD
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-20-2008, 11:05 PM   #14 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
PHP Code:
$ar = array();
$num 0;

go();

function 
go()
{
    echo 
$ar[$num];
    ++ 
$num;
    if (
$num count($ar))
        
go();


Figure out what I changed xD

It would give the same result.... (++$num)

Of course I forgot to put global $num, $ar....
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 03-20-2008, 11:07 PM   #15 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by freenity View Post
It would give the same result.... (++$num)

Of course I forgot to put global $num, $ar....
Of course but Im just bored :P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-21-2008, 01:16 AM   #16 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

How about it in a for loop?

php Code:
for
(
    $iIndex = 0, $aValue = $aItems, $aKey = array_flip($aItems);
    $iIndex < count($aItems);
    next($aValue), next($aKey), $iIndex++
) printf('Key = "%s" and Value = "%s"<br />', current($aKey), current($aValue));
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 03-25-2008, 09:10 AM   #17 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

Damn, you guys got too much spare time.

maZtah 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


All times are GMT. The time now is 05:33 PM.

 
     

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