PHP和JavaScrip分别获取关联数组的键值示例代码
程序员文章站
2023-11-18 22:08:34
php版: 复制代码 代码如下: $o = array('x'=>1, 'y'=>2, 'z'=>3); $arr = array(); $i = 0;...
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'];