从数组

时间:2017-01-30 09:50:27

标签: php arrays multidimensional-array foreach foreign-keys

我的数组是

FORM结果会拉入此类型的$ _POST数组。

位置[0] =>数组,向我显示确切的键 - [0] =>数组(1,4,5) -

您好! 我的数组提取形式是:

    Array
    (
        [chk] => Array
            (
                [0] => 1
                [1] => 4
                [2] => 5
            )

        [ID] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
                [3] => 4
                [4] => 5
            )

        [firstAttr] => Array
            (
                [0] => Sun
                [1] => Love
                [2] => Fruit
                [3] => Dog
                [4] => Sky
            )

        [secondAttr] => Array
            (
                [0] => Big
                [1] => intense
                [2] => Delicious
                [3] => Black
                [4] => Blue
            )

        [otherAttr] => Array
            (
                [0] => White
                [1] => Red
                [2] => Orange
                [3] => Old
                [4] => Nice
            )       
    )

我的结果请求是group [chk]。

我只需要取出属于[chk]组的值。

示例:

[chk] => Array
    (
        [0] => 1
        [1] => 4
        [2] => 5
    )

ID [0] => 1,    firstAttr [0] => Sun,   secondAttr [0] => Big,  otherAttr [0] => White
ID [3] => 4,    firstAttr [3] => Dog,   secondAttr [3] => Black,otherAttr [3] => Old
ID [4] => 5,    firstAttr [4] => Sky,   secondAttr [4] => Blue, otherAttr [4] => Nice

结果数组。

我从[input type =“复选框”name =“chk []”]中提取值1,4,5

现在我必须通过引用这些键来提取值:

在'示例中它们是:[chk] =>数组(1,4,5)。

    RESULT [chk] group: 
    1 = Sun, Big, White
    4 = Dog, Black, Old
    5 = Sky, Blue, Nice

$selectAll = $_POST;
$chk = $_POST['chk'];
$chkcount = count($chk);

$result = array();
foreach ($chk as $index) {
    $result[$index]['ID']   = $selectAll['ID'][$index-1];
    $result[$index]['firstAttr']    = $selectAll['firstAttr'][$index-1];
    $result[$index]['secondAttr']   = $selectAll['secondAttr'][$index-1];
    $result[$index]['otherAttr']    = $selectAll['otherAttr'][$index-1];
}

print_r($result);

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

<?php

$res =[
    'chk' => [ 1,4,5 ],

    'ID' =>[1,2,3,4,5 ],

    'firstAttr' => [

            'Sun',
            'Love',
            'Fruit',
            'Dog',
            'Sky'
       ],

    'secondAttr' => [

            'Big',
            'intense',
            'Delicious',
            'Black',
            'Blue'
       ],

    'otherAttr' => [
            'White',
            'Red',
            'Orange',
            'Old',
            'Nice'
       ]
 ];

$result = [];
foreach($res['chk'] as $value){
    $key = $value -1;
    $result[$value] = $value.' = '.$res['firstAttr'][$key].','.$res['secondAttr'][$key].','.$res['otherAttr'][$key];
}

print_r($result);