PHP - Is iteration on object ordered? [duplicate] - php

This question already has answers here:
Is foreach guaranteed to iterate in the array order in php?
(4 answers)
How does PHP 'foreach' actually work?
(7 answers)
Closed 6 years ago.
I know that I can use this to iterate on an object:
foreach ($this as $key => $value) {
print "$key => $value\n";
}
Is the order in which the properties are looked over always the same or is it random?
I tried it a bit and it seems to always follow the declaration order, as in the exemple from here: http://php.net/manual/en/language.oop5.iterations.php
But I couldn't find any documentation where this is formally specified.
Can an experimented PHP dev confirm or disconfirm what I found in my trials?

Related

Differences in this Array Declarations in PHP [duplicate]

This question already has answers here:
PHP array vs [ ] in method and variable declaration
(5 answers)
PHP: What's the difference between initializing an array with "new" vs without it?
(4 answers)
Closed 1 year ago.
What is the difference between declaring:
$myArray = Array();
and:
$myArray = [];
There's no difference, it's just two alternative syntaxes. See https://www.php.net/manual/en/language.types.array.php

Is declaring a PHP var valid with this syntax: &$var? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 1 year ago.
I'm upgrading old in-house PHP code for my company and I stumbled upon this piece of code:
foreach ($array as $key => &$value) {
// do something
}
what's the meaning of &$value and can I safely assume this is a mistype?
It is not mistype. It is reference and it means to access the same variable content by different names. If you modify the value of $value, then you would also modify the original value inside $array.
You can read more about it at References Explained, Php.net

How to create a function finding the last key of an array using value PHP in different ways [duplicate]

This question already has answers here:
PHP - Get key name of array value
(9 answers)
Closed 4 years ago.
How to get the Cat by using the value Nap in array
["Dog" => "Bite", "Cat" => "Nap"]
and i want to get Cat using value Nap
$key = array_search('Nap', $array);
What is not clear in your question is if you already know the entry is the last entry, or if you're searching for a value that is somewhere in your array.
If you have PHP 7.3, and you know it's the last entry, you can use array_key_last() (see http://php.net/manual/en/function.array-key-last.php )
$key = array_search('Nap', $array); (as mentionned by #Sanu0786 )

Why to declare array before setting it (php)? [duplicate]

This question already has answers here:
Is it necessary to declare PHP array before adding values with []?
(13 answers)
Closed 5 years ago.
I dont know why people declare array before loops and etc..:
$new= array(); // <----- why this is needed ?
foreach($something as $v){
$new[] = $v;
}
Why to declare the array before setting its value? (In other languages, i.e. C# and JAVA it is needed, but why in PHP?)
You're not setting its value, you're pushing a new element onto the array. But there needs to be an empty array to push onto.

Why does this PHP code output 1,2,2? [duplicate]

This question already has answers here:
PHP Foreach Pass by Reference: Last Element Duplicating? (Bug?) [duplicate]
(6 answers)
Why php iteration by reference returns a duplicate last record?
(1 answer)
Closed 9 years ago.
Friend posted this PHP snippet.
<?
$a=array(1,2,3);
foreach ($a as &$item){}
foreach ($a as $item){}
print_r($a);
The output is 1,2,2 - why?

Categories