【问题标题】:extract value for relative key from Array从数组中提取相对键的值
【发布时间】:2017-01-30 09:50:26
【问题描述】:

我的数组是组成的

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
            )       
    )

我的结果请求是组 [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 = "checkbox" name = "chk []" ] 中提取值 1,4,5

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

在'示例中它们是:[chk] => Array (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);

【问题讨论】:

  • $chk = $_POST[0]['chk'];试试这个
  • 预期结果是什么。明确说明

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


【解决方案1】:

试试下面的代码:

<?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);

【讨论】:

    猜你喜欢
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多