 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
03-19-2008, 04:54 AM
|
#1 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
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
|
|
|
|
|
The Following User Says Thank You to Orc For This Useful Post:
|
|
03-20-2008, 08:22 PM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
|
...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.
|
|
|
03-20-2008, 08:25 PM
|
#3 (permalink)
|
|
The Addict
Join Date: Nov 2007
Posts: 264
Thanks: 2
|
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.
|
|
|
|
03-20-2008, 08:35 PM
|
#4 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Wildhoney
...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
|
|
|
|
03-20-2008, 08:39 PM
|
#5 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
|
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.
|
|
|
03-20-2008, 08:45 PM
|
#6 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Wildhoney
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
|
|
|
|
03-20-2008, 09:02 PM
|
#7 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
|
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
|
|
|
|
03-20-2008, 09:13 PM
|
#8 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by sjaq
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
|
|
|
|
03-20-2008, 09:50 PM
|
#9 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
|
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.
|
|
|
|
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
|
|
Nor (03-29-2008), Orc (03-20-2008) |
03-20-2008, 10:26 PM
|
#10 (permalink)
|
|
The Frequenter
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
|
What about array_walk()?
|
|
|
03-20-2008, 10:58 PM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Feb 2008
Posts: 119
Thanks: 17
|
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 :)
|
|
|
|
03-20-2008, 10:59 PM
|
#12 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
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
|
|
|
|
03-20-2008, 11:05 PM
|
#13 (permalink)
|
|
The Acquainted
Join Date: Feb 2008
Posts: 119
Thanks: 17
|
Quote:
Originally Posted by Orc
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....
|
|
|
|
03-20-2008, 11:07 PM
|
#14 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by freenity
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
|
|
|
|
03-21-2008, 01:16 AM
|
#15 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,267
Thanks: 90
|
How about it in a for loop?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
03-25-2008, 09:10 AM
|
#16 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Posts: 170
Thanks: 18
|
Damn, you guys got too much spare time.

|
|
|
|
03-29-2008, 06:41 PM
|
#17 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Posts: 32
Thanks: 5
|
Quote:
Originally Posted by sjaq
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)
|
|
|
|
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
|
|
|
|