php - How to arrange array with proper value in main array -
currently trying assign value in array , existing array looks like..
array ( [detals] => array ( [0] => array ( [make] => xyx [yeardeta] => 2001 ) [1] => array ( [model] => array ( [0] => test 1 [1] => test fix [2] => hamour [3] => imagica ) ) [2] => array ( [make] => xyx [yeardeta] => 2002 ) [3] => array ( [model] => array ( [0] => test 2 ) ) ) )
i want output ..
array ( [detals] => array ( [0] => array ( [make] => xyx [yeardeta] => 2001 [model] => array ( [0] => test 1 [1] => test fix [2] => hamour [3] => imagica ) ) [2] => array ( [make] => xyx [yeardeta] => 2002 [model] => array ( [0] => test 2 ) ) ) )
and full php code
<?php $data = array(); $cat = get_terms('car_category'); foreach ($cat $catval) { $term_id =$catval->term_id; $get_query = $wpdb->get_results("select distinct meta_value wp_postmeta pm left join wp_term_relationships tr on tr.object_id=pm.post_id pm.meta_key='car_year' , tr.term_taxonomy_id='".$term_id."'"); foreach ($get_query $key => $value) { $data[] = array('make'=>$catval->term_id,'yeardeta'=>$value->meta_value); $get_query = $wpdb->get_results("select post_title wp_posts p left join wp_postmeta pm on p.id=pm.post_id left join wp_term_relationships tr on tr.object_id=pm.post_id pm.meta_key='car_year' , pm.meta_value='".$value->meta_value."' , tr.term_taxonomy_id='".$term_id."'"); $str = array(); foreach ($get_query $key => $value) { $str[] = $value->post_title; } $data[] = array('model'=>$str); } $term_id =$catval->term_id; $main_array = array('detals'=>$data); print_r($main_array); } ?>
i want output have explain above how can ?
i have modified code pasted bellow. in loop used $data[]
twice new array element inserted. please check following code (i not test), modify required , let me know clarifications:
<?php $data = array(); $cat = get_terms('car_category'); foreach ($cat $catval) { $term_id =$catval->term_id; $get_query = $wpdb->get_results("select distinct meta_value wp_postmeta pm left join wp_term_relationships tr on tr.object_id=pm.post_id pm.meta_key='car_year' , tr.term_taxonomy_id='".$term_id."'"); foreach ($get_query $key => $value) { $make = $catval->term_id; $yeardata = $value->meta_value; $get_query_model = $wpdb->get_results("select post_title wp_posts p left join wp_postmeta pm on p.id=pm.post_id left join wp_term_relationships tr on tr.object_id=pm.post_id pm.meta_key='car_year' , pm.meta_value='".$value->meta_value."' , tr.term_taxonomy_id='".$term_id."'"); $model = array(); foreach ($get_query_model $model_key => $model_value) { $model[] = $model_value->post_title; } $data[] = array('make'=>$make,'yeardata'=>$yeardata, 'model'=>$model); } $term_id =$catval->term_id; $main_array = array('detals'=>$data); print_r($main_array); } ?>
Comments
Post a Comment