在几次尝试之后,我找不到一个有效的方法来做到这一点。我目前有一个运行preg_match_all的函数,并返回三个这样的数组;
array(3) {
["name"] =>
array(3) {
0 => "Google Chrome 22.0.1229.94",
1 => "LastPass for Chrome 2.0.7",
2 => "Chromatic 0.2.3"
}
["link"] =>
array(3) {
0 => "/app/mac/32956/google-chrome",
1 => "/app/mac/42578/lastpass-for-chrome",
2 => "/app/mac/32856/chromatic"
}
["description"] =>
array(3) {
0 => " - Modern and fast Web browser."
1 => " - Online password manager and form filler for Chrome."
2 => " - Easily install and updated Chromium."
}
}我需要能够像这样组合三个数组;
array(3) {
array(3) {
["name"] = "Google Chrome 22.0.1229.94",
["link"] = "/app/mac/32956/google-chrome",
["description"] = " - Modern and fast Web browser."
}
array(3) {
["name"] = "LastPass for Chrome 2.0.7",
["link"] = "/app/mac/42578/lastpass-for-chrome",
["description"] = " - Online password manager and form filler for Chrome."
}
array(3) {
["name"] = "Chromatic 0.2.3",
["link"] = "/app/mac/32856/chromatic",
["description"] = " - Easily install and updated Chromium."
}
} 我一直在尝试count( $values )并执行一个for循环来生成新的数组。
发布于 2012-11-05 18:05:04
这是我的观点,对于你的特殊情况,假设你的原始数组是$results:
for ($i=0; $i<3;$i++) {
$combined[$i]['name'] = $results['name'][$i];
$combined[$i]['link'] = $results['link'][$i];
$combined[$i]['description'] = $results['description'][$i];
}发布于 2012-11-05 16:59:31
接下来,我将建议您真正寻找的是preg_match_all的PREG_SET_ORDER标志
preg_match_all('/.../', $foo, $bar, PREG_SET_ORDER);http://php.net/preg_match_all
否则:
$results = array();
foreach ($matches as $key => $values) {
foreach ($values as $index => $value) {
$results[$index][$key] = $value;
}
}https://stackoverflow.com/questions/13228759
复制相似问题