简体   繁体   中英

MySQL query and array output from same database column

I'm still quite new to PHP & MySql and I'm not sure whether this would be acheived as SQL or PHP.

I have the following query:

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

This is from my Wordpress database and as you can see the longitude and latitude values are under the same column "meta_key".

The array from the above query gives me similar results to the below (not exact just rough example):

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

Being from the same column of the database I don't know if its possible to fix in my query or whether its a PHP fix, I need either the items with same ID to be in the same array and possibly the renaming of the keys, if possible like below:

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

You can change your sql to get the meta_key field as well:

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

Then you can iterate the returned array, and populate a new one using the ID values as the index. You can check if an item with the current id exists in the new array, with isset() . If so, add to it (eg add the latitude value) else create the item:

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

I think that first of all you have to add in your query a "type" value that indicates if a meta_value is "longitude" o "latitude":

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

In this way, your autoput array will be like this:

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

Then you can use a cyle to popolate an array, using particula conditions:

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

At this point you have an array like this:

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

If you want exactly the output structure that you indicate in you question, you can iterate on this array to obtain what you want:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM