I have a string that looks like this
$genres = "pop,rock,jazz,80s";
i was wondering is it possible to then create a string that randomly selects any of those genres above? but removing the comma?
for example it would create
$newgenre = "pop";
You could use something like this:
$genreArray = explode(',', $genres);
$genre = $genreArray[mt_rand(0, count($genreArray))];
Alternative Method
Everyone else is using random selection from an array, try this alternative:
$genreArray = explode(',', $genres);
shuffle($genreArray); //mixes up the array using rand or mt_rand
$genre = $genreArray[0]; // take first element of shuffled array.
You can use the $genreArray = explode(',', $genres) function to put it all into an array.
Then you can generate a random index for the array, using
$randomKey = rand(0, count($genreArray))
And then, all you have to do is take the random genre from the array.
$randomGenre = $genreArray[$randomKey];
You could explode
and get a random value from the array
$genresArray = explode(',',$genres);
$your_values = array_rand ($genresArray,1);
echo $your_values[0];
Related
I have 2 data.
$dataA = 01,05,07;
$dataB = 01,07;
Now, I would like to explode the $dataA and combine back with only 2 string. like below:
$explode = explode(',',$dataA);
/*combine back get the max combination without duplicate string, example for the $dataA.
My suspect is: first array (01,05), second array(05,07), third array(07,01), I don't need (05,01) since first array have both value.
*/
After that, if the combination array value match with the $dataB (no matter which the value position is), then do something like:
$array[0] = '01,05';
$array[1] = '05,07';
$array[2] = '07,01';
//since $array[2] value = $dataB (no need to check specific position); so I would like to do as below
if(in_array(third array,$dataB)){ //use in_array or?
//do something
}
Is it any array function can done this?
is it possible that, get arrays to $value with key:
Example:
$array = Array("one"=>Array("field1"=>"value1","field2"=>"value2"),
"two"=>Array("field3"=>"value3","field4"=>"value4"));
Export arrays to value:
$first = any_main_php_function_name(0,$array);
$second = any_main_php_function_name(1,$array);
Result:
$first = Array("field1"=>"value1","field2"=>"value2");
$second = Array("field3"=>"value3","field4"=>"value4");
Basically, I wanna extract multiple array. If there is no such function (any_main_php_function_name) in PHP so How can i extract above $array.
You don't need any funtion to do that. Simply get subarrays like this:
$first = $array['one'];
$second = $array['two'];
Unless you don't know the one,two keys, then you can use array_shift to get first item (one subarray). Remember that this functions also removes returned value from root array.
array_slice does what you want
$first = array_slice($array, 0, 1);
$second = array_slice($array, 1, 1);
print_r($first);
print_r($second);
May be you can array_shift to get the element from array, Like this:
$first_element=array_shift($array);
Make sure it only removes the first element from the array and
return the value of removed element.
And if you don't want to remove the element or get the sub-array in any sequence, then you can create a function like this,
function myFunction($index,$array) {
$keys = array_keys($array);
$sub_array=$arr[$keys[$index]];
}
In above function we just get the keys in a array and then use the known index to get the sub-array using keys array.
Please check this
foreach($array["one"] as $key=>$val){
echo "key=>".$key." Values=>".$val;
}
foreach($array["true"] as $key=>$val){
echo "key=>".$key." Values=>".$val;
}
I have a key value pair string that I would like to convert to a functional array. So that I can reference the values using their key. Right now I have this:
$Array = "'Type'=>'Honda', 'Color'=>'Red'";
$MyArray = array($Array);
This is not bringing back a functional key/value array. My key value pairs are in a variable string which means the => is part of the string and i think this is where my problem is. Any help would be appreciated. All i am trying to do is convert the string to a functional key/value pair where I can grab the values using the key. My data is in a string so please don't reply with the answer "take them out of the string." I am aware that this will work:
$MyArray = array('Type'=>'Honda', 'Color'=>'Red');
But my probem is that the the data is already in the form of a string. Thank you for any help.
There is no direct way to do this. As such, you'll need to write a custom function to build the keys and values for each element.
An example specification for the custom function:
Use explode() to split each element based on the comma.
Iterate over the result and:
explode() on =>
Remove unnecessary characters, i.e. single quotes
Store the first element as the key and the second element as the value
Return the array.
Note: if your strings contain delimiters this will be more challenging.
You do need to "take them out of the string", as you say. But you don't have to do it manually. The other answer uses explode; that's a fine method. I'll show you another - what I think is the easiest way is to use preg_match_all() (documentation), like this:
$string = "'Type'=>'Honda', 'Color'=>'Red'";
$array = array();
preg_match_all("/'(.+?)'=>'(.+?)'/", $string, $matches);
foreach ($matches[1] as $i => $key) {
$array[$key] = $matches[2][$i];
}
var_dump($array);
You need to parse the string and extract the data:
$string = "'Type'=>'Honda', 'Color'=>'Red'";
$elements = explode(",",$string);
$keyValuePairs = array();
foreach($elements as $element){
$keyValuePairs[] = explode("=>",$element);
}
var_dump($keyValuePairs);
Now you can create your on array using the $keyValuePairs array.
Here is an example of one way you can do it -
$Array = "'Type'=>'Honda', 'Color'=>'Red'";
$realArray = explode(',',$Array); // get the items that will be in the new array
$newArray = array();
foreach($realArray as $value) {
$arrayBit = explode('=>', $value); // split each item
$key = str_replace('\'', '', $arrayBit[0]); // clean up
$newValue = str_replace('\'', '', $arrayBit[1]); // clean up
$newArray[$key] = $newValue; // place the new item in the new array
}
print_r($newArray); // just to see the new array
echo $newArray['Type']; // calls out one element
This could be placed into a function that could be extended so each item gets cleaned up properly (instead of the brute force method shown here), but demonstrates the basics.
I have a SQL query which returns a result of one field, so I have the following:
$article_id = $this->item->id;
$authors_default = mysql_query("SELECT multi_authors FROM jos_jxzine_articles WHERE id = '$article_id' LIMIT 1");
$authors_default = mysql_fetch_assoc($authors_default);
echo $authors_default['multi_authors'];
This echos out
128,129
and so on for different queries.
How can I make this into the following
array(128,129)
To then put into a prewritten function?
Cheers
The following code takes that MySQL row and splits it up into pieces using , as the delimiter. It then converts that array of strings to an array of integers.
$authors_arr = explode(',', $authors_default['multi_authors']);
// $authors_arr = array("128", "129");
$authors_arr = array_map('intval', $authors_arr);
// $authors_arr = array(128, 129);
You can then pass that array into a function like so:
myFunction($authors_arr); // Or however you have it setup.
$authors_array = explode(",", $authors_default['multi_authors']);
This will break apart your MySQL result into an array. Since your query pulls a string which is delimited by a comma, the explode() function can be used to separate out the string.
http://php.net/manual/en/function.explode.php
Sorry, this is untested since I have removed PHP from my localhost. If I understand you correctly.
<?php $arr = explode(',', $authors_default['multi_authors']); print_r($arr); ?>
http://php.net/manual/en/function.explode.php
By example:
FirstString='apple,gold,nature,grass,class'
SecondString='gold,class'
the Result must be :
ResultString='apple,nature,grass'
$first = explode(',',$firstString);
$second = explode(',',$secondString);
Now you have arrays and you can do whatever you want with them.
$result = array_diff($first, $second);
this is the easy way (for sure there must be more efficient ones):
First of all, you may want to separate those coma-separated strings and put them into an array (using the explode function):
$array1 = explode(',' $firstString);
$array2 = explode(',' $secondString);
Then, you can loop the first array and check whether it contents words of the second one using the in_array function (if so, delete it using the unset function):
// loop
foreach( $arra1 as $index => $value){
if( in_array ( $value , $array2 ) )
unset($array1[$index]); // delete that word from the array
}
Finally, you can create a new string with the result using the implode function:
$result = implode(',' , $array1);
That's it :D
I'm sure there is a function that can do it but you could always break up the strings and do a foreach on each one and do some string compares and build a new string. You could also break apart the second string and create a regular expression and do a preg_replace to replace the values in the string.