PHP和JavaScrip分别获取关联数组的键值示例代码
编程学习 2021-07-05 09:49www.dzhlxh.cn编程入门
关联数组的键值获取,有很多方法,在本文为大家介绍下PHP和JavaScrip中时如何实现的,感兴趣的朋友可以参考下
PHP版:
$o = array('x'=>1, 'y'=>2, 'z'=>3);
$arr = array();
$i = 0;
foreach( $o as $arr[$i++]=>$v )/*empty*/;
var_dump($arr); //$arr = array('x', 'y', 'z');
JavaScrip版:
var o = {x:1, y:2, z:3};
var arr = [], i = 0;
for ( arr[i++] in o ) /*empty*/ ;
console.log( arr ); //arr = ['x', 'y', 'z'];
代码如下:
$o = array('x'=>1, 'y'=>2, 'z'=>3);
$arr = array();
$i = 0;
foreach( $o as $arr[$i++]=>$v )/*empty*/;
var_dump($arr); //$arr = array('x', 'y', 'z');
JavaScrip版:
代码如下:
var o = {x:1, y:2, z:3};
var arr = [], i = 0;
for ( arr[i++] in o ) /*empty*/ ;
console.log( arr ); //arr = ['x', 'y', 'z'];