2014-07-04 95 views
0

我還是很新的PHP & MySql,我不確定這是否會被視爲SQL或PHP。MySQL查詢和數組輸出從同一個數據庫列

我有以下查詢:

SELECT wp_posts.ID, 
    wp_postmeta.meta_value 
FROM wp_posts 
    INNER JOIN wp_postmeta 
     ON wp_posts.ID = wp_postmeta.post_id 
WHERE wp_posts.post_type = 'cooney_properties' 
AND wp_posts.post_status = 'publish' 
AND (wp_postmeta.meta_key = 'longitude' OR wp_postmeta.meta_key = 'latitude') 

這是從我的WordPress的數據庫,你可以看到的經度和緯度值正在同列「meta_key」。

從上述查詢數組給了我相似的結果如下(不準確只是粗略的例子):

Array 
    (
    [0] => stdClass Object 
    (
     [ID] => 25016273 
     [meta_value] => 51.01454 
    ) 
    [1] => stdClass Object 
    (
     [ID] => 24617570 
     [meta_value] => 51.01447 
    ) 
    [2] => stdClass Object 
    (
     [ID] => 24780750 
     [meta_value] => 51.01535 
    ) 

    ------------------- (later on items with same ID) ------------- 

    [141] => stdClass Object 
    (
     [ID] => 24617570 
     [meta_value] => -3.107139 
    ) 
    [142] => stdClass Object 
    (
     [ID] => 24780750 
     [meta_value] => -3.096807 
    ) 
    [143] => stdClass Object 
    (
     [ID] => 24764956 
     [meta_value] => -3.104187 
    ) 
    ) 

從數據庫的同一列作爲我不知道,如果它可以修復我的查詢還是它的一個PHP修復,我需要或者有相同ID的項目是相同的數組,並可能鍵的重新命名,如果可能的話象下面這樣:

Array 
    (
    [0] => stdClass Object 
    (
     [ID] => 25016273 
     [longitude] => 51.01454 
     [latitude] => -3.107139 
    ) 
    [1] => stdClass Object 
    (
     [ID] => 24617570 
     [longitude] => 51.01447 
     [latitude] => -3.096807 
    ) 
    [2] => stdClass Object 
    (
     [ID] => 24780750 
     [longitude] => 51.01535 
     [latitude] => -3.104187 
    ) 
    ) 

回答

0

您可以在SQL改變獲得meta_key字段:

SELECT wp_posts.ID, 
    wp_postmeta.meta_value, 
    wp_postmeta.meta_key 
FROM wp_posts 
    INNER JOIN wp_postmeta 
     ON wp_posts.ID = wp_postmeta.post_id 
WHERE wp_posts.post_type = 'cooney_properties' 
AND wp_posts.post_status = 'publish' 
AND (wp_postmeta.meta_key = 'longitude' OR wp_postmeta.meta_key = 'latitude') 

然後,您可以迭代返回的數組,然後使用ID值作爲索引填充新的數組。您可以檢查新陣列中是否存在具有當前編號的項目,其中isset()。如果是的話,給它添加(如添加的緯度值),否則創建項目:

$new=array(); 

foreach($result as $item){ 

    if(isset($new[$item->ID])){ 

     $new[$item->ID][$item->meta_key] = $item->meta_value; 

    }else{ 

     $new[$item->ID] = array('ID' => $item->ID, $item->meta_key => $item->meta_value); 
    } 
} 
print_r($new); 
0

我認爲,首先你必須對您的查詢「類型」值增加,表明如果一個meta_value是「經度」 O 「緯度」:

SELECT wp_posts.ID, 
    wp_postmeta.meta_value, 
    wp_postmeta.meta_key as type 
FROM wp_posts 
    INNER JOIN wp_postmeta 
     ON wp_posts.ID = wp_postmeta.post_id 
WHERE wp_posts.post_type = 'cooney_properties' 
AND wp_posts.post_status = 'publish' 
AND (wp_postmeta.meta_key = 'longitude' OR wp_postmeta.meta_key = 'latitude') 

這樣,你的autoput陣列將是這樣的:

Array 
    (
    [0] => stdClass Object 
    (
     [ID] => 25016273 
     [meta_value] => 51.01454 
     [type] => 'longitude' 

    ) 
    [1] => stdClass Object 
    (
     [ID] => 24617570 
     [meta_value] => 51.01447 
     [type] => 'longitude' 
    ) 
    [2] => stdClass Object 
    (
     [ID] => 24780750 
     [meta_value] => 51.01535 
     [type] => 'longitude' 
    ) 

    ------------------- (later on items with same ID) ------------- 

    [141] => stdClass Object 
    (
     [ID] => 24617570 
     [meta_value] => -3.107139 
     [type] => 'latitude' 
    ) 
    [142] => stdClass Object 
    (
     [ID] => 24780750 
     [meta_value] => -3.096807 
     [type] => 'latitude' 
    ) 
    [143] => stdClass Object 
    (
     [ID] => 24764956 
     [meta_value] => -3.104187 
     [type] => 'latitude' 
    ) 

然後你可以使用一個cyle到popolate一個數組,使用particula條件:

$result = array(); 
foreach(your_array AS $k => $v) { 
    $result[$v->id][$v->type] = $v->meta_value; 
} 

此時你有一個這樣的數組:

array(
    [25016273] = array(
    ['longitude'] => 51.01454, 
    ['latitude'] => -3.104187 
), 
    [24617570] = array(
    ['longitude'] => 51.01454, 
    ['latitude'] => -3.104187 
) 
    .... 
) 

如果你想要的是你表明你的問題,你可以在這個陣列上進行迭代,以獲得您想要的輸出結構:

$result = array() 
$i = 0 
foreach ($new_array as $k => $v) { 
    result[$i]->id = $k; 
    if (isset($v['longitude']) result[$i]->longitude = $v['longitude']; 
    if (isset($v['latitude'])result[$i]->latitude = $v['latitude']; 
    $i++; 
}