我制作了一个代码来获取特定帖子类型cef_donor的所有元数据
$arg = array(
'post_type' => 'cef_donor',
);
$posts_array = get_posts($arg);
$metaData = array();
foreach($posts_array as $key => $value){
$metaData[] = get_post_meta($value->ID,'', true);
}
当我打印$ metaData时,我得到这样的数组
Array
(
[0] => Array
(
[meta_key_one] => Array (
[0] => value one
)
[meta_key_two] => Array
(
[0] => value two
)
[meta_key_three] => Array
(
[0] => value three
)
)
[1] => Array
(
[meta_key_one] => Array (
[0] => another value one
)
[meta_key_two] => Array
(
[0] => another value two
)
[meta_key_three] => Array
(
[0] => another value three
)
)
)
但我只需要一个像这样的数组
Array
(
[0] => Array
(
[meta_key_one] => value one
[meta_key_two] => value two
[meta_key_three] => value three
)
[1] => Array
(
[meta_key_one] => another value one
[meta_key_two] => another value two
[meta_key_three] => another value three
)
)
有没有其他简单的方法来获取帖子类型的所有meta_key及其meta_value。 请帮忙
答案 0 :(得分:0)
您可以创建一个新的。将旧值循环到新值。考虑这个例子:
$metaData = array( array( 'meta_key_one' => array('value_one'), 'meta_key_two' => array('value_two'), 'meta_key_three' => array('value_three'), ), array( 'meta_key_one' => array('another_value_one'), 'meta_key_two' => array('another_value_two'), 'meta_key_three' => array('another_value_three'), ),);
$new_values = array();
foreach($metaData as $key => $value) {
foreach($value as $index => $element) {
$new_values[$key][$index] = $element[0];
}
}
print_r($new_values);
示例输出:
Array
(
[0] => Array
(
[meta_key_one] => value_one
[meta_key_two] => value_two
[meta_key_three] => value_three
)
[1] => Array
(
[meta_key_one] => another_value_one
[meta_key_two] => another_value_two
[meta_key_three] => another_value_three
)
)