1

我有以下名为 $responses 的多维数组,当我执行 print_r 时它看起来像这样

大批

我的 foreach 循环看起来像这样,但它不起作用:

foreach ($responses as $response) { 
    $output[$response['poll_response_id']] = array(
        'response' => $response['response'],
        'response_vote_count' => 0,
        'voters' => ''
    );
}

当我执行 print_r($output) 时,我想得到这样的输出

输出数组

4

2 回答 2

3

对我来说似乎是一个简单的转变:

$out = array_map(function($response) {
  return array(
    'response' => current($response),
    'response_vote_count' => 0,
    'voters' => '',
  );
}, $responses);
于 2013-05-06T14:22:56.453 回答
2

You can try this:

$output = array(); 
foreach ($responses as $key => $response) { 
     $output[$key] = array(
        'response' => $response[$key],
        'response_vote_count' => 0,
        'voters' => ''
     ); 
}

If it doesn't work, please do a "var_dump" on "$responses" instead of "print_r" because we didn't view how this variable is exactly defined.

于 2013-05-06T14:14:16.127 回答