我在表单中有数据:
$data = Array ( [0] => 1 [1] => 4 [2] => 3 [3] => 3 )我想将它转换为:
$x = [[1], [2], [3], [4]];我不知道该怎么做?
我使用的是PHP-ML库( http://php-ml.readthedocs.io/en/latest/machine-learning/regression/least-squares/ )。
发布于 2018-02-19 05:27:22
如果你想通过值创建数组,你可以这样做:
$x = [];
foreach($data as $key => $value) {
array_push($x, $value);
}如果你想通过值创建数组数组,你可以像这样编辑它:
$x = [];
foreach($data as $key => $value) {
array_push($x, [$value]);
}发布于 2018-02-19 05:30:56
$data = array(0 => "0", 1 => "1", 2 => "2", 3 => "3");
$output;
$halt = count($data) - 1;
for($i = 0; $i < count($data); $i++){
if($i==$halt){
$output.="[".$data[$i]."]";
}else{
$output.="[".$data[$i]."]".", ";
}
}
$x = "[".$output."]";
echo $x;像这样吗?
但是为什么要将数组改为数组呢?
$array =数组(0 => 1,1 => 2,2 => 3,3 => 3 );
$x = json_encode($array,JSON_HEX_APOS);
echo $x;
[1,2,3,3]
发布于 2018-02-19 05:39:40
$data = array(1,4,3,3);
$x = '[['.implode('], [', $data).']]';
echo $x;https://stackoverflow.com/questions/48856724
复制相似问题