Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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中的数组数组_Php_Arrays_Loops_Symfony - Fatal编程技术网

使用循环获取PHP中的数组数组

使用循环获取PHP中的数组数组,php,arrays,loops,symfony,Php,Arrays,Loops,Symfony,我正在PHP5.6中开发Symfony3.4 以下是我问题的背景: 我有一个包含几行的信息表。我想对这些行进行排序,并根据称为Zone的特定列显示它们。因此,我们的目标是,例如 1区 *与此区域对应的信息行 2区 *与此区域对应的信息行 我在存储库中实现了我的功能,并在Twig下进行了布局。一切顺利。我所要做的就是优化控制器上的代码,使其更干净 因此,我的想法是: 通过查询检索包含所有现有不同区域的数组。 使用数组的每个值作为SQL查询的参数在此数组上循环,该SQL查询检索与传递的字段对应的行

我正在PHP5.6中开发Symfony3.4

以下是我问题的背景:

我有一个包含几行的信息表。我想对这些行进行排序,并根据称为Zone的特定列显示它们。因此,我们的目标是,例如

1区 *与此区域对应的信息行 2区 *与此区域对应的信息行

我在存储库中实现了我的功能,并在Twig下进行了布局。一切顺利。我所要做的就是优化控制器上的代码,使其更干净

因此,我的想法是:

通过查询检索包含所有现有不同区域的数组。 使用数组的每个值作为SQL查询的参数在此数组上循环,该SQL查询检索与传递的字段对应的行,并在数组变量中检索它们 在包含每个区域的数组的另一个数组中,对每个区域的数组进行数组\u推送。 但我不能。这是我的密码

存储库:

public function getInformationsZone($zone)
    {
        $queryBuilder = $this->createQueryBuilder("i")
        ->where("i.zone = :zone")
        ->orderBy("i.updatedAt","DESC")
        ->orderBy("i.criticite","DESC")
        ->setParameter('zone',$zone);

        return $queryBuilder->getQuery()->getResult();
    }

    public function getZonesActives()
    {
        $queryBuilder = $this->createQueryBuilder("i")
        ->select("i.zone")
        ->distinct(true);

        return $queryBuilder->getQuery()->getResult();
    }
控制器

/**
     * Lists all information entities.
     *
     * @Route("/", name="informations_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $information = $em->getRepository('PagesBundle:Information')->findAll();


        $listZones = $this->getDoctrine()->getRepository('PagesBundle:Information')->getZonesActives();
        $tabInfos = array();

        foreach($listZones as $key=>$value)
        {
            $zone = $this->getDoctrine()->getRepository('PagesBundle:Information')->getInformationsZone($value);
            array_push($tabInfos,$zone);
        }



// This is what i did just before

      /*  $infosZone1 = $this->getDoctrine()->getRepository('PagesBundle:Information')->getInformationsZone("Zone 1");
        $infosZone2 = $this->getDoctrine()->getRepository('PagesBundle:Information')->getInformationsZone("Zone 2");
        $infosZone3 = $this->getDoctrine()->getRepository('PagesBundle:Information')->getInformationsZone("Zone 3");

        $tabInfos = array($infosZone1,$infosZone2,$infosZone3);*/

        return $this->render('information/index.html.twig', array(
           /* 'information' => $information,
            'infosZone1'=> $infosZone1,
            'infosZone2'=> $infosZone2,
            'infosZone3'=> $infosZone3,*/
            'tabInfos'=>$tabInfos,
        ));
    }

我发现了这个错误:

执行“从信息i0中选择i0_.id作为id_0,i0_.contenu作为contenu_1,i0_.updated作为updated_在_2,i0_.zone作为zone_3,i0_.titre作为titre_4,i0_.criticite作为criticite_5”时发生异常,其中i0_.zone=?带参数的“i0.criticite DESC”订单[区域1]:

SQLSTATE[HY093]:无效参数编号:未定义参数

$zone = $this->getDoctrine()->getRepository('PagesBundle:Information')->getInformationsZone($value);
为此:

$zone = $this
    ->getDoctrine()
    ->getRepository('PagesBundle:Information')
    ->getInformationsZone($value->getZone());
您正在将all zone实体传递给getInformationsZone方法

因此,要获得区域的标题,必须调用区域的getter

$value到$value->getZone


编辑:所以,只需将$value->getZone更改为$value['zone']

循环中Dump$value参数可能重复,请查看其实际值。如果它只返回一个字符串,比如1.Using$zone=$this->getdoctor->getRepository'PagesBundle:Information'->getInformationsZone$value;我有:数组:1[▼ zone=>zone 1]没关系,我用$value['zone']更新了$value。这是工作,谢谢!我更新了我的代码。但我有一个错误:错误:调用数组上的成员函数getZone My GetZoneActives函数返回字符串数组,而不是对象数组:/@AgarioFR更新了代码。看看最后一行。