目前我正在尝试在数组中赋值,这是我现有的数组看起来像..
Array
(
[detals] => Array
(
[0] => Array
(
[make] => xyx
[yeardeta] => 2001
[model] => Array
(
[0] => Test one
[1] => test fix
[2] => Hamour
[3] => Imagica
)
)
[2] => Array
(
[make] => xyx
[yeardeta] => 2002
[model] => Array
(
[0] => Test Two
)
)
)
)
我希望它像这样输出..
<?php
$data = array();
$cat = get_terms('car_category');
foreach ($cat as $catVal) {
$term_id =$catVal->term_id;
$get_query = $wpdb->get_results("SELECT DISTINCT meta_value FROM wp_postmeta as pm
LEFT JOIN wp_term_relationships as tr
ON tr.object_id=pm.post_id
WHERE pm.meta_key='car_year'
AND tr.term_taxonomy_id='".$term_id."'");
foreach ($get_query as $key => $value) {
$data[] = array('make'=>$catVal->term_id,'yeardeta'=>$value->meta_value);
$get_query = $wpdb->get_results("SELECT post_title FROM wp_posts as p
LEFT JOIN wp_postmeta as pm
ON p.id=pm.post_id
LEFT JOIN wp_term_relationships as tr
ON tr.object_id=pm.post_id
WHERE pm.meta_key='car_year'
AND pm.meta_value='".$value->meta_value."'
AND tr.term_taxonomy_id='".$term_id."'");
$str = array();
foreach ($get_query as $key => $value) {
$str[] = $value->post_title;
}
$data[] = array('model'=>$str);
}
$term_id =$catVal->term_id;
$main_array = array('detals'=>$data);
print_r($main_array);
}
?>
这是我的完整PHP代码
{{1}}
我想输出我上面已解释过我该怎么做?
答案 0 :(得分:0)
我已修改了粘贴在下面的代码。在循环中,您使用了$data[]
两次,因此插入了新的数组元素。请检查以下代码(我无法测试),根据需要进行修改,并告知我任何说明:
<?php
$data = array();
$cat = get_terms('car_category');
foreach ($cat as $catVal) {
$term_id =$catVal->term_id;
$get_query = $wpdb->get_results("SELECT DISTINCT meta_value FROM wp_postmeta as pm
LEFT JOIN wp_term_relationships as tr
ON tr.object_id=pm.post_id
WHERE pm.meta_key='car_year'
AND tr.term_taxonomy_id='".$term_id."'");
foreach ($get_query as $key => $value) {
$make = $catVal->term_id;
$yeardata = $value->meta_value;
$get_query_model = $wpdb->get_results("SELECT post_title FROM wp_posts as p
LEFT JOIN wp_postmeta as pm
ON p.id=pm.post_id
LEFT JOIN wp_term_relationships as tr
ON tr.object_id=pm.post_id
WHERE pm.meta_key='car_year'
AND pm.meta_value='".$value->meta_value."'
AND tr.term_taxonomy_id='".$term_id."'");
$model = array();
foreach ($get_query_model as $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);
}
?>
答案 1 :(得分:0)
而不是
\\file\
替换为
$data[] = array('model'=>$str);
上面的内容在$data['model'] = $str;
中创建了一个新数组。
追加 - &gt;使用$data['newkey']
中的model
键。