0

Following code:

<?php
	$test_array = array();
	$test_array['string_index'] = "data in string index";
	$test_array[] = "data in index 0";
	$test_array[] = "data in index 1";
	$test_array[] = "data in index 2";
	
	foreach($test_array as $key => $val)
	{
		if($key != 'string_index')
		{
			echo $val."<br>";
		}
	}
?>

gives result:

data in index 1
data in index 2

Question is - where is "data in index 0"??? How to get elements from numeric indices 0-n?
Also if I change 'string_index' to something else which doesn't exist, it echoes everything except [0]. Plz, explain me this.

Thnx in advance

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by jkon
1

Where is the index 0 wow …. Well this is a good one I must admit… $key != "string_index" is the thing, 0 is also false. When you test with a comparison operator with false the result will be false. If you put $key !== "string_index" (witch means that are not equal OR they don’t share the same type) then all will be fixed.

0

Loose data type … or dynamic … has always tangles.

Edited by jkon: The original message was for other thread… sorry

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.