Solved

PHP two diminutional array iteration

Posted on 2010-08-27
5
295 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
[X]
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
  • 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 110

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 110

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

624 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