Link to home
Start Free TrialLog in
Avatar of ucsdmbdm
ucsdmbdm

asked on

PHP two diminutional array iteration

Hi,

Whats the most efficient way of iterating through ONLY a subarray of multi-dimentional array ?
Like in the example below, how can I only iterate through the set with $topnavnames[3] without having to go through the first dimension

                  
            $bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Timesheet';
            $bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'timesheets.php';
            $bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Expense Report';
            $bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'expenses.php';
            $bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Invoice';
            $bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'invoices.php';
            $bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Project Report';
            $bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'project_reports.php';
          // I only want to iterate through this set       
          $bottomnavnames [$all_roles[2]][$topnavnames[3]] = 'Application Management';
            $bottomnavtarget[$all_roles[2]][$topnavnames[3]] = 'application_management.php';
            $bottomnavnames [$all_roles[2]][$topnavnames[3]] = 'Job Management';
            $bottomnavtarget[$all_roles[2]][$topnavnames[3]] = 'job_manager.php';
            $bottomnavnames [$all_roles[2]][$topnavnames[3]] = 'Resource Allocation';
            $bottomnavtarget[$all_roles[2]][$topnavnames[3]] = 'resources_m.php';
            $bottomnavnames [$all_roles[2]][$topnavnames[3]] = 'Referrals';
            $bottomnavtarget[$all_roles[2]][$topnavnames[3]] = 'reference_m.php';
                            

Thanks
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

You are overwriting the values ...


$bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Timesheet';
$bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'timesheets.php';

$bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Expense Report';
$bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'expenses.php';

$bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Invoice';
$bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'invoices.php';

$bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Project Report';
$bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'project_reports.php';

This code will end up with only


$bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Project Report';
$bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'project_reports.php';

being the assigned values.


Can you give the $all_roles and $topnavnames values please so I can restructure your data.
This does not look like any kind of "normal" data structure -- at least not one they teach in computer science class.  You can install the code snippet and see why.  It is complicated and obviously confusing, since it destroys itself (overwriting fields) as it is defined.

I'll try to show you an alternative in a separate post.
<?php // RAY_temp_ucs.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

// MUST ASSIGN SOME VALUES TO THESE
$all_roles[2]   = 'ALL_ROLES_2';
$topnavnames[1] = 'TOP_NAV_NAMES_1';
$topnavnames[3] = 'TOP_NAV_NAMES_3';

$bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Timesheet';
$bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'timesheets.php';
$bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Expense Report';
$bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'expenses.php';
$bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Invoice';
$bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'invoices.php';
$bottomnavnames [$all_roles[2]][$topnavnames[1]] = 'Project Report';
$bottomnavtarget[$all_roles[2]][$topnavnames[1]] = 'project_reports.php';

// I only want to iterate through this set
$bottomnavnames [$all_roles[2]][$topnavnames[3]] = 'Application Management';
$bottomnavtarget[$all_roles[2]][$topnavnames[3]] = 'application_management.php';
$bottomnavnames [$all_roles[2]][$topnavnames[3]] = 'Job Management';
$bottomnavtarget[$all_roles[2]][$topnavnames[3]] = 'job_manager.php';
$bottomnavnames [$all_roles[2]][$topnavnames[3]] = 'Resource Allocation';
$bottomnavtarget[$all_roles[2]][$topnavnames[3]] = 'resources_m.php';
$bottomnavnames [$all_roles[2]][$topnavnames[3]] = 'Referrals';
$bottomnavtarget[$all_roles[2]][$topnavnames[3]] = 'reference_m.php';

// SHOW THE ARRAYS WE JUST BUILT
echo PHP_EOL . 'BOTTOM NAV NAMES' . PHP_EOL;
var_dump($bottomnavnames);
echo PHP_EOL . 'BOTTOM NAV TARGET' . PHP_EOL;
var_dump($bottomnavtarget);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ucsdmbdm
ucsdmbdm

ASKER

I am still not sure how to loop through an array within an array and skip the outer array
You can't. Each sub array is separate to every other one.