Jared Heinrichs

  • Technology Blog
  • Winnipeg Computer Repair
  • Winnipeg Photographer
  • Cooking With Jared
You are here: Home / Programming / PHP / PHP – All about arrays (Part 1 of 3)

Dec 18, 2009 By Jared Heinrichs 1 Comment

PHP – All about arrays (Part 1 of 3)

This post will help you understand what an array is and how it is used.

In this three part series I will will go over:

  • What is an array
  • Creating arrays
  • Testing for an array
  • Adding and removing array elements
  • Locating array elements
  • Traversing Arrays
  • Determining array Size and element uniqueness
  • Sorting arrays
  • Merging, slicing, splicing and dissecting arrays

What is an array?

A traditional array is defined as a group of items that share certain characteristics such as similarity and type.

  1. Examples of Similarity might be something like “car models”, “baseball teams” or “types of fruit”.
  2. Examples of types might be something like “string” or “integers”
  3. *NOTE* The items DON’T have to groups of similar items.

 

There are two main parts of an array. The array is made up of a key and a value. While “Value” is pretty obvious the key is something a lot of beginning programmer have an issue with. Keys can be one of two types. Numerical or Associative. Associative keys ten to bear some relation to the value other then its array position.

An example of a numeric array being defined:

<?php
    $provinces[0] = "BC";
    $provinces[1] = "Alberta";
    $provinces[2] = "Saskatchewan";
    $provinces[3] = "Ontario";
?>

 

The key’s in this example are “0”, “1”, “2”, “3”.

An example of an Associative array being defined:

<?php
    $provinces[BC] = "Vancouver";
    $provinces[AB] = "Calgary";
    $provinces[SK] = "Regina";
    $provinces[MB] = "Winnipeg";
    $provinces[ON] = "Toronto";
?>

 

Notice now how I used strings instead of numbers for the key identifier? The key’s also make more sense then using 0,1,2 etc.

Outputting Arrays

The easiest way of quickly outputting an array in php is by using the print_r() function. The output isn’t very nice but it allows you to see what’s all in the array. To use the print_r() function you would type:

print_r($provinces);

 

Here’s an example of the output of associative array above using print_r() function.

Array ( [BC] => Vancouver [AB] => Calgary [SK] => Regina [MB] => Winnipeg [ON] => Toronto )

 

sd

Filed Under: PHP

Trackbacks

  1. PHP – All about arrays (Part 1 of 3) | Jared Heinrichs | Coder Online says:
    Dec 19, 2009 at 12:35 am

    […] Link: PHP – All about arrays (Part 1 of 3) | Jared Heinrichs […]

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Board Game Rules
  • Camera
  • Computer Hardware
    • Blackberry
    • drivers
    • iPad
    • Magic Jack
    • USB
  • Damn Small Linux
  • Exam Notes
  • Facebook
  • FREE Flashcards
  • Games
    • PC
      • League of Legends
    • Wii
    • xbox 360
  • Music
  • Networking
    • Cisco Certification
    • Mitel
    • Palo Alto Firewall
  • News
    • Google
    • Microsoft
  • Operating System
    • Active Directory (2003)
    • Android
    • Command Prompt
    • Damn Small Linux
    • Group Policy
    • Hyper-V
    • IIS
    • ISA 2006
    • Mac OS X
    • Microsoft Exchange Server
    • Powershell
    • Security
    • SME Server
    • Terminal Server 2003
    • Ubuntu Linux
      • Adito Web SSL VPN
      • OpenVpn-als
      • Webmin
    • Virtual Machine Manager
    • Windows 2003 SBS
    • Windows 2003 Server
    • Windows 2008
    • Windows 2008 R2
    • Windows 2012R2
    • Windows 7
    • Windows 8
    • Windows Command Line
    • Windows Deployment Services
    • Windows Server Backup
    • Windows Vista
    • Windows XP
  • Phones
  • Photography
  • Photos
    • Animals
    • Misc
    • Nature
    • Portraits
  • Portfolio
  • Programming
    • CSS
    • HTML
    • jQuery
    • MySQL
    • PHP
    • Script
  • Programs
    • Acrobat
    • Acrobat Reader
    • Adobe Dreamweaver
    • Adobe Illustrator
    • Adobe Photoshop
    • Anti-virus Software
    • Antivirus
    • Backup Exec
    • Bittorent
    • Blackberry BESADMIN
    • Internet Explorer 9
    • Lightroom
    • Microsoft Office
    • Netbeans
    • Onenote
    • Outlook
    • Shelby
    • Sysprep
    • Trend
    • Video Editing
    • Visual Studio
    • Windows Live Writer
    • WireShark
    • XP Mode
    • Zarafa
  • Recipe
  • Review
  • Software Links
  • Troubleshooting
  • Uncategorized
  • Videos
  • Web Applications
    • Brage
    • Google
    • Spiceworks
    • Wordpress
  • Web Browsers
    • Internet Explorer
  • Web Server
    • XAMPP
  • Winnipeg
    • ISP

Try searching this site!

Copyright © 2021 Winnipeg Web Design