Jump to content

Foreach function question... Specifically PHP!

EnVy_28

foreach function without argument? Would it work?

 

I already tried it and it worked! LOL but I'm kinda confuse why? So, I have a stdClass object which can be extracted using foreach right? for example:

Array(    [0] => stdClass Object        (            [username] => derp            [password] => derppass            [email] => derpmail        ))

can be extracted like this: (assuming the array name is $array)

foreach($array as $arr){    echo $arr->username . '<br>';    echo $arr->password. '<br>';    echo $arr->email. '<br>';}

But what I did is I didn't put any argument in foreach and called it outside the function like this:

foreach($array as $arr){    //empty lol}echo $arr->username;echo $arr->password;echo $arr->email;

and it still worked! Dont have any error, but i think thats because my error_reporting setting is set to production value....

 

Is this safe to use?

CPU: Intel Core i5 3570K ( raped to 4.4Ghz) | Motherboard: Gigabyte GA-Z77X-UD3H | RAM: Corsair Vengeance 8GB | GPU: ASUS GTX 980 Strix


Case: Fractal Design DefineR3Storage: 256GB Crucial MX100 & 1TB Samsung Whatevs | Display(s): ASUS MG279Q1x Cheap LG Shit


PSU: Corsair HX650 or something | Cooling: Corsair H80i | Keyboard: Ducky Shine 3 | Mouse: Roccat Kone Pure | Sound: SoundBlaster Recon3D PCI-e

Link to comment
Share on other sites

Link to post
Share on other sites

foreach($array as $arr){    echo $arr->username . '<br>';    echo $arr->password. '<br>';    echo $arr->email. '<br>';}

This way you'll echo each object inside the array, if for exmaple the array contained 5 objects.

foreach($array as $arr){    //empty lol}echo $arr->username;echo $arr->password;echo $arr->email;
This will only echo the last object inside the array (because the foreach is assigning the 'current' object of the array to $arr, and it actually looped on all of them, so you get the last object) .  So if for example the array contained 5 objects, you'll only see the information of the last object being echo'd.

 

So if it's only 1 object, why do you need the array? Also if the array have more than one object, you can access an object explicitly without a 'foreach' if you know the index of it, I'm not very familiar with php but it would probably go as ArrayName[objectIndex] ($array[2] will get you the 3rd element in the array)

Gigabyte z87x-ud3h \ Intel® Core™ i7-4770K \ Corsair Vengeance 2x8GB \ EVGA GeForce GTX 780 Ti SC \ Corsair CX750M \ Onboard sound (Alc898)

Link to comment
Share on other sites

Link to post
Share on other sites

So if it's only 1 object, why do you need the array? Also if the array have more than one object, you can access an object explicitly without a 'foreach' if you know the index of it.

 

No, unfortunately that's what I thought and i knew, but, without foreach, it give me nothing as a result, but when try to print_r(), it gives me an "Array"... if you know what I mean.. Like this:

Array()

CPU: Intel Core i5 3570K ( raped to 4.4Ghz) | Motherboard: Gigabyte GA-Z77X-UD3H | RAM: Corsair Vengeance 8GB | GPU: ASUS GTX 980 Strix


Case: Fractal Design DefineR3Storage: 256GB Crucial MX100 & 1TB Samsung Whatevs | Display(s): ASUS MG279Q1x Cheap LG Shit


PSU: Corsair HX650 or something | Cooling: Corsair H80i | Keyboard: Ducky Shine 3 | Mouse: Roccat Kone Pure | Sound: SoundBlaster Recon3D PCI-e

Link to comment
Share on other sites

Link to post
Share on other sites

Is this safe to use?

I think not.

To quote the official doc: (source)

Warning

Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

It seems that in this case, the second part of that statement comes into play. If you

add a second element to the first array like this:

<?php    $testarr=array    (	0 => array		(			'username' => 'derp',			'password' => 'derppass',			'email' => 'derpmail'		),	1 => array		(			'username' => 'derp2',			'password' => 'derppass2',			'email' => 'derpmail2'		)    );	foreach ($testarr as $arr) {}	echo $arr['username'];	echo $arr['password'];	echo $arr['email'];?>
it will echo the elements of $array[1], because the last element persists (meaning

$arr has not gone out of scope, it just hangs around as the last value it was in

the foreach loop).

You might also find these pages about foreach and variable scope of use:

(link 1, link 2).

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

You won't need a foreach loop and an array, if it's only 1 object.

 

Something like this should work:

 

$me = new Person('boring', '99', 26);
echo $me->age;

 

Also, this might be of use to you.

Gigabyte z87x-ud3h \ Intel® Core™ i7-4770K \ Corsair Vengeance 2x8GB \ EVGA GeForce GTX 780 Ti SC \ Corsair CX750M \ Onboard sound (Alc898)

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry for late reply, I'm really busy with this...

 

Anyway... How about if I make it like this? Would it work?

<?php    $testarr=array    (	0 => array		(			'username' => 'derp',			'password' => 'derppass',			'email' => 'derpmail'		),	1 => array		(			'username' => 'derp2',			'password' => 'derppass2',			'email' => 'derpmail2'		)    ); 	foreach ($testarr as $arr) {            $lol1 = $arr['username'];            $lol2 = $arr['password'];            $lol3 = $arr['email'];        } 	echo $lol1;	echo $lol2;	echo $lol3;?>

CPU: Intel Core i5 3570K ( raped to 4.4Ghz) | Motherboard: Gigabyte GA-Z77X-UD3H | RAM: Corsair Vengeance 8GB | GPU: ASUS GTX 980 Strix


Case: Fractal Design DefineR3Storage: 256GB Crucial MX100 & 1TB Samsung Whatevs | Display(s): ASUS MG279Q1x Cheap LG Shit


PSU: Corsair HX650 or something | Cooling: Corsair H80i | Keyboard: Ducky Shine 3 | Mouse: Roccat Kone Pure | Sound: SoundBlaster Recon3D PCI-e

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry for late reply, I'm really busy with this...

 

Anyway... How about if I make it like this? Would it work?

I suppose that depends on your definition of "work", just try it out

and see for yourself. You should get the same result as in my example

(I do).

While this is slightly less intransparent, it still relies on the fact

that foreach doesn't have its own scope. I don't really see why you'd want

to do this (iterating over an array with foreach only to give out the last

element in that array seems kind of pointless), and I certainly wouldn't

recommend it (very bad practice).

Just because you can do something, doesn't mean you should. ;)

EDIT:

For giving out the last element of an array in PHP, I recommend either

array_pop() or end().

Read the docs and judge for yourself which one makes more sense for you.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×