Simple PHP random array [duplicate] - php

This question already has answers here:
Get random item from array [duplicate]
(4 answers)
Closed 9 years ago.
I have a simple array like this:
$input = array('Line1', 'Line2', 'Line3');
And want to echo one of the values randomly. I've done this before but can't remember how I did it and all the examples of array_rand seem more complex that what I need.
Can any help? Thanks

echo $input[array_rand($input)];
array_rand() returns the key, so we need to plug it back into $input to get the value.

Complex? Are we on the same manual page?
$rand_key = array_rand($input, 1);

You could use shuffle() and then just pick the first element.
shuffle($input);
echo $input[0];
But I would go with the array_rand() method.

array_rand will help you select a random key of an array. From there you can get the value.
$randKey = array_rand($input);
echo $input[$randKey];

Just a single function: array_rand().
echo $input[array_rand($input,1)];

Related

php array count returns 1 [duplicate]

This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Why does counting an empty string return 1?
(3 answers)
Count() returning 1 with comma separated string with explode [duplicate]
(2 answers)
Closed 3 years ago.
i have this data which when tested with direct input it works and file_put_contents confrims that data is exactly same but when i try to get the value through site it gives only 1
i tried to declare array but everytime it gives 1 only
this array count gives count as 6
$total_id_counttt = count(array(13068,13067,13066,13065,13064,13063));
echo $total_id_counttt;
but when i use this here it return 1
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = array($str_finalppo);
$total_id_counttt = count($schools_array);
it returns 1 can anyone tell where i am doing mistake
First of all, Convert the string into an array. Look it in this code you may get the idea.
Use explode function to convert string to array.
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = explode(',',$str_finalppo);
$total_id_counttt = count($schools_array);
Check more about explode
In PHP, value in quotes('') consider as string.
So when you try to count this is always returning the 1.
Try to convert the string into an array by using explode().
for example:
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = explode(',',$str_finalppo);
echo $total_id_counttt = count($schools_array);
this will give you the right answer.
And if you still confuse try to print #schools_array before using dd($schools_array);
It will definitely remove your confusion.

Neat way to get last array element in PHP [duplicate]

This question already has answers here:
How to get the last element of an array without deleting it?
(33 answers)
Closed 8 years ago.
I'd like to get last element of an array in PHP.
I often do this:
$last = $this->array_of_stuff[count($this->array_of_stuff) - 1];
I'm not very happy with that though.
Any way to make the syntax shorter and avoid repeating code?
You can use end function for this case. See the manual
end($array)
You can use end()
<?php
$a = array("a", "b", "c");
echo end($a);
https://eval.in/188679

Convert Query String to Array [duplicate]

This question already has answers here:
Parse query string into an array
(12 answers)
Closed 9 years ago.
How can I convert this to an array in PHP?
&height=0&weight=2&width=10
I'm passing a data from a jquery function using .serialize() to a PHP function.
Any ideas?
Can be done within one line. :)
parse_str('&height=0&weight=2&width=10', $array);
print_r($array);
Depending on what type of request you are performing, it may already be in an array. Have a look at the PHP documentation on $_GET and $_POST global variables.
To view the contents of said array. You can use the function print_r() which will show you the contents of the array.
print_r($_GET)
print_r($_POST)
Access individual items in the array by the item's key. For example:
echo $_POST['height'];

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.

How to grab seperated integers from long string [duplicate]

This question already has answers here:
Separate space-delimited words in a string [duplicate]
(4 answers)
Closed 11 months ago.
I am working on a enrollment system for a judo club. I am using a string to store the ID's of people, who showed up. It looks somthing like this:
0,3,4,7,8,10,
Now, I want to seperate each number into an integer array in PHP. What would the simplest solution to this be? :)
Use explode()
$ids = explode(',', '0,3,4,7,8,10');
$str = '0,3,4,7,8,10';
$arr = explode(',',$str);
use EXPLODE
$num = "0,3,4,7,8,10";
$pieces = explode(",", $num );
Explode Manual

Categories