I am trying to create little app that randomly selects a number of chords/notes from a scale but I am having trouble with using the array_rand to get those random values from an array.
I have an array called $scale that looks like this:
Array
(
[0] => f#
[1] => g#
[2] => a#
[3] => b
[4] => c#
[5] => d#
[6] => f
)
I also have an array called $chord_amt:
$chord_amt = array(2,3,4,5,6);
I have used the array_rand function to randomly select one item from the array like so:
$selected_chord_amt = $chord_amt[array_rand($chord_amt)];
Now I want to output whatever number of random chords that this function can produce:
$random_chords = array_rand($scale, $selected_chord_amt);
The problem is, if I print this array, instead of seeing the chord/note values it just shows the keys like this example:
Array ( [0] => 3 [1] => 4 )
How do I get the actual values, so the above output would look like this?
Array ( [0] => b [1] => c# )
Noob question, I know. Sorry.
I'd suggest using a different approach, actually. Using array_rand on this array
$chord_amt = array(2,3,4,5,6);
isn't really needed to generate a random number between 2 and 6. PHP has the rand function for that.
Consider the following instead:
// shuffle the list of chords
shuffle($scale);
// take a slice of it with a random length between 2 and 6
$random_chords = array_slice($scale, 0, rand(2, 6));
Just introduce array_flip with your array_rand:
$random_chords = array_rand(array_flip($scale), $selected_chord_amt);
Prints something like this:
Array
(
[0] => f#
[1] => c#
[2] => d#
)
Like this
$intersect = array_intersect_key($scale, array_flip($random_chords));
Note, I didn't test this, because I'm too lazy to rewrite your arrays, but here's the documentation.
http://php.net/manual/en/function.array-intersect-key.php
http://php.net/manual/en/function.array-flip.php
This is the question as I see it,
How to get a set of items from one array with the keys based on the
values of another array
All random things aside, you have this array
Array
(
[0] => f#
[1] => g#
[2] => a#
[3] => b
[4] => c#
[5] => d#
[6] => f
)
And then you want to get those values based on these keys
Array( 3,4 )
The answer being an array like this
Array('b','c#')
Related
STARTING ARRAY
Array
(
[0] => Array
(
[0] => /searchnew.aspx?Make=Toyota&Model=Tundra&Trim=CrewMax+5.7L+V8+6-Spd+AT+SR5&st=Price+asc
[1] => 19
)
)
I have been struggling to break down this array for the past couple days now. I have found a few useful functions to extract the strings I need when a start and end point are defined, however, I can't see that being good for long term use. Basically I'm trying to take the string relative to [0], and extract the strings following "Model=" and "Trim=", in hopes to have array like this:
Array
(
[0] => Array
(
[0] => Tundra ***model***
[1] => CrewMax+5.7L+V8+6-Spd+AT+SR5 ***trim***
[2] => 19
)
)
I'm getting this information fed through an api, so coming up with a dynamic solution is my biggest challenge. I realize this a big question, but is there a better/less hacky way of approaching this problem?
parse_url() will get you the query string and parse_str() parses the variables from that:
$q = parse_url($array[0][0], PHP_URL_QUERY);
parse_str($q, $result);
print_r($result);
Yields:
Array
(
[Make] => Toyota
[Model] => Tundra
[Trim] => CrewMax 5.7L V8 6-Spd AT SR5
[st] => Price asc
)
Now just echo $result['Model'] etc...
I looked at different options how to sort arrays. But somehow none of the given PHP commands suit my purpose.
Example - I have an array like this :
Array
(
[abc] => Array
(
[2] => 2
[3] => 3
[5] => 5
)
)
But I want to change the array to
[0] => 2
[1] => 3
[2] => 5
In other words I want to remove all keys - sort all values from LOW to HIGH and then just give em the keys from zero to X
It's much easier to work with such an array if you want to use some loops like (for, while, etc.)
Just use sort and array_values.
<?php
$array = array(
'abc' => array(
2 => 2,
5 => 5,
3 => 3,
),
);
sort($array['abc']);
$array = array_values($array['abc']);
print_r($array);
I've popped up an example at http://3v4l.org/51naW
Array
(
[0] => LAMPION
[1] => BANBU
[2] => DT-T300-FNS
[3] => T65
[4] => DT-299-FNS
[5] => T30
)
I have an array looking like this. The problem is the data stored in the array is not consistent so i have to search the array for this pattern "xx-xxx-xxx" and store it in a variable. is there any way i can do that? really need hlp
Yes.
$matches = preg_grep('/^.{2}-.{3}-.{3}\z/', $array);
If you want the first, just add [0] (you'll need a temporary variable first for < PHP 5.4).
I want to change the number in the first array in a multidimensional array. I have a code that outputs the value to an array and there is no chance for it to start counting from one - in my code. So my idea is to change the value starting from one - after it has been declared. My array look like this:
Array
(
[53] => Array
(
[name] => Volkswagen
[regularePrice] => 2139.00
)
[54] => Array
(
[name] => BMW
[regularePrice] => 2219.00
)
[55] => Array
(
[name] => Chrysler
[regularePrice] => 2399.00
)
)
I want - through a while or for - go through the array and change the values 53 to 1, 54 to 2, 55 to 3 and so on depending on how long the array is.
How do I accomplish this?
The answer is:
array_values($arr);
did you try:
$array = array_values($array);
I'm not even entirely sure how to ask the question, but assuming I have an array in memory like this:
Array (
[0] => Array
(
[0] => Array
(
[0] => 18451
[1] => MDX
)
[1] => Array
(
[0] => 18450
[1] => NSC
)
[2] => Array
(
[0] => 18446
[1] => RL
)
)
)
Is there some existing functionality to turn it into the code version of that array? I have a # of arrays I need to do this for, nested to various degrees. So I imagine I want output something like
$arrayname[] = array(array('18451','MDX'),array('18450','NSC'),array('18446','RL'));
I can write something to do it, but I'd rather not recreate the wheel if there's an existing way to do it.
This may be all you need:
http://us.php.net/var_export
manually loop through the array recursively and create a string like how you want.
var_export could help you there i think. The other option would be looping through it manually :(