Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

PHP Tutorials – Arrays (Part 2)

By Exforsys | on November 15, 2006 |
PHP Tutorial

PHP Tutorials – Arrays (Part 2)

In this PHP Tutorial you will learn Arrays (Part 2) – Looping with foreach( ), Sorting arrays and Using Multidimensional Arrays.

Looping with foreach( ):

The easiest way to iterate through each element of an array is with foreach( ).
The foreach( ) construct lets you run a code block once for each element in an array.

Example:

$fruits = array(‘apple’, ‘banana’, ‘orange’);
print “<table>\n”;
foreach ($fruits as $fruit) {
print “<tr><td>$fruit</td></tr>\n”;
}
print ‘</table>’;

This will print:

<table>
<tr><td>apple</td></tr>
<tr><td>banana</td></tr>
<tr><td>orange</td></tr>
</table>

if you want to know what element you’re on as you’re iterating through a numeric array, use for( ) instead of foreach( ).

Your for( ) loop should depend on a loop variable that starts at 0 and continues up to one less than the number of elements in the array.

Example:

$fruits = array(‘apple’, ‘banana’, ‘orange’);
$num = count($fruits);
for ($i = 0, $i < $num; $i++) {
print "Fruit number $i is $fruits[$i]\n";
}

This will print:

Fruit number 0 is apple
Fruit number 0 is banana
Fruit number 0 is orange

Sorting arrays:

To sort array values, you can use the function sort().

Example:

$dinner = array(‘Sweet Corn and Asparagus’,
‘Lemon Chicken’,
‘Braised Bamboo Fungus’);
$meal = array(‘breakfast’ => ‘Walnut Bun’,
‘lunch’ => ‘Cashew Nuts and White Mushrooms’,
‘snack’ => ‘Dried Mulberries’,
‘dinner’ => ‘Eggplant with Chili Sauce’);
sort($dinner);
sort($meal);
foreach ($dinner as $key => $value) {
print "\$dinner: $key $value\n";
}
foreach ($meal as $key => $value) {
print " \$meal: $key $value\n";
}

This will print:

$dinner: 0 Braised Bamboo Fungus
$dinner: 1 Lemon Chicken
$dinner: 2 Sweet Corn and Asparagus
$meal: 0 Cashew Nuts and White Mushrooms
$meal: 1 Dried Mulberries
$meal: 2 Eggplant with Chili Sauce
$meal: 3 Walnut Bun

can also sort arrays by key with ksort( ). This keeps key/value pairs together, but orders them by key.

Example:

$meal = array(‘breakfast’ => ‘Walnut Bun’,
‘lunch’ => ‘Cashew Nuts and White Mushrooms’,
‘snack’ => ‘Dried Mulberries’,
‘dinner’ => ‘Eggplant with Chili Sauce’);
ksort($meal);
foreach ($meal as $key => $value) {
print "\$meal: $key $value\n";
}

This will print:

$meal: breakfast Walnut Bun
$meal: dinner Eggplant with Chili Sauce
$meal: lunch Cashew Nuts and White Mushrooms
$meal: snack Dried Mulberries

Using Multidimensional Arrays:

Use the array( ) construct to create arrays that have more arrays as element values.

Example:

$lunches = array( array(‘Chicken’,’Eggplant’,’Rice’),
array(‘Beef’,’Scallions’,’Noodles’),
array(‘Eggplant’,’Tofu’));
print $lunches[0][0];
print $lunches[2][1];

This will print:

Chicken
Tofu

« « How To Reach The Top Of Your Career
PHP Tutorials – File manipulation (Part 1) » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 403 Followers
  • Popular
  • Recent
  • PHp Tutorials : Regular expressions

    November 2, 2006 - 0 Comment
  • PHP Tutorials – Functions (Part I)

    September 29, 2006 - 0 Comment
  • PHP Tutorials : Arrays

    November 5, 2006 - 0 Comment
  • PHP Tutorials – Functions (Part-2)

    October 7, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part 1)

    November 15, 2006 - 0 Comment
  • PHP Tutorials – Strings (Part I)

    October 13, 2006 - 0 Comment
  • WAMP Server

    October 26, 2008 - 0 Comment
  • PHP Tutorials – File manipulation (Part-2)

    November 22, 2006 - 0 Comment
  • PHP Tutorials : Strings (Part 2)

    October 20, 2006 - 0 Comment
  • PHP and MySQL User Registration

    October 31, 2008 - 0 Comment
  • PHP Strings

    March 18, 2009 - 0 Comment
  • How to Use Cookies in PHP

    February 18, 2009 - 0 Comment
  • PHP and MySQL User Registration

    October 31, 2008 - 0 Comment
  • WAMP Server

    October 26, 2008 - 0 Comment
  • PHP Tutorial : PHP & MySQL

    January 16, 2007 - 0 Comment
  • PHP Tutorials – Forms

    November 22, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part-2)

    November 22, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part 1)

    November 15, 2006 - 0 Comment
  • PHP Tutorials : Arrays

    November 5, 2006 - 0 Comment
  • PHp Tutorials : Regular expressions

    November 2, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • PHP Strings
  • How to Use Cookies in PHP
  • PHP and MySQL User Registration
  • WAMP Server
  • PHP Tutorial : PHP & MySQL

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2022. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Reject Read More
Privacy & Cookies Policy
Necessary Always Enabled