1

经过几次尝试,我无法找到一种有效的方法来做到这一点。我目前有一个函数正在运行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 循环来制作新数组。

4

2 回答 2

2

对于您的特定情况,这是我的看法,因为您的原始数组是$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-05T10:05:04.153 回答
1

我会继续建议您真正要寻找的是以下PREG_SET_ORDER标志preg_match_all

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;
    }
}
于 2012-11-05T08:59:30.797 回答