下面显示的是我在表单中发布的数据。由于我使用了多形式方法,因此我将发布的数据作为数组。我想将每个值保存到数据库中,即1,2,3.etc,因为它是属性“since_value”value。我怎么只得到这个值作为数组呢?
Array (
[0] => Array ( [some_value] => Array ( [0] => 1 ) )
[1] => Array ( [some_value] => Array ( [0] => 2 ) )
[2] => Array ( [some_value] => Array ( [0] => 3 ) )
[3] => Array ( [some_value] => Array ( [0] => 4 ) )
[4] => Array ( [some_value] => Array ( [0] => 5 ) )
[5] => Array ( [some_value] => Array ( [0] => 6 ) )
[6] => Array ( [some_value] => Array ( [0] => 7 ) )
)
我想让它像
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 )
我将如何做?
我试过
$x=implode('',$array);
但是它给了我一个数组到字符串的转换错误。
答案 0 :(得分:1)
这样做
<?php
$arr=Array (
0 => Array ( 'some_value' => Array ( 0 => 1 ) ),
1 => Array ( 'some_value' => Array ( 0 => 2 ) ),
2 => Array ( 'some_value' => Array ( 0 => 3 ) ),
3 => Array ( 'some_value' => Array ( 0 => 4 ) ),
4 => Array ( 'some_value' => Array ( 0 => 5 ) ),
5 => Array ( 'some_value' => Array ( 0 => 6 ) ),
6 => Array ( 'some_value' => Array ( 0 => 7 ) )
);
$new_arr= array();
foreach($arr as $arr1)
{
foreach($arr1['some_value'] as $v){
$new_arr[]=$v;
}
}
print_r($new_arr);
使用单一foreach
$new_arr= array();
foreach($arr as $k=>$arr1)
{
$new_arr[]=$arr1['some_value'][0];
}
print_r($new_arr);
答案 1 :(得分:1)
<?php
$data = array(
array('some_value' => array(1) ),
array('some_value' => array(2) ),
array('some_value' => array(3) ),
array('some_value' => array(4) ),
array('some_value' => array(5) ),
array('some_value' => array(6) ),
array('some_value' => array(7) ),
);
$output = array();
foreach($data as $k => $v)
{
$output[$k] = $v['some_value'][0];
}
输出:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)
答案 2 :(得分:0)
试试这个:
function array_flatten($array) {
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)){ $return = array_merge($return, array_flatten($value));}
else {$return[$key] = $value;}
}
return $return;
}
$array = array (
array ( "some_value" => array ( 1 ) ),
array ( "some_value" => array ( 2 ) ),
array ( "some_value" => array ( 3 ) ),
array ( "some_value" => array ( 4 ) ),
array ( "some_value" => array ( 5 ) ),
array ( "some_value" => array ( 6 ) ),
array ( "some_value" => array ( 7 ) )
);
$result = array_flatten($array);
echo "<pre>";
print_r($result);
输出:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)