更改数组格式
Change the Array Format
我有一个打印出这个值的页面:
"Firstname","Ana","George","Wilson"
"Lastname","Smith","Spencer","Carey"
"Age","18","20","22"
我使用 file_get_contents 和 str_getcsv 获得这些值。
$array= str_getcsv($test);
我得到的数组结果是这样的
Array ( [0] =>
"Firstname"
[1] => 'Ana'
[2] => 'George'
[3] => 'Wilson'
"Lastname"
[4] => 'Smith'
[5] => 'Spencer'
[6] => 'Carey'
"Age"
[7] => 18
[8] => 20
[9] => 22
))
有没有办法把Array格式改成这样?
Array
(
[0] => Array
(
[0] => 'Ana'
[1] => 'George'
[2] => 'Wilson'
)
[1] => Array
(
[0] => 'Smith'
[1] => 'Spencer'
[2] => 'Carey'
)
[2] => Array
(
[0] => 18
[1] => 20
[2] => 22
)
)
我建议您使用 file()
,而不是使用 file_get_contents()
和 str_getcsv()
像下面一样:-
<?php
$test = file('test.txt'); // you can add your file url here
echo "<pre/>";print_r($test);// initial array
foreach($test as &$te){
$new_array = explode(',',$te);
unset($new_array[0]);
$te = array_values($new_array);
}
echo "<pre/>";print_r($test); // modified and desired array
要将给定数组更改为指定格式:
$array = ['Firstname' => ['Ana', 'George', 'Wilson'], 'Lastname' => ['Smith', 'Spencer', 'Carey'], 'Age' => [18, 20, 22]];
$new_array = [];
foreach ($array as $key => $value) {
$new_array[] = $value;
}
print_r($new_array);
Array
(
[Firstname] => Array
(
[0] => Ana
[1] => George
[2] => Wilson
)
[Lastname] => Array
(
[0] => Smith
[1] => Spencer
[2] => Carey
)
[Age] => Array
(
[0] => 18
[1] => 20
[2] => 22
)
)
兄弟试试这个,希望能帮上忙:
$array = array(0=> array(
'firstname' => array(1 => 'Ana', 2 => 'George', 3=>'Wilson'),
'lastname' => array(4=>'Smith', 5=>'Spencer', 6=> 'Carey'),
'age' => array(7=>18,8=>20,9=>22)
));
$newArray = array();
echo "<pre/>";print_r($array);
foreach($array as $key=>$new){
foreach($new as $k=>$d){
$newArray[] = array_values($d);
}
}
echo "</br>New Array Format: </br>";
echo "<pre/>";print_r($newArray);
我有一个打印出这个值的页面:
"Firstname","Ana","George","Wilson"
"Lastname","Smith","Spencer","Carey"
"Age","18","20","22"
我使用 file_get_contents 和 str_getcsv 获得这些值。
$array= str_getcsv($test);
我得到的数组结果是这样的
Array ( [0] =>
"Firstname"
[1] => 'Ana'
[2] => 'George'
[3] => 'Wilson'
"Lastname"
[4] => 'Smith'
[5] => 'Spencer'
[6] => 'Carey'
"Age"
[7] => 18
[8] => 20
[9] => 22
))
有没有办法把Array格式改成这样?
Array
(
[0] => Array
(
[0] => 'Ana'
[1] => 'George'
[2] => 'Wilson'
)
[1] => Array
(
[0] => 'Smith'
[1] => 'Spencer'
[2] => 'Carey'
)
[2] => Array
(
[0] => 18
[1] => 20
[2] => 22
)
)
我建议您使用 file()
,而不是使用file_get_contents()
和 str_getcsv()
像下面一样:-
<?php
$test = file('test.txt'); // you can add your file url here
echo "<pre/>";print_r($test);// initial array
foreach($test as &$te){
$new_array = explode(',',$te);
unset($new_array[0]);
$te = array_values($new_array);
}
echo "<pre/>";print_r($test); // modified and desired array
要将给定数组更改为指定格式:
$array = ['Firstname' => ['Ana', 'George', 'Wilson'], 'Lastname' => ['Smith', 'Spencer', 'Carey'], 'Age' => [18, 20, 22]];
$new_array = [];
foreach ($array as $key => $value) {
$new_array[] = $value;
}
print_r($new_array);
Array
(
[Firstname] => Array
(
[0] => Ana
[1] => George
[2] => Wilson
)
[Lastname] => Array
(
[0] => Smith
[1] => Spencer
[2] => Carey
)
[Age] => Array
(
[0] => 18
[1] => 20
[2] => 22
)
)
兄弟试试这个,希望能帮上忙:
$array = array(0=> array(
'firstname' => array(1 => 'Ana', 2 => 'George', 3=>'Wilson'),
'lastname' => array(4=>'Smith', 5=>'Spencer', 6=> 'Carey'),
'age' => array(7=>18,8=>20,9=>22)
));
$newArray = array();
echo "<pre/>";print_r($array);
foreach($array as $key=>$new){
foreach($new as $k=>$d){
$newArray[] = array_values($d);
}
}
echo "</br>New Array Format: </br>";
echo "<pre/>";print_r($newArray);