note 118405 added to control-structures.foreach

From: Date: Tue, 01 Dec 2015 14:08:04 +0000
Subject: note 118405 added to control-structures.foreach
Groups: php.notes 
Request: Send a blank email to php-notes+get-204254@lists.php.net to get a copy of this message
String keys of associative arrays, for which is_numeric() is true and which can be type-juggled to an int will be cast to an int! If the key is on the other hand a string that can be type-juggled into a float, it will stay a string. (Observed on PHP 7.0.0RC8) <?php $arr = array(); $arr[0] = "zero"; // will stay an int $arr["1"] = "one"; // will be cast to an int ! $arr["two"] = "2"; // will stay a string $arr["3.5"] = "threeandahalf"; // will stay a string foreach($arr as $key => $value) { var_dump($key); } ?> The output will be int(0) int(1) string(3) "two" string(3) "3.5" ---- Server IP: 72.52.91.14 Probable Submitter: 147.142.120.210 ---- Manual Page -- http://php.net/manual/en/control-structures.foreach.php Edit -- https://master.php.net/note/edit/118405 Del: integrated -- https://master.php.net/note/delete/118405/integrated Del: useless -- https://master.php.net/note/delete/118405/useless Del: bad code -- https://master.php.net/note/delete/118405/bad+code Del: spam -- https://master.php.net/note/delete/118405/spam Del: non-english -- https://master.php.net/note/delete/118405/non-english Del: in docs -- https://master.php.net/note/delete/118405/in+docs Del: other reasons-- https://master.php.net/note/delete/118405 Reject -- https://master.php.net/note/reject/118405 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204254) next »