Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我可以将SQL查询的结果转换为对象吗?_Php_Sql Server_Orm - Fatal编程技术网

Php 我可以将SQL查询的结果转换为对象吗?

Php 我可以将SQL查询的结果转换为对象吗?,php,sql-server,orm,Php,Sql Server,Orm,我从Sql server db获得了以下查询结果: 我需要从PHP获得这个json输出: {"Clientes": [{ "CodCliente": 1, "NombreCliente": "Garcia", "Direcciones":[ { "Direccion": "Av Uriburu 2569", "Telefono": "4558899" }, { "Dire

我从Sql server db获得了以下查询结果:

我需要从PHP获得这个json输出:

{"Clientes":
  [{
     "CodCliente": 1,
     "NombreCliente": "Garcia",
     "Direcciones":[ 
       {
        "Direccion": "Av Uriburu 2569",
        "Telefono": "4558899"
       },
       {
        "Direccion": "Pte Roca 1527",
        "Telefono": "4887541"
       }
      ]
    },
    {
     "CodCliente": 2,
     "NombreCliente": "Gonzales",
     "Direcciones":[ 
       {
        "Direccion": "Lamadrid 475",
        "Telefono": "4897425"
       }
      ]
    },
    {
     "CodCliente": 3,
     "NombreCliente": "Ferreyra",
     "Direcciones":[ 
       {
        "Direccion": "Eva Peron 253",
        "Telefono": "4689553"
       },
       {
        "Direccion": "Laprida 658",
        "Telefono": "4658963"
       }
      ]
    }
  ]
}  
我想我应该有Clientes类和Direcciones类,但我无法关联查询结果以获得json:

class Cliente
{
    public $CodCliente;   
    public $NombreCliente; 
    public $Direcciones;

    public function __construct(){}

}

class Direccion
{
    public $CodCliente;   
    public $Direccion; 
    public $Telefono;    
    public function __construct(){}  
}
这就是我获取查询的方式,我不确定是否正确:

    $pdo = Database::connect();
    $sentencia = $pdo->prepare($comando);    
    $sentencia->execute();
    if ($sentencia) {
        $resultado = $sentencia->fetchAll(PDO::FETCH_CLASS, "Cliente"); 
         return $resultado;            
    } 

返回数据时,只需使用json对其进行编码 例如echo json_encode$数据; 我们通常将其用于API

$data = '[{
        "CodCliente": "1",
        "NombreCliente": "Garcia",
        "Direccion": "Av Uriburu 2569",
        "Telefono": "4558899"
     }, {
        "CodCliente": "1",
        "NombreCliente": "Garcia",
        "Direccion": "Pte Roca 1527",
        "Telefono": "4887541"
     }, {
        "CodCliente": "2",
        "NombreCliente": "Gonzales",
        "Direccion": "Lamadrid 475",
        "Telefono": "4897425"
     }, {
        "CodCliente": "3",
        "NombreCliente": "Ferreyra",
        "Direccion": "Eva Peron 253",
        "Telefono": "4689553"
     }, {
        "CodCliente": "3",
        "NombreCliente": "Ferreyra",
        "Direccion": "Laprida 658",
        "Telefono": "4658963"
     }]';
     $array = [];
     $decodedData = json_decode($data, true);
     foreach ($decodedData as $key) {
        $tempArray[$key['CodCliente']]['CodCliente'] = $key['CodCliente'];
        $tempArray[$key['CodCliente']]['NombreCliente'] = $key['NombreCliente'];
        $tempDirec['Direccion'] = $key['Direccion'];
        $tempDirec['Telefono'] = $key['Telefono'];
        $tempArray[$key['CodCliente']]['Direcciones'][] = $tempDirec ;
        $array[$key['CodCliente']] = $tempArray[$key['CodCliente']];
     }
     $result = json_encode(array_values($array));
     echo "<pre>";
     echo print_r($result);
     echo "</pre>";die;
说明: CodCliente类型只需要一个对象,所以我们需要对剩余的内容进行分组,所以我所做的是

首先将数据解码为数组 创建一个$array,它可以将CodCliente作为键,这样我们就不会得到重复的CodCliente 在每次迭代时,如果$array update Direcciones中已经存在CodCliente密钥 因此,最后从$array中删除密钥并对其进行编码。
嗯,您必须手动处理数据库中的数据并创建新的数组,该数组将被适当地格式化。可能是这样的

$resultado = array(
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Sv Uriburu 2569","Telefono"=>"4558899"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Pte Roce 1527","Telefono"=>"4887541"),
            array("CodCliente"=>2,"NombreCliente"=>"Gonzales","Direccion"=>"Lambadrid 475","Telefono"=>"4897425"),
            array("CodCliente"=>3,"NombreCliente"=>"Ferreyra","Direccion"=>"Eva Peron 253","Telefono"=>"4689553"),
            array("CodCliente"=>3,"NombreCliente"=>"Ferreyra","Direccion"=>"Laprida 658","Telefono"=>"4658963")
        );

根据您使用的PHP和/或库的版本,代码可能会在许多方面得到改进,但这是另一回事。

SQL数据库是关系型的,这意味着结果集是每行的,这实际上意味着特别是当连接数增加时,您可以拥有十分之一,在对象表示中可能是具有嵌套对象的对象的数百行,有时甚至数千行。从另一方面来说,一个对象,特别是当它有嵌套对象时,更像是一个树表示。在您的例子中,SQL结果可能来自保存个人详细信息的表和保存地址详细信息的表之间的联接。 您正在寻找的是关系到对象的映射,有大量的资料需要研究,只需从这里开始

现在我可以试着根据我在开发类似算法方面的经验给你一个答案。 我们开始之前的一些关键点: 1.pdo支持在您已经编写的对象上进行映射,但结果始终是一个级别的对象—不显示嵌套对象,因此它无法为您工作

2.你必须记住表的关系来创建你的类,因此如果你在个人详细信息表和地址表之间有一对多的关系,那么这个关系必须以某种方式存在于你的类中。一个简单的方法是Cliente类拥有一个数组,该数组保存类Direccion的对象,就像我所看到的那样

3.您需要一个能够进行映射的算法。由于pdo使用$sentencia->fetchAllPDO::FETCH_ASSOC;算法需要知道何时映射类属性以及何时映射对象

下面是一种可以按原样使用的方法,请注意,这段代码可能需要更多的优化,对于大的结果,性能会下降。使用风险自负

1.如果更改为$resultado=$sentencia->fetchAllPDO::FETCH_ASSOC,则输入数据;应该是这样的数组

class Cliente{

    public $CodCliente;
    public $NombreCliente;
    public $Direcciones;

    public function getDirecciones(){
        if(!isset($this->Direcciones)){
            return array(new Direccion());
        }
        return $this->Direcciones;
    }

    public function setDirecciones($Direcciones){
        $this->Direcciones = $Direcciones;
    }

    public function getCodCliente(){
        if(!isset($this->CodCliente)){
            return 0;
        }
        return intval($this->CodCliente); //ensure for the data type
    }

    public function setCodCliente($CodCliente){
        $this->CodCliente = $CodCliente;
    }

    public function getNombreCliente(){
        if(!isset($this->NombreCliente)){
            return "";
        }
        return $this->NombreCliente;
    }

    public function setNombreCliente($NombreCliente){
        $this->NombreCliente = $NombreCliente;
    }

    public function __construct(){}
}

class Direccion{
    public $Direccion;
    public $Telefono;

    public function getDireccion(){
        if(!isset($this->Direccion)){
            return 0;
        }
        return $this->Direccion; 
    }

    public function setDireccion($Direccion){
        $this->Direccion = $Direccion;
    }

    public function getTelefono(){
        if(!isset($this->Telefono)){
            return "";
        }
        return $this->Telefono;
    }

    public function setTelefono($Telefono){
        $this->Telefono = $Telefono;
    }

    public function __construct(){}
}
2.你需要改变你的课程如下

$mapper = new rbe_mapper();
$result = array();
for($i=0;$i<count($resultado);$i++){
    $client = new Cliente();
    $client = $mapper->mapArrayToObjectByProperties($resultado[$i],$client); //each iteration map a singe row from the result

    $index = getItemIndexForValueByMethod($result,$client->getCodCliente(),"getCodCliente");
    if($index===false){
        array_push($result,$client);
    }else{
        $direction = $client->getDirecciones();
        $directions = $result[$index]->getDirecciones();
        array_push($directions,$direction[0]);
        $result[$index]->setDirecciones($directions);
    }
}
3.加上这个

function getItemIndexForValueByMethod($array,$value,$method){
    if(!is_array($array)){
        return false;
    }
    if(empty($array)){
        return false;
    }

    for($i=0;$i<count($array);$i++){
        if($array[$i]->$method()==$value){
            return $i;
        }
    }

    return false;
}

class rbe_mapper{

    public function mapArrayToObjectByProperties($row,$object){
        $class = get_class($object);

        $properties = get_class_vars($class);
        $keys = array_keys($properties);
        $plength = count($keys);
        for($i=0;$i<$plength;$i++){
            $property = ucfirst($keys[$i]);
            $setter = "set".$property;
            $getter = "get".$property;
            if(method_exists($class,$setter) && method_exists($class,$getter)){
                if(is_object($object->$getter())){
                    $object->$setter($this->mapArrayToObjectByProperties($row,$object->$getter())); //for nested objects
                }elseif(is_array($object->$getter())){
                    $ar = $object->$getter();
                    $ar[0] = $this->mapArrayToObjectByProperties($row,new $ar[0]()); //for arrays
                    $object->$setter($ar);
                }else{//value setting fonr non of the avove data types
                    $value = false;
                    if(isset($row[$property])){
                        $value = $row[$property];
                    }

                    if($value!==false){
                        $object->$setter($value);
                    }
                }
            }


        }

        return $object;
    }
}
json中的结果是

$input = array(
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Sv Uriburu 2569","Telefono"=>"4558899","Email"=>"test@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Pte Roce 1527","Telefono"=>"4887541","Email"=>"test@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Sv Uriburu 2569","Telefono"=>"4558899","Email"=>"test_@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Pte Roce 1527","Telefono"=>"4887541","Email"=>"test_@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Sv Uriburu 2569","Telefono"=>"4558899","Email"=>"test@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Pte Roce 1527","Telefono"=>"4887541","Email"=>"test@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Sv Uriburu 2569","Telefono"=>"4558899","Email"=>"test_@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Pte Roce 1527","Telefono"=>"4887541","Email"=>"test_@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>2,"NombreCliente"=>"Gonzales","Direccion"=>"Lambadrid 475","Telefono"=>"4897425","Email"=>"test2@gmail.com","Brand"=>"","Model"=>""),
            array("CodCliente"=>3,"NombreCliente"=>"Ferreyra","Direccion"=>"Eva Peron 253","Telefono"=>"4689553","Email"=>"test3@gmail.com","Brand"=>"","Model"=>""),
            array("CodCliente"=>3,"NombreCliente"=>"Ferreyra","Direccion"=>"Laprida 658","Telefono"=>"4658963","Email"=>"test3@gmail.com","Brand"=>"","Model"=>"")
        );


class Cliente{
    public $CodCliente;
    public $NombreCliente;
    public $Direcciones;
    public $Emails;
    public $Car;

    public function getCar(){
        if(!isset($this->Car)){
            return new Car();
        }
        return $this->Car;
    }

    public function setCar($Car){
        $this->Car = $Car;
    }

    public function getEmails(){
        if(!isset($this->Emails)){
            return array(new Email());
        }
        return $this->Emails;
    }

    public function setEmails($Emails){
        $this->Emails = $Emails;
    }

    public function getDirecciones(){
        if(!isset($this->Direcciones)){
            return array(new Direccion());
        }
        return $this->Direcciones;
    }

    public function setDirecciones($Direcciones){
        $this->Direcciones = $Direcciones;
    }

    public function getCodCliente(){
        if(!isset($this->CodCliente)){
            return 0;
        }
        return intval($this->CodCliente); //ensure for the data type
    }

    public function setCodCliente($CodCliente){
        $this->CodCliente = $CodCliente;
    }

    public function getNombreCliente(){
        if(!isset($this->NombreCliente)){
            return "";
        }
        return $this->NombreCliente;
    }

    public function setNombreCliente($NombreCliente){
        $this->NombreCliente = $NombreCliente;
    }

    public function __construct(){}
}

class Direccion{
    public $Direccion;
    public $Telefono;

    public function getDireccion(){
        if(!isset($this->Direccion)){
            return 0;
        }
        return $this->Direccion; 
    }

    public function setDireccion($Direccion){
        $this->Direccion = $Direccion;
    }

    public function getTelefono(){
        if(!isset($this->Telefono)){
            return "";
        }
        return $this->Telefono;
    }

    public function setTelefono($Telefono){
        $this->Telefono = $Telefono;
    }

    public function __construct(){}
}

class Email{
    public $Email;

    public function getEmail(){
        if(!isset($this->Email)){
            return "";
        }
        return $this->Email;
    }

    public function setEmail($Email){
        $this->Email = $Email;
    }

    public function __construct(){}
}

class Car{
    public $Brand;
    public $Model;

    public function getBrand(){
        if(!isset($this->Brand)){
            return "";
        }
        return $this->Brand;
    }

    public function setBrand($Brand){
        $this->Brand = $Brand;
    }

    public function getModel(){
        if(!isset($this->Model)){
            return "";
        }
        return $this->Model;
    }

    public function setModel($Model){
        $this->Model = $Model;
    }

    public function __construct(){
    }
}

$mapper = new rbe_mapper();
$result = array();
for($i=0;$i<count($input);$i++){
    $client = new Cliente();
    $client = $mapper->mapArrayToObjectByProperties($input[$i],$client); //each iteration map a singe row from the result

    $index = getItemIndexForValueByMethod($result,$client->getCodCliente(),"getCodCliente");
    if($index===false){
        array_push($result,$client);
    }else{
        $direction = $client->getDirecciones();
        $directions = $result[$index]->getDirecciones();
        $_index = getItemIndexForValueByMethod($directions,$direction[0]->getDireccion(),"getDireccion");
        if($_index===false){
            array_push($directions,$direction[0]);
            $result[$index]->setDirecciones($directions);
        }

        $email = $client->getEmails();
        $emails = $result[$index]->getEmails();
        $_index = getItemIndexForValueByMethod($emails,$email[0]->getEmail(),"getEmail");
        if($_index===false){
            array_push($emails,$email[0]);
            $result[$index]->setEmails($emails);
        }
    }
}

echo json_encode($result);
请注意,getItemIndexForValueByMethod函数和带有MapArrayTobObjectByProperties方法的rbe_映射器类通常适用于大多数类似情况,您可以尝试使用其他表和联接。只要使用setter/getter方法和嵌套对象创建类,嵌套对象的示例就可以轻松完成,或者数组位于顶级类中,这段代码就可以工作。如果你需要更多的澄清或解释,请告诉我

解释 这一切背后的一个重要思想是拥有一个抽象方法MapArrayTobObjectByProperties,它将嵌套数组映射到对象pdo获取结果,映射的指南是对象本身,这就是类需要修改的原因。您仍然需要遍历pdo将返回的数组的每一行,这就是为什么我说可能会出现性能下降的原因。通常,mapArrayToObjectByProperties方法从数组映射一行,结果是一个对象。由于mapArrayToObjectByProperties方法的每个结果都是一个对象,因此您需要检查该对象是否已在数组中,或者是否已添加到对象的额外地址、电子邮件等中。此检查由getItemIndexForValueByMethod执行

我对你的例子做了一些补充,以证明你可以多么容易地扩展这个理论。在我的示例中,每个用户都可以有多封电子邮件和一辆汽车。这意味着类Cliente有一个额外的email对象数组和一个名为car的嵌套对象


你能展示一下你目前用来执行查询并返回结果的PHP代码吗?你使用的是什么版本的sql server r?sql 2008我得到了数据,我无法得到我想要的json这是我得到的,而不是我想要的:[{CodCliente:1,NombreCliente:Garcia,direcion:Av Uriburu 2569,Telefo
号码:4558899},{Codclient:1,Nombreclient:Garcia,Direccion:Pte Roca 1527,Telefono:4887541},{Codclient:2,Nombreclient:Gonzales,Direccion:Lamadrid 475,Telefono:4897425},{Codclient:3,Nombreclient:Ferreyra,Direccion:Eva Peron 253,Telefono:4689553},{Codclient:3,Nombreccion:Lappore:Ferrida,谢谢我的帮助。我有很多数据,我认为这种方式不会很好,但我理解这个想法。你认为和ORM合作能帮我吗?那要看情况了。如果你想让你的项目有类结构,那么你需要这些方法,比如ORM等等,如果你只想让你的数据输出为json,那么你不需要对象,只要从结果中获取数组并将其转换为json即可。我可以稍后发布一个小函数。我在我的CRM或其他项目中使用了这样的算法,我开发的对象比你的更复杂,而且工作很好。我正在开发一个api rest,我将有几个这样的查询。如果你有一些github,我可以去那里获取想法,那就太好了。再次感谢你们!不幸的是,我没有git中心。为了更好地理解这个想法,我对我的答案做了一些修改。说实话,因为您正在开发一个API,而不是某种应用程序MVC或类似的体系结构,我不确定是否有必要进入类/对象,或者只是对结果PDO数组进行一些转换并直接抛出json。ORM会优化这个过程吗?在这种情况下,我使用PHP5.3ORM或SQL并不能真正帮助您。当您需要导出某些特定的JSON格式时,始终需要手动重新组织数据。但我不认为这太复杂。您还可以尝试使用一些非SQL数据库,比如MongoDB,这将帮助您以类似于JSON的方式组织数据。没有任何课程。如果你有任何关于如何用图书馆或其他东西来改进它的建议,请让我知道。我绝对推荐升级PHP到5.6。甚至7.1分支。有这么多新的特性、函数和语法,比如SQL和我推荐的雄辩。您可以将其用作独立的库,也可以尝试整个框架。
//class definition here sample 2,4
$pdo = Database::connect();
    $sentencia = $pdo->prepare($comando);    
    $sentencia->execute();
    if ($sentencia) {
        $resultado = $sentencia->fetchAll(PDO::FETCH_ASSOC); 
//add here sample 3
         return json_encode($result);            
    } 
[{
        "CodCliente": 1,
        "NombreCliente": "Garcia",
        "Direcciones": [{
                "Direccion": "Sv Uriburu 2569",
                "Telefono": "4558899"
            }, {
                "Direccion": "Pte Roce 1527",
                "Telefono": "4887541"
            }
        ]
    }, {
        "CodCliente": 2,
        "NombreCliente": "Gonzales",
        "Direcciones": [{
                "Direccion": "Lambadrid 475",
                "Telefono": "4897425"
            }
        ]
    }, {
        "CodCliente": 3,
        "NombreCliente": "Ferreyra",
        "Direcciones": [{
                "Direccion": "Eva Peron 253",
                "Telefono": "4689553"
            }, {
                "Direccion": "Laprida 658",
                "Telefono": "4658963"
            }
        ]
    }
]
$input = array(
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Sv Uriburu 2569","Telefono"=>"4558899","Email"=>"test@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Pte Roce 1527","Telefono"=>"4887541","Email"=>"test@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Sv Uriburu 2569","Telefono"=>"4558899","Email"=>"test_@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Pte Roce 1527","Telefono"=>"4887541","Email"=>"test_@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Sv Uriburu 2569","Telefono"=>"4558899","Email"=>"test@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Pte Roce 1527","Telefono"=>"4887541","Email"=>"test@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Sv Uriburu 2569","Telefono"=>"4558899","Email"=>"test_@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>1,"NombreCliente"=>"Garcia","Direccion"=>"Pte Roce 1527","Telefono"=>"4887541","Email"=>"test_@gmail.com","Brand"=>"X brand","Model"=>"Y Model"),
            array("CodCliente"=>2,"NombreCliente"=>"Gonzales","Direccion"=>"Lambadrid 475","Telefono"=>"4897425","Email"=>"test2@gmail.com","Brand"=>"","Model"=>""),
            array("CodCliente"=>3,"NombreCliente"=>"Ferreyra","Direccion"=>"Eva Peron 253","Telefono"=>"4689553","Email"=>"test3@gmail.com","Brand"=>"","Model"=>""),
            array("CodCliente"=>3,"NombreCliente"=>"Ferreyra","Direccion"=>"Laprida 658","Telefono"=>"4658963","Email"=>"test3@gmail.com","Brand"=>"","Model"=>"")
        );


class Cliente{
    public $CodCliente;
    public $NombreCliente;
    public $Direcciones;
    public $Emails;
    public $Car;

    public function getCar(){
        if(!isset($this->Car)){
            return new Car();
        }
        return $this->Car;
    }

    public function setCar($Car){
        $this->Car = $Car;
    }

    public function getEmails(){
        if(!isset($this->Emails)){
            return array(new Email());
        }
        return $this->Emails;
    }

    public function setEmails($Emails){
        $this->Emails = $Emails;
    }

    public function getDirecciones(){
        if(!isset($this->Direcciones)){
            return array(new Direccion());
        }
        return $this->Direcciones;
    }

    public function setDirecciones($Direcciones){
        $this->Direcciones = $Direcciones;
    }

    public function getCodCliente(){
        if(!isset($this->CodCliente)){
            return 0;
        }
        return intval($this->CodCliente); //ensure for the data type
    }

    public function setCodCliente($CodCliente){
        $this->CodCliente = $CodCliente;
    }

    public function getNombreCliente(){
        if(!isset($this->NombreCliente)){
            return "";
        }
        return $this->NombreCliente;
    }

    public function setNombreCliente($NombreCliente){
        $this->NombreCliente = $NombreCliente;
    }

    public function __construct(){}
}

class Direccion{
    public $Direccion;
    public $Telefono;

    public function getDireccion(){
        if(!isset($this->Direccion)){
            return 0;
        }
        return $this->Direccion; 
    }

    public function setDireccion($Direccion){
        $this->Direccion = $Direccion;
    }

    public function getTelefono(){
        if(!isset($this->Telefono)){
            return "";
        }
        return $this->Telefono;
    }

    public function setTelefono($Telefono){
        $this->Telefono = $Telefono;
    }

    public function __construct(){}
}

class Email{
    public $Email;

    public function getEmail(){
        if(!isset($this->Email)){
            return "";
        }
        return $this->Email;
    }

    public function setEmail($Email){
        $this->Email = $Email;
    }

    public function __construct(){}
}

class Car{
    public $Brand;
    public $Model;

    public function getBrand(){
        if(!isset($this->Brand)){
            return "";
        }
        return $this->Brand;
    }

    public function setBrand($Brand){
        $this->Brand = $Brand;
    }

    public function getModel(){
        if(!isset($this->Model)){
            return "";
        }
        return $this->Model;
    }

    public function setModel($Model){
        $this->Model = $Model;
    }

    public function __construct(){
    }
}

$mapper = new rbe_mapper();
$result = array();
for($i=0;$i<count($input);$i++){
    $client = new Cliente();
    $client = $mapper->mapArrayToObjectByProperties($input[$i],$client); //each iteration map a singe row from the result

    $index = getItemIndexForValueByMethod($result,$client->getCodCliente(),"getCodCliente");
    if($index===false){
        array_push($result,$client);
    }else{
        $direction = $client->getDirecciones();
        $directions = $result[$index]->getDirecciones();
        $_index = getItemIndexForValueByMethod($directions,$direction[0]->getDireccion(),"getDireccion");
        if($_index===false){
            array_push($directions,$direction[0]);
            $result[$index]->setDirecciones($directions);
        }

        $email = $client->getEmails();
        $emails = $result[$index]->getEmails();
        $_index = getItemIndexForValueByMethod($emails,$email[0]->getEmail(),"getEmail");
        if($_index===false){
            array_push($emails,$email[0]);
            $result[$index]->setEmails($emails);
        }
    }
}

echo json_encode($result);