TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Tips & Tricks (http://www.talkphp.com/tips-tricks/)
-   -   Foreach (http://www.talkphp.com/tips-tricks/2490-foreach.html)

Orc 03-19-2008 04:54 AM

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. :]

Wildhoney 03-20-2008 08:22 PM

...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);
}

TlcAndres 03-20-2008 08:25 PM

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)

Orc 03-20-2008 08:35 PM

Quote:

Originally Posted by Wildhoney (Post 12595)
...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

Wildhoney 03-20-2008 08:39 PM

It'll be the $key => $value way by a long way, in terms of speed. We're just being silly :-)

Orc 03-20-2008 08:45 PM

Quote:

Originally Posted by Wildhoney (Post 12599)
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);



sjaq 03-20-2008 09:02 PM

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

Orc 03-20-2008 09:13 PM

Quote:

Originally Posted by sjaq (Post 12602)
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?

Wildhoney 03-20-2008 09:50 PM

Readability/Organisation > Speed.

Aaron 03-20-2008 10:26 PM

What about array_walk()?

freenity 03-20-2008 10:58 PM

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 :)

Orc 03-20-2008 10:59 PM

PHP Code:

$ar = array();
$num 0;

go();

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



Figure out what I changed xD

freenity 03-20-2008 11:05 PM

Quote:

Originally Posted by Orc (Post 12610)
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....

Orc 03-20-2008 11:07 PM

Quote:

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

Of course I forgot to put global $num, $ar....

Of course but Im just bored :P

Wildhoney 03-21-2008 01:16 AM

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));

maZtah 03-25-2008 09:10 AM

Damn, you guys got too much spare time.

:-)

Morishani 03-29-2008 06:41 PM

Quote:

Originally Posted by sjaq (Post 12602)
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 ^^


All times are GMT. The time now is 06:52 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0