note 114661 added to function.array-walk
| From: | ArsenyNikonian | 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