【问题标题】:Acces to element of array [closed]访问数组元素[关闭]
【发布时间】:2016-07-02 00:50:41
【问题描述】:

我的问题是:

我正在尝试访问数组示例的元素:

info() {
'library_code' => "example"
'report_date_end' => "2016-03-15 12:49:42"
'report_date_start' =>"2016-03-15 12:49:42"
'isbn' => NULL
}

所以我在做:

$libraryXML     = $info['library_code'];

但我有一个错误:

注意:未定义索引:library_code

我如何获取这些信息?

额外信息:

public function getInfo(){
    $sql = "select library_code, report_date_end, report_date_start, concept from xml_reports where status = 0";

    $rsm = new ResultSetMapping();
    $rsm->addScalarResult('library_code', 'library_code');
    $rsm->addScalarResult('report_date_end', 'report_date_end');
    $rsm->addScalarResult('report_date_start', 'report_date_start');
    $rsm->addScalarResult('concept', 'isbn');
    $query = $this->_em->createNativeQuery($sql, $rsm);

    return $query->getResult();
}

$info               = $this->xmlReportRepo->getInfo();

谢谢!

【问题讨论】:

  • $info 变量的创建在哪里?
  • 信息是函数还是什么?
  • 如果 info 是一个数组,那么它应该被定义为 $info = array( 'library_code' => "example" 'report_date_end' => "2016-03-15 12:49:42" ' report_date_start' =>"2016-03-15 12:49:42" 'isbn' => NULL );
  • print_r($info); 看看你得到了什么?
  • @devpro 当我做 var_dump($info) 控制台显示错误。

标签: php symfony


【解决方案1】:

你定义错误的数组。

应该是:

array(
  key  => value,
  key2 => value2,
  key3 => value3,
  ...
)

发件人:PHP array manual

【讨论】:

  • 但我没有声明数组是函数getInfo的返回
  • 查看手册中的示例 #7。
  • 谢谢@AntonEpikhin
【解决方案2】:

正如 Anton Epikhin 所说:

你的数组定义错误,但你也可以使用 [] (php 5.4)

$info = [
'library_code' => "example"
'report_date_end' => "2016-03-15 12:49:42"
'report_date_start' =>"2016-03-15 12:49:42"
'isbn' => NULL
]

看起来你在做一些 js 代码而不是 php

【讨论】:

  • 谢谢 :) 发帖前我需要更加小心和审核
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多