如何从php上的数组值中分离元素并构建新数组?
我已获得此阵列(来自csv文件): 如何构建如下所示的新阵列:如何从php上的数组值中分离元素并构建新数组?,php,arrays,Php,Arrays,我已获得此阵列(来自csv文件): 如何构建如下所示的新阵列: [0] : ( 'entity_id' => '667', 'commission_book' => '667', 'old_price' => '667', 'new_price' => '667', ); [1] : ( 'entity_id' => '668', 'commission_book' => '668', 'old_price' => '668', 'new
[0] : (
'entity_id' => '667',
'commission_book' => '667',
'old_price' => '667',
'new_price' => '667',
);
[1] : (
'entity_id' => '668',
'commission_book' => '668',
'old_price' => '668',
'new_price' => '668',
)
换句话说,我想用第一个数组构建2个对象,请问有什么方法可以实现吗?我已经试了几个小时了这是一种简单但优雅的方法:
<?php
$input = [
0 => 'entity_id;commission_book;old_price;new_price',
1 => '667;667;667;667',
2 => '668;668;668;668'
];
$output = [];
// drop header entry
array_shift($input);
// process remaining entries
foreach ($input as $key=>$entry) {
$x = &$output[$key];
list(
$x['entity_id'],
$x['commission_book'],
$x['old_price'],
$x['new_price']
) = explode(';', $entry);
}
print_r($output);
这是一种简单但优雅的方法:
<?php
$input = [
0 => 'entity_id;commission_book;old_price;new_price',
1 => '667;667;667;667',
2 => '668;668;668;668'
];
$output = [];
// drop header entry
array_shift($input);
// process remaining entries
foreach ($input as $key=>$entry) {
$x = &$output[$key];
list(
$x['entity_id'],
$x['commission_book'],
$x['old_price'],
$x['new_price']
) = explode(';', $entry);
}
print_r($output);
使用
阵列\u切片
和阵列\u组合
的简短解决方案:
$from_csv = [
0 => 'entity_id;commission_book;old_price;new_price',
1 => '667;667;667;667',
2 => '668;668;668;668'
];
$result = [];
$keys = explode(";", $from_csv[0]); // header fields
foreach(array_slice($from_csv, 1) as $v){
$result[] = array_combine($keys, explode(";", $v));
}
echo '<pre>';
var_dump($result);
// the output:
array(2) {
[0]=>
array(4) {
["entity_id"]=>
string(3) "667"
["commission_book"]=>
string(3) "667"
["old_price"]=>
string(3) "667"
["new_price"]=>
string(3) "667"
}
[1]=>
array(4) {
["entity_id"]=>
string(3) "668"
["commission_book"]=>
string(3) "668"
["old_price"]=>
string(3) "668"
["new_price"]=>
string(3) "668"
}
}
$from\u csv=[
0=>“实体id;佣金簿;旧价格;新价格”,
1 => '667;667;667;667',
2 => '668;668;668;668'
];
$result=[];
$keys=explode(“;”,$from_csv[0]);//标题字段
foreach(数组\u切片($from\u csv,1)作为$v){
$result[]=array_combine($keys,explode(;“,$v));
}
回声';
var_dump($结果);
//输出:
阵列(2){
[0]=>
阵列(4){
[“实体id”]=>
字符串(3)“667”
[“佣金簿”]=>
字符串(3)“667”
[“旧价格”]=>
字符串(3)“667”
[“新价格”]=>
字符串(3)“667”
}
[1]=>
阵列(4){
[“实体id”]=>
字符串(3)“668”
[“佣金簿”]=>
字符串(3)“668”
[“旧价格”]=>
字符串(3)“668”
[“新价格”]=>
字符串(3)“668”
}
}
使用阵列\u切片和阵列\u组合的简短解决方案
:
$from_csv = [
0 => 'entity_id;commission_book;old_price;new_price',
1 => '667;667;667;667',
2 => '668;668;668;668'
];
$result = [];
$keys = explode(";", $from_csv[0]); // header fields
foreach(array_slice($from_csv, 1) as $v){
$result[] = array_combine($keys, explode(";", $v));
}
echo '<pre>';
var_dump($result);
// the output:
array(2) {
[0]=>
array(4) {
["entity_id"]=>
string(3) "667"
["commission_book"]=>
string(3) "667"
["old_price"]=>
string(3) "667"
["new_price"]=>
string(3) "667"
}
[1]=>
array(4) {
["entity_id"]=>
string(3) "668"
["commission_book"]=>
string(3) "668"
["old_price"]=>
string(3) "668"
["new_price"]=>
string(3) "668"
}
}
$from\u csv=[
0=>“实体id;佣金簿;旧价格;新价格”,
1 => '667;667;667;667',
2 => '668;668;668;668'
];
$result=[];
$keys=explode(“;”,$from_csv[0]);//标题字段
foreach(数组\u切片($from\u csv,1)作为$v){
$result[]=array_combine($keys,explode(;“,$v));
}
回声';
var_dump($结果);
//输出:
阵列(2){
[0]=>
阵列(4){
[“实体id”]=>
字符串(3)“667”
[“佣金簿”]=>
字符串(3)“667”
[“旧价格”]=>
字符串(3)“667”
[“新价格”]=>
字符串(3)“667”
}
[1]=>
阵列(4){
[“实体id”]=>
字符串(3)“668”
[“佣金簿”]=>
字符串(3)“668”
[“旧价格”]=>
字符串(3)“668”
[“新价格”]=>
字符串(3)“668”
}
}
$array=array(
0=>“实体id;佣金簿;旧价格;新价格”,
1 => '667;667;667;667',
2 => '668;668;668;668');
$array[0]=分解(“;”,$array[0]);
$array[1]=分解(“;”,$array[1]);
$array[2]=分解(“;”,$array[2]);
$newarray=array();
对于($i=0;$i$array=array)(
0=>“实体id;佣金簿;旧价格;新价格”,
1 => '667;667;667;667',
2 => '668;668;668;668');
$array[0]=分解(“;”,$array[0]);
$array[1]=分解(“;”,$array[1]);
$array[2]=分解(“;”,$array[2]);
$newarray=array();
对于($i=0;$iWell,我希望你这么做。我们都这么做。否则生活会很无聊,不是吗?做了另一次修改,foreach循环更容易遵循,并且该版本对输入键中的间隙更可靠。嗯,我希望你这样做。我们都这样做。否则生活会很无聊,不是吗?做了另一次修改,foreach循环更容易遵循输入键d该版本对输入键中的间隙更为健壮。
$array = array(
0 => 'entity_id;commission_book;old_price;new_price',
1 => '667;667;667;667',
2 => '668;668;668;668');
$array[0] = explode(";",$array[0]);
$array[1] = explode(";",$array[1]);
$array[2] = explode(";",$array[2]);
$newarray = array();
for ($i=0;$i<count($array[0]);$i++){
$newarray[0][$array[0][$i]] = $array[1][$i];
$newarray[1][$array[0][$i]] = $array[2][$i];
}
echo "<pre>";
var_dump($array);
var_dump($newarray);
echo "</pre>";