MySQL查询和来自同一数据库列的数组输出

时间:2014-07-04 14:31:14

标签: php mysql sql arrays wordpress

我还不熟悉PHP& MySql和我不确定这是否会被用作SQL或PHP。

我有以下查询:

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
    )
   )

2 个答案:

答案 0 :(得分: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()检查新数组中是否存在具有当前ID的项目。如果是这样,添加到它(例如添加纬度值),否则创建项目:

$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);

答案 1 :(得分: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来使用特定条件来弹出一个数组:

$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++;
}