php - Modify an array -
i've got array generated mysql query database it's :
array ( [0] => array ( [0] => array ( [id_device] => 1 [device_name] => iphone 5 [device_brand] => apple ) [1] => array ( [id_device] => 2 [device_name] => iphone 4/4s [device_brand] => apple ) ) [1] => array ( [0] => array ( [id_device] => 3 [device_name] => galaxy s4 [device_brand] => samsung ) [1] => array ( [id_device] => 4 [device_name] => galaxy s3 [device_brand] => samsung ) ) )
i'd like :
array ( [apple] => array ( [0] => array ( [id_device] => 1 [device_name] => iphone 5 ) [1] => array ( [id_device] => 2 [device_name] => iphone 4/4s ) ) [samsung] => array ( [0] => array ( [id_device] => 3 [device_name] => galaxy s4 ) [1] => array ( [id_device] => 4 [device_name] => galaxy s3 ) ) )
i don't understand logic in array (i'm pretty sure it's because i'm stressed , busy though). can me ?
another solution change mysql query maybe, i'm obtaining array php class in use 2 methods :
public static function getdevicesbrands() { $sql = 'select distinct '._db_prefix_.'device.device_brand '._db_prefix_.'device'; $rq = db::getinstance(_ps_use_sql_slave_)->executes($sql); return ($rq); } public static function getdevicesbybrand($brand) { $sql = 'select id_device, device_name, device_brand '._db_prefix_.'device '._db_prefix_.'device.device_brand = "'.$brand.'"'; $rq = db::getinstance(_ps_use_sql_slave_)->executes($sql); return ($rq); }
in controller shown array using piece of code :
$device_brand = device::getdevicesbrands(); $row = count($device_brand); $devices_by_brand = array(); ($i = 0; $i <= $row - 1; $i++) { array_push($devices_by_brand, device::getdevicesbybrand($device_brand[$i]['device_brand'])); } echo"<pre>"; print_r($devices_by_brand); echo"</pre>";
changing for
-loop in controller should trick:
for ($i = 0; $i <= $row - 1; $i++) { $brand = $device_brand[$i]['device_brand']; $devices_by_brand[$brand] = device::getdevicesbybrand($brand); }
Comments
Post a Comment