Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP two diminutional array iteration

Posted on 2010-08-27
5
292 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:Richard Quadling
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 109

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 109

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:Richard Quadling
ID: 33635131
You can't. Each sub array is separate to every other one.

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

856 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