Reputation: 2615
I have a simple PHP multi-dimension array that I'm wanting to loop through, specifically the array with the name License
and add all the values
item that start with the same first word to its own array (with the key set to that word).
The idea is I'd end up with something similar to this:
[desktop] => Array(
[0] => 1–3 users
[1] => 4–6 users
[2] => 7–10 users
)
[web] => Array(
[0] => 10k views/month
[1] => 25k views/month
[2] => 50k views/month
)
[app] => Array(
[0] => 5k downloads
[1] => 5k–10k downloads
[2] => 10k–25k downloads
)
This is an example of the array I'm working with $product['options']
Array
(
[0] => Array
(
[id] => 6059896471689
[product_id] => 4678842515593
[name] => Class
[position] => 1
[values] => Array
(
[0] => Regular
[1] => Oblique
[2] => Family
)
)
[1] => Array
(
[id] => 6355533201545
[product_id] => 4678842515593
[name] => License
[position] => 2
[values] => Array
(
[0] => Desktop 1–3 users
[1] => Desktop 4–6 users
[2] => Desktop 7–10 users
[3] => Desktop 11–15 users
[4] => Desktop 16–25 users
[5] => Desktop 25–50 users
[6] => Desktop 51–100 users
[7] => Desktop 101–150 users
[8] => Desktop 151–250 users
[9] => Desktop 251–400 users
[10] => Desktop 401–700 users
[11] => Desktop 701–1000 users
[12] => Desktop 1000+ users
[13] => Web 10k views/month
[14] => Web 25k views/month
[15] => Web 50k views/month
[16] => Web 100k views/month
[17] => Web 250k views/month
[18] => Web 500k views/month
[19] => Web 750k views/month
[20] => Web 1m+ views/month
[21] => App 5k downloads
[22] => App 5k–10k downloads
[23] => App 10k–25k downloads
[24] => App 25k–50k downloads
[25] => App 50–100k downloads
[26] => App 100k–250k downloads
[27] => App 250k–500k downloads
[28] => App 500k–1m downloads
[29] => App 1m+ downloads
)
)
)
And this is where I'm currently at:
foreach ($product['options'] as $option) {
if ($option['name'] == 'Style') {
$optionTitle = 'Select your fonts';
} elseif ($option['name'] == 'License') {
$optionTitle = 'Please select the type of license(s) you require';
} else {
$optionTitle = '';
}
$optionValues = array();
foreach ($option['values'] as $value) {
if ($option['name'] == 'License') {
// 3 different arrays to be added to $optionValues (desktop, web, app) HOW?
//substr(strstr($value, ' '), 1);
} else {
$optionValues[] = $value;
}
}
if ($option['name'] == 'License') {
$optionValues['desktop'] = ???;
$optionValues['web'] = ???;
$optionValues['app'] = ???;
}
$productOptions[] = array(
'name' => $option['name'],
'title' => $optionTitle,
'values' => $optionValues
);
}
Upvotes: 0
Views: 43
Reputation: 57121
To create the data split up as you want, you can just use explode()
with a space, but limit it to 2 items. The first is the type and the second is the range. Then add the items to the $optionValues
array using these values.
So something like ...
if ($option['name'] == 'License') {
list($type, $range) = explode(" ", $value, 2);
$optionValues[$type][] = $range;
} else {
You can also remove the following code...
if ($option['name'] == 'License') {
$optionValues['desktop'] = ???;
$optionValues['web'] = ???;
$optionValues['app'] = ???;
}
Upvotes: 2