douque8855 2016-01-20 19:26
浏览 45
已采纳

PHP - 向关联数组添加新的“键值”

I'm trying almost everything to add a new key and its value to a result query.

    $consulta = "SELECT p.id_empleado,count(p.id_empleado) as pendientes,e.nombre,e.apellidos FROM partidas_empleados p ";
    $consulta.= "inner join empleados e on p.id_empleado=e.id_empleado ";
    $consulta.= "WHERE abierta=TRUE group by id_empleado";

    $sql = $con->prepare($consulta);
    $ok = $sql->execute();
    $query = $sql->fetchAll(PDO::FETCH_ASSOC);

    for($i=0;$i<count($query);$i++){
        //echo "hola";
        $fila = $query[$i];
        $consulta = "SELECT id_partida FROM partidas_empleados where id_empleado=? ";
        $sql = $con->prepare($consulta);
        $ok = $sql->execute(array($fila['id_empleado']));
        $sub_query = $sql->fetchAll(PDO::FETCH_ASSOC);
        //echo $sub_query;
        //$fila[]= array("lista_partidas"=>$sub_query);
        $fila['lista_partidas']= $sub_query;
    }

$sub_query is just a list of asociative arrays

I'm trying to add $sub_query to $query with lista_partidas as a key.

  • 写回答

1条回答 默认 最新

  • dongtangxi1584 2016-01-21 07:08
    关注

    As @kunruh and @jeroen said I was creating a copy of and modifying that copy.

        $consulta = "SELECT p.id_empleado,count(p.id_empleado) as pendientes,e.nombre,e.apellidos FROM partidas_empleados p ";
        $consulta.= "inner join empleados e on p.id_empleado=e.id_empleado ";
        $consulta.= "WHERE abierta=TRUE group by id_empleado";
    
        $sql = $con->prepare($consulta);
        $ok = $sql->execute();
        $query = $sql->fetchAll(PDO::FETCH_ASSOC);
    
        for($i=0;$i<count($query);$i++){
            $consulta = "SELECT id_partida FROM partidas_empleados where id_empleado=? ";
            $sql = $con->prepare($consulta);
            $ok = $sql->execute(array($fila['id_empleado']));
            $sub_query = $sql->fetchAll(PDO::FETCH_ASSOC);
            $query[$i]['lista_partidas']= $sub_query;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?