How to change numbers to random positions in PHP - php

How would I go about changing the following numbers around to a random positions in php?
Do I need to explode the numbers?
40,52,78,81,25,83,37,77
Thanks

$arr = explode(',', '40,52,78,81,25,83,37,77');
shuffle($arr);
echo implode(',', $arr);
http://ideone.com/sh2uH

So you want to shuffle the array order? Use PHP's shuffle function.
http://php.net/manual/en/function.shuffle.php
EDIT:
Didn't realise your numbers were in a string. The other answer sums it up.

Assuming the numbers are in a string:
$numbers = '40,52,78,81,25,83,37,77';
$numbers = explode(',',$numbers);
shuffle($numbers);
$numbers = implode(',',$numbers);

Try something like this.
$string = "40,52,78,81,25,83,37,77";
$numbers = explode(",", $string);
shuffle($numbers);
print_r($numbers);
explode breaks the string out into an array separating entries by ,
shuffle will operate on the array by reference and put them in random order

Related

Get the last value in a comma-separated string

I have a string with numbers, stored in $numbers:
3,6,86,34,43,52
What's the easiest way to get the last value after the last comma? In this case the number 52 would be the last value, which I would like to store in a variable.
The number can vary in size, so trying:
substr($numbers, -X)
does not help me out I think.
This should work for you:
Just use strrpos() to get the position of the last comma and then use substr() to get the string after the last comma, e.g.
$str = "3,6,86,34,43,52";
echo substr($str, strrpos($str, ",") + 1);
output:
52
Just explode the string by the separator character and pick the last of the resulting tokens:
<?php
$string = '3,6,86,34,43,52';
$tokens = explode(',', $string);
echo end($tokens);
An alternative would be to use a regular expression:
<?php
$string = '3,6,86,34,43,52';
preg_match('/,([0-9]+)$/', $string, $tokens);
echo end($tokens);
Personally I have the opinion that efficiency is less important that easy of reading and understanding the code these days. Computation power is cheap, developers are expensive. That is why I would use the first approach, expect when the number of elements in the string gets big.
You can do it like this:
$numbers = "3,6,86,34,43,52";
$arr = explode(",",$numbers);
echo $arr[count($arr)-1];
I'd just explode it to an array, and get the last element:
$numbers = '3,6,86,34,43,52';
$arr = explode(',', $numbers);
echo $arr[count($arr) - 1];
A direct, single-function approach would be to trim every upto the last comma.
Code: (Demo)
$numbers = "3,6,86,34,43,52";
echo preg_replace('/.*,/', '', $numbers);
// 52

How do I know how many arguments explode created

Using explode(), How can I check how many arguments explode created? Is there function which check this or do I have to primary check how many times a character I chose to split on appears in string?
explode() return an array, the number of array elements can be returned with count().
$number = count(explode([a, b, c])); // 3
Return array after explode, use count() will do.
$str = 'Apple, Mango, Orange, Banana';
$exp = explode(',',$str);
echo count($exp);

separate with commas

hey there I have this,
$following_user_id .= $row['following_user_id'];
and I get
44443344330
then I use the implode() function and seperate with commans
44,44,33,44,33,0,
but I don't want the last comma on the last number?
Is this possible?
$following_user_ids = array();
//loop this:
$following_user_ids[] = $row['following_user_id'];
$user_ids_string = implode(',',$following_user_ids);
You can split the string into an array of characters, then implode the array.
$array = preg_split('//', $following_user_id, -1, PREG_SPLIT_NO_EMPTY);
echo implode( ',', $array );
Collect your data into an array of strings and use the implode function:
$uids = array();
while($row = mysql_fetch_assoc($result)){
array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);
Check implode: http://php.net/manual/en/function.implode.php
Code example: I'm assuming your using some sort of loop?
$arrUsers = new array();
... your loop code here ...
array_push($arrUsers, $row['following_user_id']);
... end loop code ..
$following_user_id = impload(",", $arrUsers);
Implode should not be inserting a comma at the end of that string there. Are you sure there isn't an empty string at the end of your array sequence?
Either way, to fix the string you have, just get rid of the last character of the string:
$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);
Further, if you're not going to be using the non-comma delimited number set, why don't you just add a comma every time you add a user id. That way you don't even have to use the implode function.
This works for me:
<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>
Try using arrays, example
<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';
echo implode(',', $arr);

How do I place a comma between each character in a string with PHP? [duplicate]

hey there I have this,
$following_user_id .= $row['following_user_id'];
and I get
44443344330
then I use the implode() function and seperate with commans
44,44,33,44,33,0,
but I don't want the last comma on the last number?
Is this possible?
$following_user_ids = array();
//loop this:
$following_user_ids[] = $row['following_user_id'];
$user_ids_string = implode(',',$following_user_ids);
You can split the string into an array of characters, then implode the array.
$array = preg_split('//', $following_user_id, -1, PREG_SPLIT_NO_EMPTY);
echo implode( ',', $array );
Collect your data into an array of strings and use the implode function:
$uids = array();
while($row = mysql_fetch_assoc($result)){
array_push($uids, $row['following_user_id']);
}
$following_user_id = implode(',', $uids);
Check implode: http://php.net/manual/en/function.implode.php
Code example: I'm assuming your using some sort of loop?
$arrUsers = new array();
... your loop code here ...
array_push($arrUsers, $row['following_user_id']);
... end loop code ..
$following_user_id = impload(",", $arrUsers);
Implode should not be inserting a comma at the end of that string there. Are you sure there isn't an empty string at the end of your array sequence?
Either way, to fix the string you have, just get rid of the last character of the string:
$concatUserIds = "44,44,33,44,33,0,";
$concatUserIds = substr($concatUserIds, 0, strlen($concatUserIds) - 1);
Further, if you're not going to be using the non-comma delimited number set, why don't you just add a comma every time you add a user id. That way you don't even have to use the implode function.
This works for me:
<?php
$following_user_id.= $row['following_user_id'];
$following_user_id=preg_replace('/(?<=\d)(?=(\d)+(?!\d))/',',',$following_user_id);
echo $following_user_id."<br>";
?>
Try using arrays, example
<?php
$arr = array();
$arr[] = 'foo';
$arr[] = 'bar';
echo implode(',', $arr);

strlen for two or more strings?

Which is the correct way of making strlen return the lenght of several strings put together.
For example if one string is hello and other is jim it should return: 8. (hello=5 jim=3)
I need to get the combined lenght of $array[0] $array[1] $array[2] $array[3] and $array[4]
Thanks
Use implode on it before:
echo strlen(implode($array));
You can also combine it with array_slice if you don't want the whole array:
echo strlen(implode(array_slice($array, 0, 4)));
array_sum(array_map("strlen", $array_of_strings))
$len = 0;
foreach($array as $str)
$len += strlen($str);
use something like this:
$string = '';
foreach ($array as $val) {
$string .= $val;
}
echo strlen($string);
This will avoid multiple strlen calls and hence, should be a bit faster then calling strlen inside the foreach loop, atleast theoretically.
You can use implode() to transform your array of strings in a single string and then strlen the result.
echo strlen(implode('', $array))
If you want to get the collective length of all of the strings in an array, you can do this:
$len = strlen(implode('',$myArray));
*Note that this code will join the strings together with no spaces between them. You can change the first parameter to implode() if you want this to be different.

Categories