what is the best way fetch mysql data into Multi-Dimensional Arrays?
SQL:
SELECT tariff, monthly_cost, point, phones.name FROM phoneplanpoint
LEFT JOIN phones on phoneplanpoint.phone_id = phones.phone_id
LEFT JOIN price_plans on price_plans.priceplan_id = phoneplanpoint.priceplan_id
Output:
tariff monthly_cost point name
Matrix 15.00 6.0 Blackberry
Matrix 20.00 10.0 Blackberry
Lion 15.00 12.5 Blackberry
Lion 20.00 14.5 Blackberry
Matrix 15.00 6.5 Iphon开发者_运维百科e
Matrix 20.00 7.5 Iphone
I am trying put that data into Multi-Dimensional Arrays, so it will be easy to prepare for layout view to output data.
This is a quick example for testing, is this correct way? Ofcourse I will use loop.
$Priceplan['Blackberry'] = array();
$Priceplan['Blackberry']['tariff'][] = array("Name" => "Matrix 15.00", "Point" => "6.0");
$Priceplan['Blackberry']['tariff'][] = array("Name" => "Matrix 20.00", "Point" => "10.0");
I want the view layout data look like this:
The following should get you going assuming that the data returned by your query is formatted like this :
$data = array(
array(
'tariff' => 'Matrix',
'monthly_cost' => '15.00',
'point' => '6.0',
'name' => 'Blackberry'
),
array(
'tariff' => 'Matrix',
'monthly_cost' => '20.00',
'point' => '10.0',
'name' => 'Blackberry'
),
array(
'tariff' => 'Lion',
'monthly_cost' => '15.00',
'point' => '12.5',
'name' => 'Blackberry'
),
array(
'tariff' => 'Lion',
'monthly_cost' => '20.00',
'point' => '14.5',
'name' => 'Blackberry'
),
array(
'tariff' => 'Matrix',
'monthly_cost' => '15.00',
'point' => '6.5',
'name' => 'Iphone'
),
array(
'tariff' => 'Matrix',
'monthly_cost' => '20.00',
'point' => '7.5',
'name' => 'Iphone'
)
);
There might be a better way of doing this, but its at least a beginning
foreach ($data as $plan) {
if (!isset($output[$plan['name']])) {
$output[$plan['name']] = array();
}
$options = array(
'name' => $plan['tariff'],
'price' => $plan['monthly_cost'],
'point' => $plan['point']
);
$output[$plan['name']][] = $options;
}
var_dump($output);
Which will output your data like that
array(2) {
["Blackberry"] => array(4) {
[0] => array(3) {
["name"] => string(6) "Matrix"
["price"] => string(5) "15.00"
["point"] => string(3) "6.0"
}
[1] => array(3) {
["name"] => string(6) "Matrix"
["price"] => string(5) "20.00"
["point"] => string(4) "10.0"
}
[2] => array(3) {
["name"] => string(4) "Lion"
["price"] => string(5) "15.00"
["point"] => string(4) "12.5"
}
[3] => array(3) {
["name"] => string(4) "Lion"
["price"] => string(5) "20.00"
["point"] => string(4) "14.5"
}
}
["Iphone"] => array(2) {
[0] => array(3) {
["name"] => string(6) "Matrix"
["price"] => string(5) "15.00"
["point"] => string(3) "6.5"
}
[1] => array(3) {
["name"] => string(6) "Matrix"
["price"] => string(5) "20.00"
["point"] => string(3) "7.5"
}
}
}
And finally, if you want to make sure that it work well, be sure to add order by in your query (on the phone name and the tariff). This code won't take care of duplicates. If you want to pimp this code a bit you could complete with array_unique and make sure that you use the SORT_REGULAR optionnal parameter.
精彩评论