改变二维数组的索引

时间:2012-04-02 14:52:29

标签: php arrays multidimensional-array

我有一个带有以下输出的二维数组

Array
(
[0] => Array
    (
        [customer_id] => 7
        [language_id] => 1
        [variableitem_id] => 13
        [name] => QUESTION_HEADLINE
        [value] => Bitte geben Sie Ihren Downloadkey ein:
    )

[1] => Array
    (
        [customer_id] => 7
        [language_id] => 1
        [variableitem_id] => 15
        [name] => QUESTION_BUTTON
        [value] => Start!
    )
)

我想将数组的输出转换为类似

的输出
Array
(
 [QUESTION_HEADLINE] => Array
 (
    [value] => Bitte geben Sie Ihren Downloadkey ein:
 )

 [QUESTION_BUTTON] => Array
 (
    [value] => Start!
 )
)

我正在使用这段代码为什么它没有给我正确的数组索引作为名称

    $final = array();
            foreach($data['variables_data'] as $vals){
                $final[$vals['name']]['value'] = $vals['value'];
                echo '<pre>';
                print_r($final[$vals['name']]['value']);
                echo '</pre>';
            } 

3 个答案:

答案 0 :(得分:0)

foreach ($originalArray as $orgSubArray) {
    $newArray[$orgSubArray['name']]['value'] = $orgSubArray['value'];
}

答案 1 :(得分:0)

$final = array()    
Foreach ($arr as $vals) {
   $final[$vals['name']] = $vals['value'];
}

这与CI无关。这真的很简单..你来这儿之前尝试了什么?

答案 2 :(得分:0)

$final = array();    
foreach ($arr as $vals) {
   $final[$vals['name']] = array('value' => $vals['value']);
}
echo '<pre>';
print_r($final);
echo '</pre>';

这应该可以胜任。输出=&gt;

> Array (
>     [QUESTION_HEADLINE] => Array
>         (
>             [value] => Bitte geben Sie Ihren Downloadkey ein:
>         )
> 
>     [QUESTION_BUTTON] => Array
>         (
>             [value] => Start!
>         )
> 
> )