【问题标题】:Get an array of arrays in PHP with a loop使用循环在 PHP 中获取数组数组
【发布时间】:2019-03-26 12:42:20
【问题描述】:

我正在开发 PHP 5.6 中的 Symfony 3.4。

这是我的问题的背景:

我有一个包含几行的“信息”表。我想对这些行进行排序并根据名为“Zone”的特定列显示它们。所以目标是拥有例如

“区域 1” * 该区域对应的信息行" “2区” * 该区域对应的信息行" ...

我在Repository中实现了我的功能,我也在Twig下做了布局。一切正常。我所要做的就是优化控制器上的代码以使其更简洁。

因此我的想法是:

  1. 通过查询检索包含所有现有不同区域的数组。
  2. 在此数组上循环,使用数组的每个值作为 SQL 查询的参数,该查询检索与传递的字段对应的行并在数组变量中检索它们
  3. 制作每个区域的数组的array_push(),在另一个数组中包含每个区域的数组。

但我不能。这是我的代码

存储库:

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,
        ));
    }

我有这个错误:

执行 'SELECT i0_.id AS id_0, i0_.contenu AS contenu_1, i0_.updated_at AS updated_at_2, i0_.zone AS zone_3, i0_.titre AS titre_4, i0_.criticite AS critical_5 FROM information i0_ WHERE i0_ 时发生异常.zone = ? ORDER BY i0_.criticite DESC' 带参数 ["Zone 1"]:

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

【问题讨论】:

  • 在循环中转储$value参数并查看它的实际值。如果它只返回一个字符串,如Zone 1.
  • 使用 $zone = $this->getDoctrine()->getRepository('PagesBundle:Information')->getInformationsZone($value);我有:array:1 [▼ "zone" => "Zone 1" ]
  • 没关系,我将 ($value) 更新为 ($value['zone']。它的工作,谢谢!

标签: php arrays loops symfony


【解决方案1】:

替换:

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

有了这个:

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

您将所有区域实体传递给getInformationsZone 方法。

所以要获取区域的标题,必须调用区域的getter。

$value$value->getZone();


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

【讨论】:

  • 我更新了我的代码。但我有这个错误:错误:调用数组上的成员函数getZone()我的getZonesActives()函数返回一个字符串数组,而不是一个对象数组:/
  • @AgrioFR 更新了代码。只看最后一行。
猜你喜欢
  • 2017-08-15
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
相关资源
最近更新 更多