在php中遍历一个数组,并将其存入一个一维数组中,

2024-12-02 18:55:31
推荐回答(4个)
回答1:

$result=Array (0 => Array ( 'days' => 20131217 ) ,
1 => Array ( 'days' => 20131218 ) ,
2 => Array ( 'days' => 20131219 ) ,
3 => Array ( 'days' => 20131220 ) ,
4 => Array ( 'days' => 20131221 ) ,
5 => Array ( 'days' => 20131222 ) ,
6 => Array ( 'days' => 20131223 ) ,
);

foreach($result as $key=>$value){
$newarr[]=$value['days']; //放入一维数组newarr
};

print_r($newarr);

//Array ( [0] => 20131217 [1] => 20131218 [2] => 20131219 [3] => 20131220 [4] => 20131221 [5] => 20131222 [6] => 20131223 )

回答2:

$days = array();
for($i=0;$i    $arr = $result[$i];
    echo $i." ";
    echo $arr['days']."
";
    $days[] = $arr['days'];
}
print_r($days);

回答3:


$result=Array (
    0 => Array ( 'days' => 20131217) ,
    1 => Array ( 'days' => 20131218 ) ,
    2 => Array ( 'days' => 20131219 ) ,
    3 => Array ( 'days' => 20131220 ) ,
    4 => Array ( 'days' => 20131221 ) ,
    5 => Array ( 'days' => 20131222 ) ,
    6 => Array ( 'days' => 20131223 ) ,
);
foreach ($result as $a){
   foreach ($a as $b){
       $c[]=$b;
   
   
   }
}

print_r($c);

?>

回答4:

function arrayChange($array){static $arr2=array();
foreach($array as $v){
if(is_array($v)){
arrayChange($v);
}else{
$arr2[]=$v;
}
}
$arr2=array_unique($arr2); //这条语句是去掉重复值的
return $arr2;
}
$arr=arrayChange($result);
var_dump($arr);
?>