InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Specifying an Array Not Beginning at Element 0 in PHP
PHP

Specifying an Array Not Beginning at Element 0 in PHP

InfinityCoder November 19, 2016

You want to assign multiple elements to an array in one step, but you don’t want the first index to be 0.

Instruct array() to use a different index using the => syntax:

1
$presidents = array(1 => 'Washington', 'Adams', 'Jefferson', 'Madison');

Arrays in PHP—like most, but not all, computer languages—begin with the first entry located at index 0. Sometimes, however, the data you’re storing makes more sense if the list begins at 1.

(And we’re not just talking to recovering Pascal programmers here.)
In the Solution, George Washington is the first president, not the zeroth, so if you wish to print a list of the presidents, it’s simpler to do this:

1
2
3
foreach ($presidents as $number => $president) {
    print "$number: $president\n";
}

than this:

1
2
3
4
foreach ($presidents as $number => $president) {
    $number++;
    print "$number: $president\n";
}

The feature isn’t restricted to the number 1; any integer works:

1
2
3
$reconstruction_presidents = array(16 => 'Lincoln', 'Johnson', 'Grant');
// alternatively,
$reconstruction_presidents = [16 => 'Lincoln', 'Johnson', 'Grant'];

Also, you can use => multiple times in one call:1

1
2
3
whig_presidents = array(9 => 'Harrison', 'Tyler', 12 => 'Taylor', 'Fillmore');
// alternatively,
$whig_presidents = [9 => 'Harrison', 'Tyler', 12 => 'Taylor', 'Fillmore'];

PHP even allows you to use negative numbers in the array() call. (In fact, this method works for noninteger keys, too.)

What you’ll get is technically an associative array, although as we said, the line between numeric arrays and associative arrays is often blurred in PHP; this is just another one of these cases:

1
2
3
$us_leaders = array(-1 => 'George II', 'George III', 'Washington');
// alternatively,
$us_leaders = [-1 => 'George II', 'George III', 'Washington'];

If Washington is the first US leader, George III is the zeroth, and his grandfather George II is the negative-first.
Of course, you can mix and match numeric and string keys in one array() definition, but it’s confusing and very rarely needed:

1
2
3
4
$presidents = array(1 => 'Washington', 'Adams', 'Honest' => 'Lincoln',
                    'Jefferson');
// alternatively,
$presidents = [1 => 'Washington', 'Adams', 'Honest' => 'Lincoln', 'Jefferson'];

This is equivalent to:

1
2
3
4
$presidents[1] =          'Washington';    // Key is 1
$presidents[] =           'Adams';         // Key is 1 + 1 => 2
$presidents['Honest'] =   'Lincoln';       // Key is 'Honest'
$presidents[] =           'Jefferson';     // Key is 2 + 1 => 3

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Preventing Changes to Classes and Methods in PHP
You want to prevent another developer from redefining specific methods …

Preventing Changes to Classes and Methods in PHP

Printing Correct Plurals in PHP
You want to correctly pluralize words based on the value …

Printing Correct Plurals in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap