Foreach loop shuffled on output [duplicate] - php

This question already has answers here:
Shuffle an array in PHP
(6 answers)
Closed 5 years ago.
Is there a simple way to get the result of this foreach loop shuffled on output at each page load?
foreach(glob('images/fotoalbum/*.*') as $file)

you can't shuffle after output but you can before.
try this
$dir=glob('images/fotoalbum/*.*');
shuffle($dir);
foreach($dir as $file){
//operation here
}

Related

How to remove empty element in an array [duplicate]

This question already has answers here:
Remove empty array elements
(27 answers)
Closed 5 years ago.
I have been trying array_filter but doesn't work on my part.
If you want is to remove empty arrays inside an array, you can use this
$array= array_filter(array_map('array_filter', $array));

PHP - Is iteration on object ordered? [duplicate]

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?

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?

How to split array values to new array? [duplicate]

This question already has answers here:
How to "flatten" a multi-dimensional array to simple one in PHP? [duplicate]
(23 answers)
Closed 9 years ago.
how to split received values of an array into a new array?
so from this:
[["+","+","+"],["-","+","+"],["*","+","+"],["\/","+","+"],
to this:
["+"],["+"],["+"],["-"],["+"],["+"],["*"],["+"],["+"],
can someone help me?
Flatten your array by looping through it
$aFlattened = array();
foreach($aOriginalArray AS $aOperators){
$aFlattened = array_merge($aFlattened, $aOperators);
}

php random array - random again when doing while [duplicate]

This question already has answers here:
php random order from a foreach
(3 answers)
Closed 9 years ago.
Right now, every single time I do a while do, it goes from top to bottom of my array. How can I make it go through each value but in a random mode, not from top to bottom?
Here's what I have:
$xbb = array('avotf1',
'avotf2',
'avotf3',
'avotf4',
'avotf5',
'avotf6',
'avotf7',
'avotf8',
'avotf9',
'avotf11',
'avotf12',
'avotf13',
'avotf14',
'avotf15',
'avotf10');
foreach($xbb as $item)
{
echo "$item<br>";
}
How do I shuffle the random array that I have and still show all 15 values?
Shuffle it with shuffle():
shuffle($xbb);
Searching Google for php shuffle array will give you tons of results as well, by the way.

Categories