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...
Related
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#')
Using PHP I am running a query on my database and as a result I get a data-set with optional values. An example:
Result:
Array
(
[0] => Array
(
[attribute_1] => Red,
[attribute_2] => Car,
[name] => Sportscar
)
[1] => Array
(
[attribute_1] => Red,
[attribute_2] => ,
[name] => Rose
)
)
I want to format the resulting Data as such:
Array
(
[Red] => Array
(
[Car] => Array
(
[0] => Sportscar
)
)
[0] => Rose
)
So that I could access the Sportscar by
$array['Red']['Car'][0];
And the Rose by
$array['Red'][0]
This will make it easier to output the data with a nice little foreach loop.
of course the actual case is a bit more complex with a lot more values etc but this example demonstrates the principle.
The problem is that I can't think of a proper way to format the data efficiently. Not necessarily recursive but at least not one million if and elses would be nice.
Any ideas?
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'm starting out with this array, of which I only need the numbered keys:
Array
(
[4118] => Car
[4668] => Bus
)
and I've whittled it down to this:
Array
(
[0] => 4118
[1] => 4668
)
but for some reason, drupal and the code I'm working with will only fully accept an array in this format (and also my preferred format):
array(4118,4668);
or this one:
array(0 => 4118,1 => 4668);
Does anyone know how to do this?
There is array_keys() just for that.
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 :(