快速多维数组到单个数组
shift multidimentional array to single array
我想从父数组中删除键 0 并将子数组设置为父数组。
在这里我将获得单个值,所以一个数组对我来说没问题。
我当前的数组如下所示
Array
(
[0] => Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27@gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
)
我想要像下面这样的。预期结果
Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27@gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
为此我尝试了如下但没有成功。
$new = array();
foreach ($data as $v){
$new = array_merge($new , array_values($v)) ;
}
但在我的代码中它被删除了密钥,例如 id,api_key,等等....
我的新数组中也需要键名。请推荐
删除 array_values
解决方案
<?php
$test = array(
array
(
'id' => 3,
'api_key' => 'acount266'
)
);
$new = array();
foreach($test as $v){
$new = array_merge($new, $v);
}
var_dump($new);
结果
array(2) {
["id"]=>
int(3)
["api_key"]=>
string(9) "acount266"
}
我试过这个:
$arr = array();
foreach ($examples as $example) {
foreach ($example as $e) {
array_push($arr, $e);
}
}
根据 PHP 的文档所述
reset() function returns the value of the first array element, or
FALSE if the array is empty.
$array = array(
array(
'id' => 3,
'api_key' => 'acount266',
'auth_domain' => 'Tester26',
'database_url' => 'vcc.test.acc+27@gmail.com',
'project_id' => '12345',
'storage_bucket' => '',
'secret_key_path' => '',
'fcm_server_key' => 1,
'messaging_sender_id' => 0,
'key_phrase' => '',
'disable' => 0,
'created' => '',
'updated' => ''
)
);
print_r(reset($test));
不要过于复杂,将第一个元素重新分配给父数组既快速又简单:
<?php
$array =
array (
0 =>
array (
'first' => 'Michael',
'last' => 'Thompson'
)
);
$array = $array[0];
var_export($array);
输出:
array (
'first' => 'Michael',
'last' => 'Thompson',
)
或者:
$array = array_shift($array);
我想从父数组中删除键 0 并将子数组设置为父数组。 在这里我将获得单个值,所以一个数组对我来说没问题。
我当前的数组如下所示
Array
(
[0] => Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27@gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
)
我想要像下面这样的。预期结果
Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27@gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
为此我尝试了如下但没有成功。
$new = array();
foreach ($data as $v){
$new = array_merge($new , array_values($v)) ;
}
但在我的代码中它被删除了密钥,例如 id,api_key,等等....
我的新数组中也需要键名。请推荐
删除 array_values
解决方案
<?php
$test = array(
array
(
'id' => 3,
'api_key' => 'acount266'
)
);
$new = array();
foreach($test as $v){
$new = array_merge($new, $v);
}
var_dump($new);
结果
array(2) {
["id"]=>
int(3)
["api_key"]=>
string(9) "acount266"
}
我试过这个:
$arr = array();
foreach ($examples as $example) {
foreach ($example as $e) {
array_push($arr, $e);
}
}
根据 PHP 的文档所述
reset() function returns the value of the first array element, or FALSE if the array is empty.
$array = array(
array(
'id' => 3,
'api_key' => 'acount266',
'auth_domain' => 'Tester26',
'database_url' => 'vcc.test.acc+27@gmail.com',
'project_id' => '12345',
'storage_bucket' => '',
'secret_key_path' => '',
'fcm_server_key' => 1,
'messaging_sender_id' => 0,
'key_phrase' => '',
'disable' => 0,
'created' => '',
'updated' => ''
)
);
print_r(reset($test));
不要过于复杂,将第一个元素重新分配给父数组既快速又简单:
<?php
$array =
array (
0 =>
array (
'first' => 'Michael',
'last' => 'Thompson'
)
);
$array = $array[0];
var_export($array);
输出:
array (
'first' => 'Michael',
'last' => 'Thompson',
)
或者:
$array = array_shift($array);