changing php array to show distinct value and array of product ids -


i'm not sure start one, , brain fried right now. thought i'd ask help! i'm working on opencart site, , i'm trying change array groups product_ids categories.

here's array it's spitting out:

array (     [0] => array         (             [product_id] => 50             [category] => shop carton         )      [1] => array         (             [product_id] => 52             [category] => shop carton         )      [2] => array         (             [product_id] => 53             [category] => shop carton         )      [3] => array         (             [product_id] => 54             [category] => shop carton         )      [4] => array         (             [product_id] => 55             [category] => shop box         )      [5] => array         (             [product_id] => 56             [category] => shop box         )      [6] => array         (             [product_id] => 57             [category] => shop box         )      [7] => array         (             [product_id] => 58             [category] => shop box         )  ) 

but here's i'm hoping get:

array (     [0] => array         (             [category] => shop carton             [product_id] => array              (                 [product_id] => 50                 [product_id] => 52                 [product_id] => 53                 [product_id] => 54             )         )      [1] => array         (             [category] => shop box             [product_id] => array              (                 [product_id] => 55                 [product_id] => 56                 [product_id] => 57                 [product_id] => 58             )         ) ) 

here's code creating array:

$data['children']           = $this->model_catalog_product->getproductchildren($this->request->get['product_id']);                     $data['options']            = array();                     $product_option_value_data  = array();                      foreach ($data['children'] $product) :                          foreach ($this->model_catalog_product->getproductoptions($product['product_id']) $option) :                              foreach ($option['product_option_value'] $option_value) :                                 $product_option_value_data = $option['name'].' '.$option_value['name'];                             endforeach;                              $data['options'][] = array(                                 'product_id'    => $product['product_id'],                                 'category'      => $product_option_value_data                             );                          endforeach;                      endforeach;                      echo '<pre>';print_r($data['options']);echo '</pre>'; 

if can me achieve i'm after, fantastic.

first thing

array          (             [product_id] => 50             [product_id] => 52             [product_id] => 53             [product_id] => 54         ) 

is not possible cause key duplicate change little bit try code

$lists = [     [         "product_id" => 50,         "category"   => "shop carton"     ],[         "product_id" => 52,         "category"   => "shop carton"     ],[         "product_id" => 53,         "category"   => "shop carton"     ],[         "product_id" => 54,         "category"   => "shop carton"     ],[         "product_id" => 55,         "category"   => "shop box"     ],[         "product_id" => 56,         "category"   => "shop box"     ],[         "product_id" => 57,         "category"   => "shop box"     ],[         "product_id" => 58,         "category"   => "shop box"     ] ];  //define temp data $tmps = [];  foreach ($lists $list) {     $cat = $list["category"];     $pro = $list["product_id"];      if (!isset($tmps[$cat])) {         $tmps[$cat] = [];     }      //add product temp     $tmps[$cat][] = ["product_id" => $pro]; }  //define output $outputs = [];  //format data temp foreach ($tmps $cat => $pros) {     $outputs[] = [         "category"   => $cat,         "product_id" => $pros,     ]; }  print_r($outputs);  

hope help.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -