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

2024年12月02日 18:55
有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);
?>