Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP two diminutional array iteration

Posted on 2010-08-27
5
Medium Priority
?
298 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 1500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

670 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