我有这样的数组。
array (size=4)
0 =>
array (size=1)
'serial_id' => string '214' (length=3)
1 =>
array (size=1)
'serial_id' => string '218' (length=3)
2 =>
array (size=1)
'serial_id' => string '221' (length=3)
3 =>
array (size=1)
'serial_id' => string '319' (length=3)
而且我希望将此作为单个数组
array (size=4)
'0' => string '214' (length=3)
'1' => string '218' (length=3)
'2' => string '221' (length=3)
'3' => string '319' (length=3)
我不想运行循环,是否有任何提供我所需输出的数组函数
有人可以帮我吗???我是php数组函数的新手。感谢所有答案,但正在等待使用数组函数的最短答案。这是我在php中的代码
<?php
$array = array( array('serial_id' => 214),
array('serial_id' => 218 ),
array('serial_id' => 221),
array('serial_id' => 319) );
var_dump($array);
$array2 = array(218,214,221,319);
var_dump($array2);
?>
您可以尝试使用此代码,我看到您正在更新您的问题
<?php
function convert_array($a) {
for($j = 0; $j < count($a); $j++) {
$b[$j] = $a[$j]['serial_id'];
}
return $b;
}
$array = array( array('serial_id' => 214),
array('serial_id' => 218 ),
array('serial_id' => 221),
array('serial_id' => 319) );
$array2 = convert_array($array);
var_dump($array2);
?>
如果使用php> 5.5.0
<?php
$array = array( array('serial_id' => 214),
array('serial_id' => 218 ),
array('serial_id' => 221),
array('serial_id' => 319) );
$array2 = array_column($array, 'serial_id');
var_dump($array2);
?>
尝试一下。这是您的旧阵列。尝试将其布置。以示例为例。
$array = array(0 => array("serial_id"=>"214"),
1 => array("serial_id"=>"218"),
2 => array("serial_id"=>"221"),
3 => array("serial_id"=>"319")
);
您可以使用foreach在一个新数组中获取内部元素$ new_array = array();foreach($ array as $ key => $ inner_array){
foreach($inner_array as $k=>$v){
$new_array[$k] = $v;
}
}
Output will be
$new_array = array(
'serial_id_1' => string '214',
'serial_id_2' => string '218',
'serial_id_3' => string '221',
'serial_id_4' => string '319',
);
按照下面的示例遍历数组以生成新数组:
$array = array("0" => array("serial_id"=>"214"),
"1" => array("serial_id"=>"218"),
"2" => array("serial_id"=>"221"),
"3" => array("serial_id"=>"319")
);
$new_arr=array();
foreach( $array as $key => $value ){
$new_arr["serial_id_".$key]=$value["serial_id"];
}
print_r($new_arr);
在这里尝试http://sandbox.onlinephpfunctions.com/code/2291c9eaf9842fdbeda4fd69bc707d7404b390d0
循环通过新数组
foreach( $new_arr as $key => $value ){
echo $key."\t=>\t".$value."\n";
}