在php中分配一个关联数组切片
发布时间:2020-12-13 17:02:04 所属栏目:PHP教程 来源:网络整理
导读:在perl中,我可以为散列中的多个值分配一个列表,如下所示: # define the hash...my %hash = ( foo = 1,bar = 2,baz = 3,);# change foo,bar,and baz to 4,5,and 6 respectively@hash{ 'foo','bar','baz' } = ( 4,6 ); 有没有办法在PHP中做同样的事情?事实上
在perl中,我可以为散列中的多个值分配一个列表,如下所示:
# define the hash... my %hash = ( foo => 1,bar => 2,baz => 3,); # change foo,bar,and baz to 4,5,and 6 respectively @hash{ 'foo','bar','baz' } = ( 4,6 ); 有没有办法在PHP中做同样的事情?事实上,有没有办法获得一个关联数组? 解决方法
没有与Perl语法等效的东西.但是您可以创建一系列感兴趣的键并使用它来仅更改数组的一部分.
$koi=array('foo','baz' ); foreach($koi as $k){ $myarr[$k]++; //or whatever } 要么 array_walk($myarr,create_function('&$v,$k','$v=(in_array($k,$koi))? $v*2 : $v;')); //you'd have to define $koi inside the function (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |