我查询了我的数据库并将结果存储到数组中。它看起来像这样:
$query1 = $wpdb->prepare(
"SELECT DISTINCT meta_value FROM {$wpdb->prefix}posts,{$wpdb->prefix}postmeta WHERE meta_key LIKE %s AND post_name = %s",
'dich_vu_%_name_service',
'bang-gia-dich-vu'
);
$query2 = $wpdb->prepare(
"SELECT DISTINCT meta_value FROM {$wpdb->prefix}posts,{$wpdb->prefix}postmeta WHERE meta_key LIKE %s AND post_name = %s",
'dich_vu_%_price_service',
'bang-gia-dich-vu'
);
$results = $wpdb->get_results( $query1.' UNION '.$query2);
这全部放在名为Array
(
[0] => Array
(
[Submission_ID] => 111
[First_Name] => Dylan
[Last_Name] => Taylor
[Abstract_Title] => Research 1
)
[1] => Array
(
[Submission_ID] => 222
[First_Name] => Michael
[Last_Name] => Jones
[Abstract_Title] => Research 2
)
[2] => Array
(
[Submission_ID] => 333
[First_Name] => Wills
[Last_Name] => Adams
[Abstract_Title] => Research 3
)
)
的变量中。目前,我正在我的页面上显示一些数据:
$results
一切正常,但是,如果索引键被替换为提交ID:
,则很方便echo $results[0][Abstract_Title]
所以我可以这样做(否则我必须打印数组只是为了每次都查找索引键):
Array
(
[111] => Array
(
[Submission_ID] => 111
[First_Name] => Dylan
[Last_Name] => Taylor
[Abstract_Title] => Research 1
)
[222] => Array
(
[Submission_ID] => 222
[First_Name] => Michael
[Last_Name] => Jones
[Abstract_Title] => Research 2
)
[333] => Array
(
[Submission_ID] => 333
[First_Name] => Wills
[Last_Name] => Adams
[Abstract_Title] => Research 3
)
)
有什么想法吗? (我是编程新手。)我找到的最接近的答案是:php replace array id keys但我无法理解解决方案。我认为echo $results[111][Abstract_Title]
是最好的选择吗?新数组应该放在同一个变量还是新变量中?
答案 0 :(得分:4)
您可以像{/ p>一样使用array_combine
和array_column
$final_result = array_combine(array_column($result,'Submission_ID'),$result);
print_r($final_result);