Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8670997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:50:24+00:00 2026-06-12T18:50:24+00:00

I need some help starting a function that will parse through an array, check

  • 0

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

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] = jsmith@gmail.com
            [purchasing_name] = Jane Doe
            ...etc

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:50:25+00:00Added an answer on June 12, 2026 at 6:50 pm

    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' => '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);
    

    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"
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help with Activerecord Querying in a has_many :through association. Model: Job class
Need some help assigning a mouseover event to display some icons that start out
Need some help with DataFormatString in GridView. I have a Double value that needs
I'm just starting out with WPF and need some help with routed events. I
I'm looking for a function or code snippet or some starting help in creating
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.