重组数组以将元素从第 3 个位置连接到数组末尾并存储为第 3 个元素
Restructure array to join elements from 3rd position to end of array and store as 3rd element
我有这样的数组:
Array(
[0] => 85
[1] => 85167920
[2] => ELECTRICAL/ELECTRONIC
[3] => DEVICES
[4] => FOR
[5] => REPELLING
[6] => INSECTS
[7] => (E.G.MOSQUITOES
[8] => ETC)
)
和
Array(
[0] => 85
[1] => 851680
[2] => ELECTRIC
[3] => HEATING
[4] => RESISTORS
)
我想要这样的数组:
Array(
[0] => 85
[1] => 851680
[2] => ELECTRIC HEATING RESISTORS
)
和
Array(
[0] => 85
[1] => 85167920
[2] => ELECTRICAL/ELECTRONIC DEVICES FOR REPELLING INSECTS (E.G.MOSQUITOES ETC)
)
我不确定使用什么方法:合并/合并/推送。
我应该使用什么?
使用array_slice()
和implode()
方法:(Demo)
$array=[85,851680,'ELECTRIC','HEATING','RESISTORS'];
$array=[$array[0],$array[1],implode(' ',array_slice($array,2))];
var_export($array);
输出:
array (
0 => 85,
1 => 851680,
2 => 'ELECTRIC HEATING RESISTORS',
)
这应该可以正常工作:
<?php
$a = [
85,
85167920,
'ELECTRICAL/ELECTRONIC',
'DEVICES',
'FOR',
'REPELLING',
'INSECTS',
'(E.G.MOSQUITOES
ETC)'
];
$b = [
85,
851680,
'ELECTRIC',
'HEATING',
'RESISTORS'
];
$aa = array_slice($a, 0,2);
$aa[] = implode(' ',array_slice($a, 2));
$bb = array_slice($b, 0,2);
$bb[] = implode(' ',array_slice($b, 2));
print_r($aa);
echo '<br>';
print_r($bb);
?>
试试下面的代码,
<?php
$array = array();
$temp_array = array();
$temp_str_array=array();
$array[]=85;
$array[]=85167920;
$array[]='ELECTRICAL/ELECTRONIC';
$array[]='DEVICES';
$array[]='FOR';
$array[]='REPELLING';
$array[]='INSECTS';
$array[]='(E.G.MOSQUITOES';
$array[]='ETC)';
foreach($array as $k=>$values){
if($k<=1){
$temp_array[$k]=$values;
}
else{
$temp_str_array[]=$values;
}
if(count($array)==($k+1)){
$temp_array[3]=implode(" ",$temp_str_array);
}
}
echo "<pre>";
print_r($temp_array);
?>
输出,
Array
(
[0] => 85
[1] => 85167920
[3] => ELECTRICAL/ELECTRONIC DEVICES FOR REPELLING INSECTS
(E.G.MOSQUITOES ETC)
)
我有这样的数组:
Array(
[0] => 85
[1] => 85167920
[2] => ELECTRICAL/ELECTRONIC
[3] => DEVICES
[4] => FOR
[5] => REPELLING
[6] => INSECTS
[7] => (E.G.MOSQUITOES
[8] => ETC)
)
和
Array(
[0] => 85
[1] => 851680
[2] => ELECTRIC
[3] => HEATING
[4] => RESISTORS
)
我想要这样的数组:
Array(
[0] => 85
[1] => 851680
[2] => ELECTRIC HEATING RESISTORS
)
和
Array(
[0] => 85
[1] => 85167920
[2] => ELECTRICAL/ELECTRONIC DEVICES FOR REPELLING INSECTS (E.G.MOSQUITOES ETC)
)
我不确定使用什么方法:合并/合并/推送。
我应该使用什么?
使用array_slice()
和implode()
方法:(Demo)
$array=[85,851680,'ELECTRIC','HEATING','RESISTORS'];
$array=[$array[0],$array[1],implode(' ',array_slice($array,2))];
var_export($array);
输出:
array (
0 => 85,
1 => 851680,
2 => 'ELECTRIC HEATING RESISTORS',
)
这应该可以正常工作:
<?php
$a = [
85,
85167920,
'ELECTRICAL/ELECTRONIC',
'DEVICES',
'FOR',
'REPELLING',
'INSECTS',
'(E.G.MOSQUITOES
ETC)'
];
$b = [
85,
851680,
'ELECTRIC',
'HEATING',
'RESISTORS'
];
$aa = array_slice($a, 0,2);
$aa[] = implode(' ',array_slice($a, 2));
$bb = array_slice($b, 0,2);
$bb[] = implode(' ',array_slice($b, 2));
print_r($aa);
echo '<br>';
print_r($bb);
?>
试试下面的代码,
<?php
$array = array();
$temp_array = array();
$temp_str_array=array();
$array[]=85;
$array[]=85167920;
$array[]='ELECTRICAL/ELECTRONIC';
$array[]='DEVICES';
$array[]='FOR';
$array[]='REPELLING';
$array[]='INSECTS';
$array[]='(E.G.MOSQUITOES';
$array[]='ETC)';
foreach($array as $k=>$values){
if($k<=1){
$temp_array[$k]=$values;
}
else{
$temp_str_array[]=$values;
}
if(count($array)==($k+1)){
$temp_array[3]=implode(" ",$temp_str_array);
}
}
echo "<pre>";
print_r($temp_array);
?>
输出,
Array
(
[0] => 85
[1] => 85167920
[3] => ELECTRICAL/ELECTRONIC DEVICES FOR REPELLING INSECTS
(E.G.MOSQUITOES ETC)
)