如何在关联数组中插入数据库的结果

时间:2017-12-13 10:16:07

标签: php wordpress

我有一个查询从database获取所有策略ID。我想将所有结果放在关联数组中,如下所示:

[0]=>Q-CID-14328097
[1]=>Q-CID-1507193624
[2]=>id3

这是我的代码:

global $wpdb;

$results = $wpdb->get_results($wpdb->prepare("SELECT id FROM wp_frm_items WHERE user_id=%d ORDER BY created_at DESC", 97));
$i = 0;

if ($results) {
    foreach($results as $row) {
        $policy_id = $wpdb->get_results($wpdb->prepare("SELECT meta_value FROM wp_frm_item_metas WHERE field_id=%d AND item_id=%s", 203, $row->id));
        $policy_transaction_type = $wpdb->get_results($wpdb->prepare("SELECT meta_value FROM wp_frm_item_metas WHERE field_id=%d AND item_id=%s", 390, $row->id));
        $policy_transaction_type = $policy_transaction_type[0]->meta_value;
        $policy_id = $policy_id[0]->meta_value;
        $array = array($policy_id);

}
}

但是这会回来:

Array ( [0] => Q-CID-14328097 ) Array ( [0] => ) Array ( [0] => Q-CID-14328097 ) Array ( [0] => ) Array ( [0] => Q-CID-14328097 ) Array ( [0] => Q-CID-1507193624 ) Array ( [0] => Q-CID-1510050174 ) Array ( [0] => Q-CID-1511814120 ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => ) Array ( [0] => )

1 个答案:

答案 0 :(得分:0)

每次都设置$ array。您可以在循环之前设置一次,然后向其中插入元素:

ipa=$(ifconfig | grep -Po "inet addr:\K[^\s]+" | grep -v "^127")

这就是全部