解析一个巨大的多维数组并从中构造一个新数组

时间:2012-10-10 01:36:04

标签: php multidimensional-array traversal

我需要一些帮助来启动一个将通过数组解析的函数,检查某些值,如果存在这些值,则创建一个包含这些值的新数组,并将它们全部放在一起。

我正在通过这样的数组:

Array
(
    [0] => Array
        (
            [id] => 86
            [34] => 695
            [39] => 0
            [40.1] => Yes
            [36.1] => Yes 
            [35.4] => Card
            [33.3] => Dekalb
            [33.4] => Illinois
            [33.5] => 60115
            [33.6] => United States
            [35.1] => 1143
            [33.1] => 5555 Write Rd
            [33.2] => Write School
            [32.6] => John
            [32.3] => Smith
            [28] => jsmith@gmail.com
            [27] => 5555556554
            [25] => NIUSN
            [14.3] => Jane
            [14.6] => Doe
            [11.2] => 695
            [12] => 1
            [11.1] => In-Person
            [3] => 0
            [2.2] => 595
            [2.1] => Online
        )
    [1] => Array
        (
        ...same stuff as before
        )

我创建了一个函数parseArray,我传递了上面的数组。我查看了每个键/值组合,如果存在某个键,我将其设置为变量。然后,如果存在所有正确的变量,我将其与数组相关联,然后将其推送到最终数组(其中将保留所有值):

function parseArray($arry) {

    $results = array();
    $current_result = array();

    foreach($arry as $a) {
        foreach($a as $k => $v) {       
            if ( $k == '14.3' ) {
                $attendee_first_name = $v;
            }
            if ( $k == '14.6' ) {
                $attendee_last_name = $v;
            }
            if ( $attendee_first_name && $attendee_last_name ) {
                $full_name = $attendee_first_name . ' ' . $attendee_last_name;
            }
        }
        $current_result['attendee_name'] = $full_name;
    }

    array_push($results, $current_result);

    return $results;
}

现在他们一直在这样做,一直非常程序化,非常笨重。我希望能够深入了解如何为遍历数组和分配值生成更清晰/更漂亮的代码。

理想情况下,我会喜欢这样的结果:

Array
(
    [0] => Array
        (
            [attendees_name] = John Smith
            [attendees_email] = jsmith@gmail.com
            [purchasing_name] = Jane Doe
            ...etc

所以我可以简单地通过foreach传递输出并轻松输出所需的信息。

1 个答案:

答案 0 :(得分:1)

代码:

# initialize your test array

$arry = array(
    0 => array(
            'id' => 86,
            '34' => 695,
            '39' => 0,
            '40.1' => 'Yes',
            '36.1' => 'Yes', 
            '35.4' => 'Card',
            '33.3' => 'Dekalb',
            '33.4' => 'Illinois',
            '33.5' => '60115',
            '33.6' => 'United States',
            '35.1' => '1143',
            '33.1' => '5555 Write Rd',
            '33.2' => 'Write School',
            '32.6' => 'John',
            '32.3' => 'Smith',
            '28' => 'jsmith@gmail.com',
            '27' => '5555556554',
            '25' => 'NIUSN',
            '14.3' => 'Jane',
            '14.6' => 'Doe',
            '11.2' => '695',
            '12' => '1',
            '11.1' => 'In-Person',
            '3' => 0,
            '2.2' => 595,
            '2.1' => 'Online'
        ),
1 => array(
        'id' => 86,
        '34' => 695,
        '39' => 0,
        '40.1' => 'Yes',
        '36.1' => 'Yes', 
        '35.4' => 'Card',
        '33.3' => 'Dekalb',
        '33.4' => 'Illinois',
        '33.5' => '60115',
        '33.6' => 'United States',
        '35.1' => '1143',
        '33.1' => '5555 Write Rd',
        '33.2' => 'Write School',
        '32.6' => 'Douglas',
        '32.3' => 'Adams',
        '28' => 'douglas.adams@gmail.com',
        '27' => '5555556554',
        '25' => 'NIUSN',
        '14.3' => 'Frank',
        '14.6' => 'Wright',
        '11.2' => '695',
        '12' => '1',
        '11.1' => 'In-Person',
        '3' => 0,
        '2.2' => 595,
        '2.1' => 'Online'
    )

);

# How to do your function elegantly

foreach($arry as $a) {
    $results[] = array(
        'attendees_name' => $a['32.6'] . " " . $a['32.3'],
        'attendees_email' => $a['28'],
        'purchasing_name' => $a['14.3'] . " " . $a['14.6'] 
    );
}

# print results

var_dump($results);

结果:

array(2) {
  [0]=>
  array(3) {
    ["attendees_name"]=>
    string(10) "John Smith"
    ["attendees_email"]=>
    string(16) "jsmith@gmail.com"
    ["purchasing_name"]=>
    string(8) "Jane Doe"
  }
  [1]=>
  array(3) {
    ["attendees_name"]=>
    string(13) "Douglas Adams"
    ["attendees_email"]=>
    string(23) "douglas.adams@gmail.com"
    ["purchasing_name"]=>
    string(12) "Frank Wright"
  }
}