note 114661 added to function.array-walk

From: Date: Wed, 19 Mar 2014 12:38:56 +0000
Subject: note 114661 added to function.array-walk
Groups: php.notes 
Request: Send a blank email to php-notes+get-198979@lists.php.net to get a copy of this message
If the project often change the datatype of the array. Если в проекте нужно часто менять тип данных в массиве. <?php /** * Callback-функция (для array_walk) преобразует тип данных элементов массива. * @param mixed $item Значение элемента обрабатываемого массива. * @param int|string $key Ключ элемента массива. * @param string $type Тип данных, в который преобразуем элементы массива. */ function convertTypeValueArray(&$item, $key, $type) { $arValidType = array("boolean", "bool", "integer", "int", "float", "string", "array", "object", "null"); $type = strtolower($type); if(in_array($type, $arValidType)) { settype($item, $type); } } $arTest = array("", 1, " 2", 3.1, "4.2", " 5 "); array_walk($arTest, "convertTypeValueArray", "int"); var_dump($arTest); // array(6) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) [5]=> int(5) } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 109.198.191.125 ---- Manual Page -- http://php.net/manual/en/function.array-walk.php Edit -- https://master.php.net/note/edit/114661 Del: integrated -- https://master.php.net/note/delete/114661/integrated Del: useless -- https://master.php.net/note/delete/114661/useless Del: bad code -- https://master.php.net/note/delete/114661/bad+code Del: spam -- https://master.php.net/note/delete/114661/spam Del: non-english -- https://master.php.net/note/delete/114661/non-english Del: in docs -- https://master.php.net/note/delete/114661/in+docs Del: other reasons-- https://master.php.net/note/delete/114661 Reject -- https://master.php.net/note/reject/114661 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198979) next »