1

我需要一些帮助来启动一个函数,该函数将解析一个数组,检查某些值,如果这些值存在,则使用这些值创建一个新数组并将它们一起 array_push。

我正在通过一个这样的数组:

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 传递输出并轻松输出所需的信息。

4

1 回答 1

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"
  }
}
于 2012-10-10T02:07:54.847 回答