Search code examples
phpmultidimensional-arraytraversal

Parsing through a giant mulltidimensional array and construct a new array from it


I need some help starting a function that will parse through an array, check for certain values, if those values exist, create a new array with those values and array_push them all together.

I'm passing through an array such as this:

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] => [email protected]
            [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
        )

I made a function parseArray to which I passed the above array. I go through every key/value combo and if a certain key exists, I set that to a variable. Then if all the right variables exist, I associate that to an array then push it to a final array (where all values will be held):

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;
}

Now they way I have been doing it, has been very procedural and very clunky. I would love to get some insight on how to produce much cleaner/beautiful code for traversing an array and assigning value.

Ideally I would love something like this to result:

Array
(
    [0] => Array
        (
            [attendees_name] = John Smith
            [attendees_email] = [email protected]
            [purchasing_name] = Jane Doe
            ...etc

So I can simply pass the output through a foreach and output the desired information easily.


Solution

  • Code:

    # 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' => '[email protected]',
                '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' => '[email protected]',
            '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);
    

    Results:

    array(2) {
      [0]=>
      array(3) {
        ["attendees_name"]=>
        string(10) "John Smith"
        ["attendees_email"]=>
        string(16) "[email protected]"
        ["purchasing_name"]=>
        string(8) "Jane Doe"
      }
      [1]=>
      array(3) {
        ["attendees_name"]=>
        string(13) "Douglas Adams"
        ["attendees_email"]=>
        string(23) "[email protected]"
        ["purchasing_name"]=>
        string(12) "Frank Wright"
      }
    }