按索引位置组合三个数组

时间:2012-11-05 08:56:28

标签: php arrays

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

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:1)

我会继续并建议你真正寻找的是PREG_SET_ORDER的{​​{1}}标志:

preg_match_all

http://php.net/preg_match_all

否则:

preg_match_all('/.../', $foo, $bar, PREG_SET_ORDER);