[英]Convert array to another character [php]
I have data in the form:我有以下形式的数据:
$data = Array ( [0] => 1 [1] => 4 [2] => 3 [3] => 3 )
I want to convert it to:我想将其转换为:
$x = [[1], [2], [3], [4]];
I do not know how to do this?我不知道该怎么做?
I'm using the library PHP-ML ( http://php-ml.readthedocs.io/en/latest/machine-learning/regression/least-squares/ ).我正在使用 PHP-ML 库( http://php-ml.readthedocs.io/en/latest/machine-learning/regression/least-squares/ )。
If you want to create array from values, you can do it this way:如果你想从值创建数组,你可以这样做:
$x = [];
foreach($data as $key => $value) {
array_push($x, $value);
}
If you want to create array of arrays from values you can edit it like this:如果要从值创建数组数组,可以像这样编辑它:
$x = [];
foreach($data as $key => $value) {
array_push($x, [$value]);
}
$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;
Like so?像这样?
But why change array to array?但是为什么要将数组更改为数组呢?
Ahhh I see, You want it in a json format?*啊哈,我明白了,您想要 json 格式的文件吗?*
$array = array( 0 => [1], 1 => [2] , 2 => [3], 3 => [3] ); $array = array( 0 => [1], 1 => [2] , 2 => [3], 3 => [3] );
$x = json_encode($array, JSON_HEX_APOS); $x = json_encode($array, JSON_HEX_APOS);
echo $x;回声 $x;
[[1],[2],[3],[3]] [[1],[2],[3],[3]]
$data = array(1,4,3,3);
$x = '[['.implode('], [', $data).']]';
echo $x;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.