0

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/ ).

neubert
  • 15,947
  • 24
  • 120
  • 212
Wojtek
  • 51
  • 1
  • 2
  • 8
  • So you want to convert an array containing 1,3,3 and 4 to an array containing 1,2,3 and 4? Where does the different value come from? – Mark Baker Feb 18 '18 at 21:25
  • Do you simply want to sort the values? And mistyped 2 as 3? – Mark Baker Feb 18 '18 at 21:33
  • Mark, I dont need sorting. I cant provide correct data to the library. Take a look at the code: https://www.paste.org/91101#code – Wojtek Feb 18 '18 at 23:09
  • Then explain how one of the 3 values is transformed to a 2 value.... what is the logic behind that? Otherwise I'd suggest `$x = array_chunk($data,1);` – Mark Baker Feb 18 '18 at 23:12
  • You have right, this is examples values. Correct is: $data = Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) – Wojtek Feb 18 '18 at 23:15

3 Answers3

0

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]);
}
MatejG
  • 1,393
  • 1
  • 17
  • 26
  • Thank you for your answer. I checked your solution but unfortunately it does not work. Currently, the code looks like this: https://www.paste.org/91101#code – Wojtek Feb 18 '18 at 23:06
0
$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?*

    $array = array( 0 => [1], 1 => [2] , 2 => [3], 3 => [3] );

    $x = json_encode($array, JSON_HEX_APOS);

    echo $x;

[[1],[2],[3],[3]]

Richard
  • 325
  • 7
  • 23
  • Thank you for your answer. I checked your solution but unfortunately it does not work. Currently, the code looks like this: https://www.paste.org/91101#code – Wojtek Feb 18 '18 at 23:06
  • Try again? I updated the code @Wojtek - `json_encode($array, JSON_HEX_APOS);` – Richard Feb 19 '18 at 07:47
  • Thank you, but it doesnt work :( . I paste a simplified code that works correctly with numbers but not with an array: https://www.paste.org/91112 Do you have an idea how to solve it? – Wojtek Feb 19 '18 at 08:41
  • @Wojtek take a look at https://www.w3schools.com/php/func_misc_eval.asp `$output = eval("$x");"` or something – Richard Mar 04 '18 at 21:25
0
$data = array(1,4,3,3);

$x = '[['.implode('], [', $data).']]';

echo $x;
  • Thank you for your answer. I checked your solution but unfortunately it does not work. Currently, the code looks like this: https://www.paste.org/91101#code – Wojtek Feb 18 '18 at 23:07