0

Hello guys I need a little help here about arrays. I have 3 arrays, one is for base the other is for header and the last is the header fields. In my PHP code I have this I am using CodeIgniter.

public function field_study() {

            $fields_header = array('Accounting/Finance','Admin/Human Resources','Arts/Media/Communications','Building/Construction','Computer/Information Technology','Education/Training','Engineering','Healthcare','Hotel/Restaurant','Manufacturing','Sales/Marketing','Sciences', 'Services', 'Others');

            $fields = array(

                array(
                    'Audit & Taxation',
                    'Bangking/Financial',
                    'Corporate Finance/Investment',
                    'General/Cost Accounting'
                ),

                array(
                    'Clerical/Administrative',
                    'Human Resources',
                    'Secretarial',
                    'Top Management',
                ),

                array(
                    'Advertising',
                    'Arts/Creative Design',
                    'Entertainment',
                    'Public Relations'
                ),

                array(
                    'Architect/Interior Design',
                    'Civil Engineering/Construction',
                    'Property/Real Estate',
                    'Quantity Serveying'
                ),

                array(
                    'IT - Hardware',
                    'IT - Network/Sys/DB Administrator',
                    'IT - Software',
                ),

                array(
                    'Education',
                    'Training $ Dev.'
                ),

                array(
                    'Chemical Engineering',
                    'Electrical Engineering',
                    'Electronics Engineering',
                    'Environmental Engineering',
                    'Industrial Engineering',
                    'Mechanical/Automotive Engineering',
                    'Oil/Gas Engineering',
                    'Other Engineering'
                ),

                array(
                    'Doctor/Diagnosis',
                    'Pharmacy',
                    'Nurse/Medical Support',
                ),

                array(
                    'Food/Beverage/Restaurant',
                    'Hotel/Tourism'
                ),

                array(
                    'Maintenance',
                    'Manufacturing',
                    'Process Design & Control',
                    'Purchasing/Material Mgmt',
                    'Quality Assurance'
                ),

                array(
                    'Sales - Corporate',
                    'Marketing/Business Dev',
                    'Merchandising',
                    'Retail Sales',
                    'Sales - Eng/Tech/IT',
                    'Sales - Financial Services',
                    'Telesales/Telemarketing' 
                ),

                array(
                    'Actuarial/Statistics',
                    'Agriculture',
                    'Aviation',
                    'Biotechnology',
                    'Chemistry',
                    'Food Tech/Nutritionist',
                    'Geology/Geographics',
                    'Science & Technology'
                ),

                 array(
                    'Security/Armed Forces',
                    'Customer Service',
                    'Logistics/Supply Chain',
                    'Law/Legal Services',
                    'Personal Care',
                    'Social Services',
                    'Tech & Helpdesk Support'
                ),

                array(
                    'General Work',
                    'Journalist/Editors',
                    'Publishing',
                    'Others'
                )

            );

            $main = array(
                    $fields_header, $fields
            );

            return $main;   

        }

I need to include this in my form. So I have something like this after I hardcoded it.

<select name="academic[1][field_study]" id="field_study" class="form-control">
                        <option value="" selected="selected">- Select Field Of Study -</option>
                        <optgroup label="Accounting/Finance">
                            <option value="Audit & Taxation">Audit & Taxation</option>
                            <option value="Banking/Financial">Banking/Financial</option>
                            <option value="Corporate Finance/Investment">Corporate Finance/Investment</option>
                            <option value="General/Cost Accounting">General/Cost Accounting</option>
                        </optgroup>             
                        <optgroup label="Admin/Human Resources">
                            <option value="Clerical/Administrative">Clerical/Administrative</option>
                            <option value="Human Resources">Human Resources</option>
                            <option value="Secretarial">Secretarial</option>
                            <option value="Top Management">Top Management</option>
                        </optgroup> 

But what I want is include this array and use it in a loop but I don't know how. Can you help me with this? Ok that's all thanks.

2
  • 1
    wouldn't it be a lot easier and cleaner to use an associative array: 'Accounting/Finance' => array('Audit & Taxation', etc...) and do foreach($fields AS $key => $val) to get the opt group, then foreach($val AS $option) to loop through the options for each key? Commented Mar 12, 2014 at 1:50
  • I added a short example to illustrate... Commented Mar 12, 2014 at 1:55

1 Answer 1

2

Using an associative array would be easiest here:

$fields = array(
    'Accounting/Finance' => array(
                    'Audit & Taxation',
                    'Bangking/Financial',
                    'Corporate Finance/Investment',
                    'General/Cost Accounting'
                ),
    'Admin/Human Resources' => array(
                    'Clerical/Administrative',
                    'Human Resources',
                    'Secretarial',
                    'Top Management',
                )
);

then in the html:

<?php foreach($fields AS $key => $val): ?>
<optgroup label="<?php echo $key; ?>">
    <?php foreach($val AS $option): ?>
    <option value="<?php echo $option; ?>"><?php echo $option; ?></option>
    <?php endforeach; ?>
</optgroup>
<?php endforeach; ?> 
Sign up to request clarification or add additional context in comments.

1 Comment

Ok sir thanks for the help I actually converted my array to associative. I will try your answer, :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.