-1

where in php-ml looping variable is always error array_unshift like this:

Fatal error: Uncaught TypeError: array_unshift() expects parameter 1 to be array, string given in C:\xampp\htdocs\prediksi\assets\main system\vendor\php-ai\php-ml\src\Phpml\Regression\LeastSquares.php:100 Stack trace: #0 C:\xampp\htdocs\prediksi\assets\main system\vendor\php-ai\php-ml\src\Phpml\Regression\LeastSquares.php(100): array_unshift('[2018, 1, 1], [...', 1) #1 C:\xampp\htdocs\prediksi\assets\main system\vendor\php-ai\php-ml\src\Phpml\Regression\LeastSquares.php(81): Phpml\Regression\LeastSquares->getSamplesMatrix() #2 C:\xampp\htdocs\prediksi\assets\main system\vendor\php-ai\php-ml\src\Phpml\Regression\LeastSquares.php(42): Phpml\Regression\LeastSquares->computeCoefficients() #3 C:\xampp\htdocs\prediksi\admin\modul\show_predict.php(40): Phpml\Regression\LeastSquares->train(Array, Array) #4 C:\xampp\htdocs\prediksi\admin\index.php(85): require('C:\\xampp\\htdocs...') #5 {main} thrown in C:\xampp\htdocs\prediksi\assets\main system\vendor\php-ai\php-ml\src\Phpml\Regression\LeastSquares.php on line 100

why this happen?

there is my code to looping :

$query1 = "SELECT * FROM master_data";

$result1 = mysqli_query($conn, $query1);

$data1 = "";

while($row1 = mysqli_fetch_array($result1)){
    $TH=number_clean($row1['TH']);
    $B=$row1['B'];

$data1 .= "[".$row1["T"].", ".$B.", ".$TH."], ";

}

$data1 = substr($data1, 0, -2);

$query2 = "SELECT * from master_data";

$result2 = mysqli_query($conn, $query2);

$data2 = "";

while($row2 = mysqli_fetch_array($result2)){

  $data2 .= "".$row2["Penjualan"].", ";

}

$data2 = substr($data2, 0, -2); 

$tgl=$_POST['tgl'];
$dat1=$_POST['data1'];
$dat2=$_POST['data2'];

$samples = [$dat1];
$targets = [$dat2];

$regression = new LeastSquares();
$regression->train($samples, $targets);
$result = round($regression->predict([$t, $b, $th]));
echo $t."<br>";
echo $b."<br>";
echo $th."<br>";
echo $result."<br>";

and the result is in up of my question

when i use manual (insert one by one) it works

why this happen ?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • The reason is right there in the error: you are feeding the array_unshift function a string, when it requires an array – fraggley May 30 '20 at 12:25
  • what i must do to clear an error? – ridwan ariana May 30 '20 at 12:27
  • looping in array or what? im on confuse about this – ridwan ariana May 30 '20 at 12:28
  • Post the code where you are calling array_unshift() – fraggley May 30 '20 at 12:29
  • private function getSamplesMatrix() { $samples = []; foreach ($this->samples as $sample) { array_unshift($sample, 1); $samples[] = $sample; } return new Matrix($samples); } – ridwan ariana May 30 '20 at 12:31
  • 1
    Think, before you post. Initially you are showing code that doesn't include the source of the error message. Then you post additional code as an answer instead of editing your original post (REMOVE that answer before more people downvote it), and now you add additional code as a comment instead of adding it to your original post. You are making it hard for people to help you. Also, take a look at how to add strings to an array, and how to turn the array into a string afterwards. It will make your code easier to read and prevents you from having to remove the last comma. – RST May 30 '20 at 12:38

1 Answers1

0

The issue is when you are calling array_unshift you are passing a string, not a variable. Looking at your function you should change it to:

private function getSamplesMatrix() { 
    $samples = []; 
    array_unshift($samples, 1); 
    return new Matrix($samples); 
} 

Remove the foreach loop.

However I'm confused by your code as you are performing array_unshift on an empty array. You can achieve the same thing with:

$samples = array(1);
fraggley
  • 1,215
  • 2
  • 9
  • 19