I have an array of letters and I want to pick 5 unique ones.
$variable_array = array('x', 'n', 'f', 'w', 'g', 'r', 'c', 'm', 'y', 'u', 'p', 'a', 'd', 'h', 'k', 'z');
So, I want something like this...
$variable_1 = $variable_array[array_rand($variable_array)];
$variable_2 = $variable_array[array_rand($variable_array)];
$variable_3 = $variable_array[array_rand($variable_array)];
$variable_4 = $variable_array[array_rand($variable_array)];
$variable_5 = $variable_array[array_rand($variable_array)];
but all of the variables need to be unique. I know there are other answers similar to this on Stackoverflow already, but they all seem to be slightly different than my situation. Any ideas?
PHP shuffle(): http://php.net/manual/en/function.shuffle.php
// make sure you have only unique values in your array
// $variable_array = array_unique($variable_array);
// randomize array
shuffle($variable_array);
// get 5 elements like suggested in the comments:
// $top5_array = array_slice($variable_array, 0, 5);
$top5_array = ARRAY();
// do 5 times ...
//
// if (is_array($variable_array) AND count($variable_array) >=5) {
//
for ($i=0; $i<=4; $i++) {
$top5_array[] = $variable_array[$i];
print $variable_array[$i].'<br />';
}
var_dump($top5_array);
Related
I have an array:
$arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
Is there a way to make it contain elements only divisible by 3 (or n)? So in the above example g would be removed as it's left over from a division of 3?
EDIT
I want amount of elements divisible by 3.
You can use a combination of array_slice(), count() and modulus (%) to get the array evenly divisible:
$arr_length = count($arr);
$new_arr = array_slice($arr, 0, $arr_length - ($arr_length % 3));
Sounds like you want the length to be divisible by 3 and you want to remove elements until that happens. array_slice can help you out here.
$to_remove = count($arr) % 3;
if($to_remove > 0){
$arr = array_slice($arr, 0, -$to_remove);
}
#panthro simply try with for loop like below and use array_pop()
<?php
$arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
if(count($arr) % 3 != 0){
$remainder = count($arr) % 3;
for($i=0;$i < $remainder;$i++){
array_pop($arr);
}
}
echo "<pre>";
print_r($arr); //final array divisible by 3
I have 6 variables and each other have integer values, I want to get the variable name which have the greatest value.
The variables are: $a = 2, $b = 3, $c = 3, $d = 4, $e = 4, $f = 4
If I use this code:
$var = compact('a', 'b', 'c', 'd', 'e', 'f');
arsort($var);
$name = key($var);
The variable $name will contain only $d. The question is, how to get $d, $e, $f?
Try array_keys() with the search argument:
$var = compact('a', 'b', 'c', 'd', 'e', 'f');
arsort($var);
$max = reset($var); // get the maximum value (first item)
$results = array_keys($var, $max, true); // search for all the maximums and return the keys
Or use max() without sorting:
$var = compact('a', 'b', 'c', 'd', 'e', 'f');
$max = max($var); // get the maximum value
$results = array_keys($var, $max, true); // search for all the maximums and return the keys
Its working nicely:
$str = 'a';
echo ++$str; // prints 'b'
$str = 'z';
echo ++$str; // prints 'aa'
Its very useful to get next column name in an excel file.
But if I use similar code using -- operator to get the previous letter then its not working:
$str = 'b';
echo --$str; // prints 'b' but I need 'a'
$str = 'aa';
echo --$str; // prints 'aa' but I need 'z'
What can be the solution to get the previous letter similarly?
And what can be the reason as its not working?
$str='z';
echo chr(ord($str)-1); //y
Note: This isn't circular for a-z. Need to add rules for that
Fiddle
Edit
This edit covers for your special requirement from excel example. Although its a little longer piece of code.
//Step 1: Build your range; We cant just go about every character in every language.
$x='a';
while($x!='zz') // of course you can take that to zzz or beyond etc
{
$values[]=$x++; // A simple range() call will not work for multiple characters
}
$values[]=$x; // Now this array contains range `a - zz`
//Step 2: Provide reference
$str='ab';
//Step 3: Move next or back
echo $values[array_search(strtolower($str),$values)-1]; // Previous = aa
echo $values[array_search(strtolower($str),$values)+1]; // Next = ac
Fiddle
Using array:
$cla=array('A', 'B', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'R', 'S', 'Š', 'Z', 'Ž', 'T', 'U', 'V', 'Õ', 'Ä', 'Ö', 'Ü');
$direction = -1;
$element = 'Ü';
$element2 = $cla[((array_search($element,$cla)+count($cla)+$direction)%count($cla))];
I could solve in this way.
How is it?
The cons is that it only can handle uppercase right now.
Some more work can also fix that.
<?php
function get_previous_letter($string){
$last = substr($string, -1);
$part=substr($string, 0, -1);
if(strtoupper($last)=='A'){
$l = substr($part, -1);
if($l=='A'){
return substr($part, 0, -1)."Z";
}
return $part.chr(ord($l)-1);
}else{
return $part.chr(ord($last)-1);
}
}
echo get_previous_letter("AAAAAA");
?>
CODEPAD
I am trying to do chord transposition in PHP the array of Chord values are as followed...
$chords1 = array('C','C#','D','D#','E','F','F#','G','G#','A','A#','B','C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C');
An example would be D6/F#. I want to match the array value and then transpose it by a given number position in the array. Here is what I have so far...
function splitChord($chord){ // The chord comes into the function
preg_match_all("/C#|D#|F#|G#|A#|Db|Eb|Gb|Ab|Bb|C|D|E|F|G|A|B/", $chord, $notes); // match the item
$notes = $notes[0];
$newArray = array();
foreach($notes as $note){ // for each found item as a note
$note = switchNotes($note); // switch the not out
array_push($newArray, $note); // and push it into the new array
}
$chord = str_replace($notes, $newArray, $chord); // then string replace the chord with the new notes available
return($chord);
}
function switchNotes($note){
$chords1 = array('C','C#','D','D#','E','F','F#','G','G#','A','A#','B','C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C');
$search = array_search($note, $chords1);////////////////Search the array position D=2 & F#=6
$note = $chords1[$search + 4];///////////////////////then make the new position add 4 = F# and A#
return($note);
}
This works, except the problem is that if I use a split chord like (D6/F#) The chord is transposed to A#6/A#. It is replacing the first note (D) with an (F#) then, Both (F#'s) with an (A#).
The question is... How can I keep this redundancy from happening. The desired output would be F#6/A#. Thank you for your help. If the solution is posted, I WILL mark it as answered.
You can use preg_replace_callback function
function transposeNoteCallback($match) {
$chords = array('C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B', 'C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B', 'C');
$pos = array_search($match[0], $chords) + 4;
if ($pos >= count($chords)) {
$pos = $pos - count($chords);
}
return $chords[$pos];
}
function transposeNote($noteStr) {
return preg_replace_callback("/C#|D#|F#|G#|A#|Db|Eb|Gb|Ab|Bb|C|D|E|F|G|A|B/", 'transposeNoteCallback', $noteStr);
}
Test
echo transposeNote("Eb6 Bb B Ab D6/F#");
returns
G6 C# Eb C F#6/A#
Cheap advice: move into natural numbers domain [[0-11]] and associate them with corresponding notes at display time only, it will save you many time.
The only problem will be homophones sounds [e.g. C-sharp / D-flat], but hope you can deduce it from tonality.
Here my table looks like
|A Bay of Blood|
|The Adventure|
|Blood River|
|Dooms Day|
|....|
I want to show a-z index
A B C D E F...... Z
and i want to give only clickable link for letter which can be found in table
This should work :
$abc = array ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
foreach ($abc as $v) {
$ret = $db->query("SELECT COUNT (column) WHERE column LIKE '" .$v. "%'");
$count = $ret->fetchColumn();
if ($count == 0)
echo '<li>' .$v. '</li>';
else
echo '<li>' .$v. '</li>';
}
A simple approach would be to use a query like this:
"SELECT column1, column2 FROM yourtable WHERE title LIKE '$letter%'"
... where $letter is A, B or C etc...
Make an array where letters are keys and values are true (clickable link) or false (just display). Initialize all values to false:
$letters = array_fill_keys(range('A', 'Z'), false);
Loop over the titles and set the value that corresponds to the first letter of each to be true:
foreach ($titles as $title) {
$letters[$title[0]] = true;
}
Then loop over the result and display a link or just the letter accordingly:
foreach ($letters as $letter => $link) {
if ($link) ...
else ...
}
A | B ....
$qry = "select <col_name> from <table_name> where <col_name> LIKE '".$_GET['alphabet']."%'";