Skip to content

How to use PHP arrays (for Java programmers)

PHP arrays differ from Java arrays, in the sense that they need not be indexed by integers -- they are similar to ordered hash maps.

// Create an array and place values in it:
$foo = [];
$foo[0] = 'first';
$foo[1] = 'second';

// Iterate over an array:
for ($i = 0; $i < count($foo); ++$i) {
    print($foo[$i] + " ");
}

// Provide a scalar key and map it to a value:
$array = [
    "foo" => "bar",
    "bar" => "foo",
];

In PHP when we write:

$user = array("name" => "John", "email" => "john@mail.com");

this is equivalent to a HashMap in Java:

HashMap<String, String> map = new HashMap<String, String>();
map.put("name", "John");
map.put("email", "john@mail.com");

A more complex example

MAP User ids to permissions

Here is a code snippet from the CollectionPermissionController located in the app\Http\Controllers folder in the FLDBv2 project:

$table_userid_to_permission = [];
foreach ($permissions as $permission) {
    $table_userid_to_permission[$permission->owner_id] = [];
}

foreach ($permissions as $permission) {
    $table_userid_to_permission[$permission->owner_id][$permission->permissions] = $permission;
}

Here we create a 2D array by iterating through the array of permissions retrieved from the database table collection_permissions.

In the first for loop, for each permission, the user id is used as a key to map to a 2nd empty array.

In the 2nd for loop, the permission string (create, view, update, delete, anonymised) is used as a key to map to the variable $permission, the CollectionPermission object retrieved from the database.

In Java the above would be the equivalent to Map\<String, Map\<String, CollectionPermission\>\>.

MAP user ids to permission verbs

// Create a 2d array of userids mapping to permissions verbs
// Map<String(user_id), List<String(permission verbs>>
$table_form_userid_to_permissionverb = [];
foreach ($permissionverbs as $permissionverb) {
    $userids = $request->input($permissionverb);
    // if the array exists, ie if 1 check box has been checked
    if ($userids) {
        foreach ($userids as $userid) {
            // if the userid key has not been created, create it
            if (!isset($table_form_userid_to_permissionverb[$userid])) {
                $table_form_userid_to_permissionverb[$userid] = [];
            }
            // add permission as value to userid key
            $table_form_userid_to_permissionverb[$userid][] = $permissionverb;
            }
        }
    }
}

In this code snippet, we create another 2d array.

First we iterate through the array of permissions verbs which are defined as constants in the CollectionPermission model. These permission verbs are for each CRUD verb + anonymised view.

The POST request contains a list of arrays for each CRUD verb + anon view and only the checked checkboxes in the form are added to this array when the request is made.

If an array is not empty, it iterates through the checked boxes (whose value is the associated users id), if that user id is not present as an associative key in the $table_form_userid_to_permissionverb array then that userid key is created, otherwise if present is maps the permission verb to the userid key.

In Java the above would be the equivalent to Map\<String, List\<String\>\>.

Further reading