我有一个像这样的数组(它在数组中有一个数组。每个父数组只有一个子数组)
issue.ubsId issue.majorVersion issue.assetClass issue.assetType issue.securityType issue.active issue.lastUpdatedTime event.majorVersion event.lastUpdatedTime issue.status issue.isoCfi issue.isin issue.nominalCurrency issue.nominalValueOfUnit issue.issueName issue.regSInd issue.rule144AInd issue.securityFormType
issue.bbSecurityType issue.cusip issue.common issue.cins issue.valoren issue.wertpapier issue.ubsTradable issue.restrictedListCode issue.restrictedOffTime issue.restrictedOnTime
0200200307 13 EQUITY STOCK Common Stock 1 20171203120808000 15 20171203120810000 ACTIVE ES---- SE0005994001 XXX -9999.0 RLS GLOBAL PAID SUB -9999 -9999 UNKNOWN Common Stock 107404309
W7753D159 24542360 -9999 -9999 -9999
-9999 -9999.0 -9999.0 -9999 -9999.0 -9999
-9999 0 SS KA -9999 2 -9999.0 -9999 -9999 -9999 -9999 -9999 4 20150316054137973 0 -9999 -9999 0 -9999 0 0 -9999 9668256 2735465 -9999.0 -9999 -9999 -9999.0 -9999.0 -9999 -9999 -9999.0
我试图删除我的父数组(子数组之前的那个数组),并保留子数组,这样就可以将子数组转换为父数组。
我的期望如下:
$value4old = Array
(
[0] => Array
(
[0] => Array
(
[id] => 112
[id_tindak_lanjut] => 30.3
[nilai_tindak_lanjut] => 0
[memo_tindak_lanjut] =>
[tindak_lanjut_no] => 1
[tgl_tindak_lanjut] => 2018-01-30
)
)
[1] => Array
(
[0] => Array
(
[id] => 111
[id_tindak_lanjut] => 30
[nilai_tindak_lanjut] => 0
[memo_tindak_lanjut] =>
[tindak_lanjut_no] => 2
[tgl_tindak_lanjut] => 2018-01-07
)
)
[2] => Array
(
[0] => Array
(
[id] => 110
[id_tindak_lanjut] => 20.3
[nilai_tindak_lanjut] => 0
[memo_tindak_lanjut] =>
[tindak_lanjut_no] => 2
[tgl_tindak_lanjut] => 2017-10-24
)
)
[3] => Array
(
[0] => Array
(
[id] => 108
[id_tindak_lanjut] => 10.2
[nilai_tindak_lanjut] => 0
[memo_tindak_lanjut] =>
[tindak_lanjut_no] => 1
[tgl_tindak_lanjut] => 2018-01-19
)
[1] => Array
(
[id] => 109
[id_tindak_lanjut] => 30.3
[nilai_tindak_lanjut] => 0
[memo_tindak_lanjut] => Tes
[tindak_lanjut_no] => 2
[tgl_tindak_lanjut] => 2018-01-22
)
)
)
我试过这段代码
Array
(
[0] => Array
(
[id] => 112
[id_tindak_lanjut] => 30.3
[nilai_tindak_lanjut] => 0
[memo_tindak_lanjut] =>
[tindak_lanjut_no] => 1
[tgl_tindak_lanjut] => 2018-01-30
)
[1] => Array
(
[id] => 111
[id_tindak_lanjut] => 30
[nilai_tindak_lanjut] => 0
[memo_tindak_lanjut] =>
[tindak_lanjut_no] => 2
[tgl_tindak_lanjut] => 2018-01-07
)
[2] => Array
(
[id] => 110
[id_tindak_lanjut] => 20.3
[nilai_tindak_lanjut] => 0
[memo_tindak_lanjut] =>
[tindak_lanjut_no] => 2
[tgl_tindak_lanjut] => 2017-10-24
)
[3] => Array
(
[id] => 108
[id_tindak_lanjut] => 10.2
[nilai_tindak_lanjut] => 0
[memo_tindak_lanjut] =>
[tindak_lanjut_no] => 1
[tgl_tindak_lanjut] => 2018-01-19
)
[4] => Array
(
[id] => 109
[id_tindak_lanjut] => 30.3
[nilai_tindak_lanjut] => 0
[memo_tindak_lanjut] => Tes
[tindak_lanjut_no] => 2
[tgl_tindak_lanjut] => 2018-01-22
)
)
但我得到如下
$in = count($value4old)-1;
$val = array();
for ($i=0;$i<=$in;$i++) {
$val = array_merge($value4old[$i]);
};
print_r($val);
echo "</pre>";
我的代码可能出现什么问题?
答案 0 :(得分:2)
有很多方法可以做到这一点,您的代码也可以正常运行,但您在import {Button,Modal} from 'react-bootstrap'
<Button bsStyle="primary" type="submit">Create</Button>
中犯了一个小错误:
您写道:
array_merge
您需要的是
$val = array_merge($value4old[$i]);
基本上你每次只是做一个作业而不是合并,你最终得到了最后一个值。
答案 1 :(得分:2)
$new = [];
foreach ($value4old as $array) {
foreach($array as $inner) {
$new[] = $inner;
}
}
答案 2 :(得分:1)
要删除外部数组(当它只有一个值时),我总是使用current()
。
$array = [
'0' => ['key' => 'value'];
];
$array = current($array);
将导致:
array(1) {
'key' => 'value'
}
对于需要展平多个键的数组,我建议只使用foreach
循环。
$newArray = [];
foreach ($array as $key => $value) {
$newArray[$key] = current($value);
}