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

Recommended Answers

All 2 Replies

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.

Loose data type … or dynamic … has always tangles.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.