From PHP to Android, the simple foreach loop example

Switching from one programming language to another usually means checking up on few basic things and trying to replicate code functionality from one language to another. For those of you in the PHP world like me that are trying to start the Android development here is a brief look on how foreach loop can be done in Android (Java).

 * PHP version
 * $item = array('group'=>'customer', 'pass'=>'mypass123', 'email'=>'');
 * $item2 = array('group'=>'admin', 'pass'=>'mypass345', 'email'=>'');
 * list = array($item, $item2);
ArrayList<hashmap <String, String>> list = new ArrayList</hashmap><hashmap <String, String>>();
HashMap<string , String> item = new HashMap</string><string , String>();
HashMap</string><string , String> item2 = new HashMap</string><string , String>();
item.put("group", "customer");
item.put("pass", "mypass123");
item.put("email", "");
item2.put("group", "admin");
item2.put("pass", "mypass345");
item2.put("email", "");     
 * PHP version
 * foreach($list as $hashMapItem) {
 * 		var_dump($hashMapItem['email']);
 * 		var_dump($hashMapItem['group']);
 * 		var_dump($hashMapItem['pass']); 
 * }
for(HashMap</string><string , String> hashMapItem : list) {
	Log.d("MY_DEBUG_STUFF_EMAIL", hashMapItem.get("email"));
	Log.d("MY_DEBUG_STUFF_GROUP", hashMapItem.get("group"));
	Log.d("MY_DEBUG_STUFF_PASS", hashMapItem.get("pass"));

Short but hopefully helpful.

You made it all the way down here so you must have enjoyed this post! You may also like:

Branko Ajzele, | 13

Parsing the XMLRPC webservice response in Android

Branko Ajzele, | 2

From PHP to Android, the simple stuff – Intents and the POST-GET analogy

Branko Ajzele, | 8

From PHP to Android, the simple stuff – What about arrays

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.