0

我试图在特定的自定义字段中提供一个包含所有值的数组。值本身也是数组。我尝试了各种数组函数,但没有遇到正确的一个或正确的组合。到目前为止,这是我的代码:

$args = array(
    'post_type' => 'match_report',
    'post_status' => 'publish', 
    'meta_query' => array(
    'relation' => 'OR',
        array(
            'key' => 'report_home-scorers'
        ),
        array(
            'key' => 'report_away-scorers'
        )
    )
);

$reportscore = new WP_Query($args); 
$scorersResults = array();

if ( $reportscore->have_posts() ) {
    while ( $reportscore->have_posts() ) { 
        $reportscore->the_post();

$homescorers = get_post_meta($post->ID,'report_home-scorers',false);
$awayscorers = get_post_meta($post->ID,'report_away-scorers',false);            

foreach ($homescorers as $homescorer){
 array_push($scorersResults, $homescorer);
}
 foreach ($awayscorers as $awayscorer){
 array_push($scorersResults, $awayscorer);
}
?>

<?php } wp_reset_postdata(); //endif
}//endwhile

$scorerResults = remove_empty($scorersResults);

function remove_empty($array) {
return array_filter($array, '_remove_empty_internal');
}

function _remove_empty_internal($value) {
return !empty($value) || $value === 0;
} 

如果我这样做,我会得到什么print_r($scorerResults);

Array
(
[1] => Array
    (
        [0] => 1
        [1] => 63
    )

[2] => Array
    (
        [0] => 263
        [1] => 195
    )

[3] => Array
    (
        [0] => 
    )

[4] => Array
    (
        [0] => 
    )
)

我只想要数组中内部数组中的值。

4

1 回答 1

0

假设您希望$scoreResults数组结束,因为array(1,63,263,195)您可以使用如下array_reduce函数:

function gatherScores($lhs, $rhs) {
  foreach ($rhs as $key => $value)
    if ($value)
      $lhs[] = $value;
  return $lhs;  
}

$scorerResults = array_reduce($scorerResults, "gatherScores", array());

我不确定第三个和第四个数组中的空白值是什么以及应该如何处理它们,因此您可能需要更改if ($value)条件以检查不同的内容。就目前而言,它显然也会过滤掉零分数。

于 2013-06-21T22:37:47.337 回答