Home > front end >  Array to string conversion error but I'm not even converting anything
Array to string conversion error but I'm not even converting anything

Time:01-31

I wrote a simple code that takes strings from an array and adds to a string.It keeps giving me error in one line,but I'm doing the same thing in the lines before and it did not give me error for those.

<?php

class database{

    public $into="";

    public function into(Array $array,$flag){
        $a=0;
        $string='';

        foreach($array as $key=>$value){
            if($flag==1){
                if($a==0){
                    $string.=" (".$key.",";
                }elseif($a<(count($array)-1)){
                    $string.=$key.",";
                }
                else{
                    $string.=$key.") VALUES";
                }
            }
            if($flag==2){
                if($a==0){
                    $string.=" ('".$value."',";
                }
                elseif($a<count($array)-1){
                    $string.="'".$value."',";
                }
                else{
                    $string.="'".$value."')";
                }
            }
            $a  ;
        }
        $this->into.=$string;
    }

}

$db = new database;
$array=array(["title"=>"8","rank"=>"99"]);
$db->into($array,1);
$db->into($array,2);
echo $db->into;
?>

error is:

( ! ) Notice: Array to string conversion in C:\wamp64\www\class-file-advance\connect.php on line 41

I tried a few simple things like removing the 'Array' modifier,changing the way I call the method,and I tried changing the $string with $this->into ,but it didn't work.

My professor coded this and it worked just fine on his computer,yet I tried it on MY system and it didn't work:

<?php
class connect{
    public $connect;
    public function __construct(){
        try{
            $this->connect=new PDO("mysql:host=localhost;dbname=project","root",'');
            $this->connect->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        }catch(PDOException $error){
            echo "connection failed:".$error->getmessage();
        }
    }
}

class database extends connect{
    public $table;
    public $where="";
    public $order="";
    public $limit="";
    public $into="";

    public function where(Array $array){
        $temp=0;

        foreach($array as $key=>$value){
            if($temp==0){
             $this->where .= " WHERE ".$key."='".$value."' AND ";
            }
            elseif($temp < count($array)-1){
              $this->where .= $key."='".$value."' AND ";}
            else{
              $this->where .=$key."='".$value."'";}

            $temp  ;
        }
        
    }
}
   
$db = new database;
$array=array(["title"=>"8","rank"=>"99"]);
$db->where($array);
var_dump($db->select());

?>

CodePudding user response:

$array=array(["title"=>"8","rank"=>"99"]);

This will create an array containing another array. So foreach($array as $key=>$value){ will populate $key with the value 0 and $value with the array ["title"=>"8","rank"=>"99"].

Your code expects $key to be "title" and "rank" and $value to be "8" and "99".

For that, you need to remove the nesting from $array. Either remove the outer array() or the inner []:

$array=["title"=>"8","rank"=>"99"];
// or:
$array=array("title"=>"8","rank"=>"99");
  •  Tags:  
  • Related