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

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.

Related

How to add in digits per json objects wih php [duplicate]

This question already has answers here:
How do I `json_encode()` keys from PHP array?
(4 answers)
Closed 4 years ago.
img
How do I add digits per objects?
https://paste.ubuntu.com/p/mSzJnY87KF/
I want to do;
img
what I have to do, I have tried many ways.
tnks for responses
Just need some thing like this
$arr = [];
$arr["1"] = "content 1";
$arr["10"] = "content 2";
echo json_encode($arr);
do not use array_push

Find string, then find string in-between chars [duplicate]

This question already has answers here:
What kind of data format is that?
(2 answers)
Closed 4 years ago.
I'm having a hard time with this....
So, I have a load of text:
a:13:{s:9:"live_odds";i:0;s:14:"show_matchbook";i:0;s:12:"show_betfred";i:1;s:16:"show_williamhill";i:1;s:12:"betfair_show";i:1;s:16:"betfair_username";N;s:16:"betfair_password";s:9:"";s:20:"betfair_affiliate_id";s:3:"888";
Now, what I am trying to do is search for betfair_affiliate_id";s:3: within that bulk of text and then display the text between the speech marks.
I was trying
$betfred_show = 'betfair_affiliate_id";s:3:"';
$betfred_show_value = substr($string, strpos($string, $betfred_show) + strlen($betfred_show), 3);
which does work, it brings back 888... but it's not really future proof as this just gets the 3 next digits. However, these could change to 4 or 5 digits.
Any suggestions?
It is a serialised array. You can simply unserialize it and access the key
$array = unserialize($input);
$output = $array['betfair_affiliate_id'];

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 )

Php Unset All Keys [duplicate]

This question already has answers here:
For cleared or unset php arrays, are elements garbage collected?
(3 answers)
Closed 8 years ago.
My question might seems basic but still, can't figure how to works this out.
Consider an array of my favorite fruits
$array = array("Banana","Rasberry","Blackberry")
I'm looking to clear this array so that all keys and values would be erased. My array would be empty just like if I had wrote
$array = array();
Then, I could array_push some new data in.
I thought that I could array_walk($array, unset($array[$key]) but it's not working properly.
Your question includes the best solution for your situation:
$array = array();
This is the fastest way to make the $array variable point to an empty array.

Remove all items of an array except the last 5 on PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Remove elements from array?
how would I remove all array elements but last 5 ones? The array is a log, but the log will become extensive so I just want to see the five recent items (a.k.a. last five elements)
Use array_slice:
$a5 = array_slice($arr, -5);
$new = array_slice($old, -5)
if you are getting this array from the text file, you shouldn't read the whole file into array.
either use a command line utility to get 5 last lines,
$last5 = `tail $logfile`;
or at least read only last chunk of it, of considerable side of, say 1Kb and than get last 5 out of it.

Categories