Solved

PHP two diminutional array iteration

Posted on 2010-08-27
5
285 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:ucsdmbdm
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:RQuadling
ID: 33546366
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33549615
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

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33549647
Here is how the professionals might simplify the data structure.  An array of objects might make sense, too, depending on what else you want to keep associated with the page names and URLs.

Generally speaking, if you find yourself confused by a multi-dimensional array, you probably want to change the code to use a simpler, more obvious organization.

HTH, ~Ray
<?php // RAY_temp_ucs.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

// ASSIGN SOME VALUES TO THESE PAGE NAMES AND URLS
$nav1['Timesheet']      = 'timesheets.php';
$nav1['Expense Report'] = 'expenses.php';
$nav1['Invoice']        = 'invoices.php';
$nav1['Project Report'] = 'project_reports.php';

// I only want to iterate through this set
$nav3['Application Management'] = 'application_management.php';
$nav3['Job Management']         = 'job_manager.php';
$nav3['Resource Allocation']    = 'resources_m.php';
$nav3['Referrals']              = 'reference_m.php';

// SHOW THE ARRAYS WE JUST BUILT
echo PHP_EOL . 'NAV 1' . PHP_EOL;
var_dump($nav1);
echo PHP_EOL . 'NAV 3' . PHP_EOL;
var_dump($nav3);

// SHOW HOW TO USE THE ITERATOR
foreach ($nav3 as $page_name => $page_link)
{
    echo PHP_EOL . "PAGE NAME: $page_name POINTS TO URL: $page_link";
}

Open in new window

0
 

Author Closing Comment

by:ucsdmbdm
ID: 33631464
I am still not sure how to loop through an array within an array and skip the outer array
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 33635131
You can't. Each sub array is separate to every other one.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
The viewer will learn how to dynamically set the form action using jQuery.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now