如何从DB获取数组

时间:2016-08-05 08:22:34

标签: php mysql wordpress

我有一个问题。

我有一张table1这样的表:

ID | email | first_name | last_name
1  | bla   | bladibla   | bladibladibla

有了这个,我得到另一张桌子table2

ID | table1_id | name | value
1  | 1         | foo  | bar
2  | 1         | foo1 | bar1
3  | 1         | foo2 | bar2

我的问题:如何在php中获得结果?

$array = [
    id => 1,
    email => bla,
    first_name => bladibla,
    last_name => bladibladibla,
    data => [
        [
            name => foo,
            value => bar
        ],
        [
            name => foo1,
            value => bar1
        ],
        [
            name => foo2,
            value => bar2
        ],
    ]
]

我的解决方案目前有效,但似乎不是处理它的最佳方法。

我现在有这个,但它似乎不是正确的方法,因为在第一个foreach循环中,它为table1中的每一行向db发出一个查询。

$query = "SELECT * FROM `table1`";
$results = $wpdb->get_results($query);

foreach($results as $result) {
    $otherQuery = "SELECT * FROM `table2` WHERE `table1_id` = " . $result['id'];
    $table2Results = $wpdb->get_results($otherQuery);

    foreach($table2Results as $table2Result) {
        // save all data in a new array just like I want it.
    }
}

4 个答案:

答案 0 :(得分:0)

$query = "Select 
               t1.ID,t1.email,t1.first_name,t2.last_name,t2.name,t2.value
          From 
              table1 t1 
          Inner Join 
              table2 t2 
          On t1.ID=t2.table1_id";

$results = $wpdb->get_results($query);
$arr1=[];
$arr2=[];
foreach($results as $result) {       
    $arr1[]=['id'=>$result['ID'],'email'=>$result['email'],
             'first_name'=>$result['first_name'],
             'last_name'=>$result['last_name'],'data'=>[]];        
    $arr2[]=['name' => $result['name'],'value' => $result['value']];
}

$arr1=array_unique($arr1);
$arr1['data'][]=$arr2;

答案 1 :(得分:0)

试试这个:

$query = "SELECT * FROM `table1`";
$results = $wpdb->get_results($query);
$arr = []; // $arr = array();
foreach($results as $result) {
    $temp = $result;
    $otherQuery = "SELECT * FROM `table2` WHERE `table1_id` = " . $result['id'];
    $table2Results = $wpdb->get_results($otherQuery);

    foreach($table2Results as $table2Result) {
        // save all data in a new array just like I want it.
        $temp['data'][] = $table2Result
    }

    $arr[] = $temp;
}

print_($arr);

答案 2 :(得分:0)

$data = array();
$i = 0;
foreach($results as $result) 
{
        $data[$i] = $result;

        $otherQuery = "SELECT * FROM `table2` WHERE `table1_id` = " . $result['id'];
        $table2Results = $wpdb->get_results($otherQuery);

        $j = 0;
        $data[$i]['data'] = array();
        foreach($table2Results as $table2Result)
        {
                $data[$i]['data'][$j] = $table2Result;
                $j++;
        }
        $i++;
}
print_r($data);

答案 3 :(得分:0)

应该在JOIN中获取重复项,因为你的示例数组显示每个table1记录与多个table2记录相关。所以做多个查询并不能避免这种情况。我想你想要的是:

$query = "SELECT * FROM table1 LEFT JOIN table2 WHERE table1.id = table2.table1_id";
$results = $wpdb->get_results($query);

foreach($results as $result) {

    $table1_id = $result["table1.id"];

    $entries[$table1_id]['id'] = $table1_id;
    $entries[$table1_id]['email'] = $result["table1.email"];
    $entries[$table1_id]['first_name'] = $result["table1.first_name"];
    $entries[$table1_id]['last_name'] = $result["table1.last_name"];

    $entries[$table1_id]['data'][] = array($result["table2.name"], $result["table2.value"]);

}

print_r($entries) // the full array of table1 entries with the sub-array of table2 entries per table1 entry.

通过将每个数组条目键入table1.id,可以确保即使重复这些条目,table2条目也会作为新条目添加到数据数组中,而不会为主“外部”表1创建新的数组条目记录。

如果你不想在数组本身上使用table1.id键(并且有一个简单的基于0的索引数组,你可以在循环完成之后重新索引,如:

$entries = array_values($entries);