Is There Java Hashmap Equivalent in PHP

Is there Java HashMap equivalent in PHP?

Arrays in PHP can have Key Value structure.

Is there any way to declare 'Hybrid' HashMap of java like in PHP

It seems that a Map<String, Object> is needed and may be created but it won't be convenient to use because the values are raw objects:

Map<String, Object> map = Map.of(
"key_0", Map.of("name", "category0", "sub", Collections.emptyList()),
"key_1", Map.of("name", "category1", "sub", List.of("sub_1", "sub_2", "sub_3")),
"key_2", Map.of("name", "category2", "sub", Map.of(
"sub_1", List.of("sub_1_1", "sub_1_2"),
"sub_2", List.of("sub_2_1", "sub_2_2")
))
);

Converting Java hashmap initializer to PHP equivalent

Arrays in PHP have a key-value structure...thus is correct:

$this->headers[$key] = $value;

In fact, the PHP manual says:

An array in PHP is actually an ordered map.

http://php.net/manual/de/language.types.array.php

Although, according to How is the PHP array implemented on the C level? , it is actually a HashTable, which means you can rely on the O(1) lookup.

How can I implement the java hashmap by using PHP?

First, trim off whitespace by using trim(fgets($_fp)) everywhere instead of just fgets($_fp) -- that fixes things on my end at least.

Second, the code you pasted is missing the closing curly bracket on your second for loop.

Third, have fun with 30 Days of Code :-) (once you get the above straightened out you also need to have your code read in the number of entries at the beginning, and "Read the queries until end-of-file" at the end).

What is Java equivalent for PHP array with non-numeric keys?

You can use a HashMap or a Hashtable. Not sure which one to use? Read the API or check out this question which discusses the pros and cons of each.

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

Java code equivalent to following php code

Map<String, String> newPerson = new HashMap<String, String>()
{{
put("firstname", "First");
put("lastname", "Last");
put("email", "test@example.com");
}};

Java equivalent to PHP's multidimensional arrays

There is no direct equivalent in Java. You would have to create a class Person with 4 fields firstName, lastName, job and age, and then use a Person[].

searching for PHP equivalent for Java's Map and List?

In PHP, arrays serve as all types of containers. They can be used as maps, lists and sets.

Linked list functionality is provided through the reset/prev/current/next/end functions; have a look at the "see also" section of the docs to make sure you don't miss any of them.

For map functionality you just index into the array directly; but keep in mind that array keys in PHP can only be strings or integers, and also that most of the time mixing both in the same array is not the right thing to do (although there is no technical reason not to, and there are cases where it makes sense).

SPL provides the SplDoublyLinkedList class, but there's nothing in there that a simple array doesn't also give you.



Related Topics



Leave a reply



Submit