在另一个数组php foreach中获取数组值

时间:2014-06-04 09:00:24

标签: php arrays

我有一个数组

Array
(
    [0] => Array
        (
            [folding_knives__no_assist__possession19] => G
            [folding_knives__no_assist__possession___length20] => 
            [folding_knives__no_assist___open_cary21] => G
            [folding_knives__no_assist__open_carry_length22] => 
            [folding_knives__no_assist__concealed23] => G
            [folding_knives__no_assisted__concealed_length24] => 
            [folding_knives__no_assist__concealed_w__ccw_required25] => R
            [folding_knives__no_assist__concealed_w__ccw_required_l26] => 
            [folding_knives__no_assist___notes27] => 
        )

    [1] => Array
        (
            [folding_knives__assisted_opening__possession28] => G
            [folding_knives__assisted_opening__possession_length29] => 
            [folding_knives__assisted_opening__open_carry30] => G
            [folding_knives__assisted_opening__open_carry_length31] => 
            [folding_knives__assisted_opening___concealed32] => G
            [folding_knives__assisted_opening__concealed_length33] => 
            [folding_knives__assisted_opening___concealed_w__ccw_re34] => R
            [folding_knives__assisted_opening__concealed_w__ccw_req35] => 
            [folding_knives___assisted_opening___notes36] => 
        )
)

我已经尝试将我的数组数据显示到另一个数组但不能

foreach ($chunks as $key => $val)
{

                $allknife[] = array( 
                        'name'=>$key[0],
                        'possession'=>$val[0],
                        'possession_length'=>$val[1]                    
                    );


}

这里name'=>$key[0]这里将是第一个项目键,如folding_knives__no_assist__possession19

2 个答案:

答案 0 :(得分:0)

它应该链接这个,你想要字段计数链接1/2/3/4 ...然后你必须使用标志逻辑,如,i = 0;和i ++

$allknife[] = array();
foreach ($chunks as $key => $val)
{
   $allknife['name']=>$val['yourvaluesoeshere'],
   $allknife['possession']=>$val['yourvaluesoeshere'],
   $allknife['possession_length']=>$val['yourvaluesoeshere']                    
}

答案 1 :(得分:-1)

由于您的阵列具有不一致的密钥名称,因此您不能依赖此名称,首先需要一致的名称!

更像这样映射数组:

array(
    0 => array(
        'assist' => true,
        'open_carry_nr' => 21,
        'open_carry_length' => 22
        // and so on...
    ),

    2 => array(
        'assist' => false,
        'open_carry_nr' => 18,
        'open_carry_length' => 1337
        // and so on...
    )
);

更好的做法。