0

这是我使用的查询:

$idList="32,33,21,11";          
$query = "SELECT post_id, meta_value FROM wp_posts 
          WHERE meta_key = 'grade' AND post_id IN ($idList)";

结果,where 子句成为键:

$result = array(
              0 => array('post_id'=> 32, 'meta_value'=>5),
              1 => array('post_id'=> 33, 'meta_value'=>2),
              2 => array('post_id'=> 21, 'meta_value'=>8),
           )

我希望有这样的结果:

$result = array(
              0 => array( 32 => 5 ),
              1 => array( 33 => 2 ),
              2 => array( 21 => 8 ),
           )

如何做到这一点?谢谢!

4

1 回答 1

1

像这样循环结果集:

$_final = array(); // new result set

foreach ($result as $value)
  $_final[] = array($value['post_id'] => $value['meta_value']);

unset($result); // free memory from old result set

更新:如果元素太多并且您担心内存消耗,您可以通过以下方式进行操作:

$_final = array(); // new result set

foreach ($result as $key => $value) {
  $_final[] = array($value['post_id'] => $value['meta_value']); // composing new data
  unset($result[$key]); // unsetting already parsed value
}

unset($result); // freeing memory from old result set
于 2012-08-16T16:07:25.183 回答