Array Types of Java and PHP Part 7

Arrays of Java and PHP

This is the seventh part of the Introduction to Java and PHP series. Grouping data in Java and PHP will be discussed. Whenever possible, code examples will be made in a similar manner for both Java and PHP.

Arrays in Java and PHP are very similar. Arrays in Java are handled exactly the same in PHP.

In this tutorial, Java and PHP will be used to create a program consisting of arrays. The program will be compiled and executed. In order to make the work-flow faster, please use a programming editor.

    Tools are required:

  • Text editor for creating and modifying the Java and PHP files.
  • Java.
  • PHP.

Optional Download and install suitable text editor

Any text editor will work. A bare minimum should be syntax-highlighting for Java and PHP. To select a reviewed lightweight programming editor read the Ojambo.com Lightweight Programming Editors.

Lesson 26: Array Long Syntax

Java PHP
int[] numArray = new int [5]; // Integer array $numArray = array(); // Array
numArray[0] = 1; // Integer array value
numArray[1] = 2; // Integer array value
numArray[2] = 3; // Integer array value
numArray[3] = 4; // Integer array value
numArray[4] = 5; // Integer array value
$numArray[0] = 1; // Integer array value
$numArray[1] = 2; // Integer array value
$numArray[2] = 3; // Integer array value
$numArray[3] = 4; // Integer array value
$numArray[4] = 5; // Integer array value

In Java, the array length is defined at the creation time and cannot be altered. In PHP, the array is defined once a value is entered, and the array’s length can be altered.

Lesson 27: Array Initializers

Java PHP
Long syntax
int[] numArray1 = new int [5] {1, 2, 3, 4, 5}; // Integer array $numArray1 = array(1, 2, 3, 4, 5); // Array
Short syntax
int[] numArray2 = {1, 2, 3, 4, 5}; // Integer array $numArray2 = array(1, 2, 3, 4, 5); // Array

Java uses Curly brackets or Squiggly Brackets “{ }” while PHP uses parenthesis “( )” for assigning values to array indexes.

Lesson 28: Multi-dimensional Arrays

Java PHP
String[][] stringArray = { // String array
{“Dr. “, “Ojambo”},
{“Ms. “, “Ajambo”},
{“Mr. “}
};
$stringArray = array( // String array
“Dr. ” => “Ojambo”,
“Ms. ” => “Ajambo”,
“Mr. ”
);

Multi-dimensional arrays for Java are similar to those of PHP in that the sub-arrays may vary in length. Multi-dimensional arrays are basically maps where specific keys being mapped to a value.

Lesson 29: Print Arrays

Java PHP
for (int value : numArray) { // Foreach Loop
System.out.println(“Number is” + value); // Print Array value
}
foreach ($numArray as $value) { // Foreach Loop
echo “Number is” . $value; // Print Array value
}

Array values can be printed individually in both Java and PHP. It is much easier to use a loop to print or manipulate the array values.

ArrayTypes.java file

/**
 * Ojambo.com Array Types Java Tutorial
 * ArrayTypes.java Copyright 2011 Edward
 * http://www.ojambo.com
 * 
 */
 
// This class displays Array Types
public class ArrayTypes { //This class is called ArrayTypes
    public static void main(String[] args) { // Main Method
        // Declare variables and assign values immediately 
	    int[] numArray2 = {1, 2, 3, 4, 5}; // Integer array
            
        // Multi-dimensional Array
        String[][] stringArray = { // String array 
			{"Dr. ", "Ojambo"}, 
			{"Ms. ", "Ajambo"}, 
			{"Mr. "} 
		}; 	
        
        // Print Arrays
		for (int value : numArray2) { // Foreach Loop 
			System.out.println("Number is" + value); // Print Array value 
		}
		for (String stringKey[] : stringArray) { // Foreach Loop 
			for (String value : stringKey) { // Inner foreach loop
				System.out.print(value); // Print Array value 
			}
			System.out.println();
		}
		
    }
}

The ArrayTypes.java file can be compiled as javac ArrayTypes.java and executed with java ArrayTypes.

ArrayTypes.php file

<?php
/*
 * Ojambo.com Array Types PHP Tutorial
 * ArrayTypes.php Copyright 2011 Edward
 * http://www.ojambo.com
 * 
 */

	// Declare variables and assign values immediately 
	$numArray2 = array(1, 2, 3, 4, 5); // Array
		
   // Multi-dimensional Array
	$stringArray = array( // String array 
		"Dr. " => "Ojambo", 
		"Ms. " => "Ajambo", 
		"Mr. " 
	);		
	
	// Print Arrays
	foreach ($numArray2 as $value) { // Foreach Loop 
		echo "Number is" . $value; // Print Array value 
	} 
	foreach ($stringArray as $stringKey => $value) { // Foreach Loop 
		echo $stringKey . $value; // Print Array value 
	} 

?>

The ArrayTypes.php can be compiled as php -l ArrayTypes.php and compiled with php ArrayTypes.php

Conclusion:

Java variables must begin with a lowercase letter. In Java, every variable must be specifically declared while PHP only requires a dollar sign. Sometimes Java and PHP share the same Array Types such as the integer and string arrays.

In Java and PHP, foreach loops can be used to manipulate arrays. For example, a loop can be used to print the values of the array. This is the end of the Introduction to Java and PHP series.

    Recommendations:

  1. This tutorial uses an optional lightweight programming editor with syntax-highlighting features.
  2. The concepts taught in the first twenty-nine lessons can be applied to other programming languages.
  3. When selecting a programming language to master, pay close attention to license restrictions.