dongpa6867 2018-04-19 18:13
浏览 152
已采纳

使用CachingIterator将PHP数组转换为字符串转换通知

I have done some searching on Stackoverflow already on this topic, however as far as I can tell, I am not treating an array as a string ?

The message I am getting is :

Array to string conversion in X on line 42

Line 42 of my code is the opening of a foreach loop :

foreach ($collection as $element) {

Variable $collection is a caching iterator that is based on database output:

$collection=new \CachingIterator(new \ArrayIterator($this->dbData));

If I print_r() on $this->dbData, I certainly get an array:

Array
(
    [0] => Array
        (
            [c_id] => A
 )

    [1] => Array
        (
            [c_id] => B
)

So, in summary:

  • We have a confirmed output of an array
  • We know that ArrayIterator() expects an array as argument
  • We know that CachingIterator() expects an Iterator as argument
  • We know that foreach() can loop over an Iterator

TL;DR I am really not sure what I am treating as string here ?

EDIT TO ADD....

Even if I greatly simplify my code, I can still reproduce:

<?php
error_reporting (E_ALL | E_STRICT);
ini_set ('display_errors', 1);
$arrX=array(array("c_id"=>"A"),array("c_id"=>"B"));
$collection=new \CachingIterator(new \ArrayIterator($arrX));
foreach($collection as $element) {
echo $element["c_id"].PHP_EOL;
}

Notice: Array to string conversion in /Users/bp/tmp/test.php on line 6

A

Notice: Array to string conversion in /Users/bp/tmp/test.php on line 6

B

  • 写回答

2条回答 默认 最新

  • dongpei2835 2018-04-19 19:03
    关注

    The short answer is, you're inadvertently asking the CachingIterator to convert the sub-arrays to strings during iteration. To not do this, don't use the CachingIterator::CALL_TOSTRING or CachingIterator::TOSTRING_USE_INNER flags.

    You can set no flags, by using 0 as the value for the $flags parameter, or use a different flag: this can be done in the constructor, or after initialisation by using CachingIterator::setFlags().

    For example:

    $array = [["c_id" => "A"], ["c_id" => "B"]];
    $collection = new CachingIterator(new ArrayIterator($array), 0);
    foreach ($collection as $element) {
        // no E_NOTICE messages, yay!
    }
    

    And a few words by way of explanation...

    By default, the CachingIterator class sets the CachingIterator::CALL_TOSTRING flag as noted in the PHP manual page on CachingIterator.

    public __construct ( Iterator $iterator [, int $flags = self::CALL_TOSTRING ] )
    

    When this flag (or the CachingIterator::TOSTRING_USE_INNER flag) is set, and the CachingIterator::next() method is called (i.e. during iteration) the current value (in this case each sub-array) or the inner iterator (in this case, the ArrayIterator), respectively, is converted to a string and saved internally. This string value is what is returned from CachingIterator::__toString() when one of those flags is used.

    When using any of the other flags, the above is not done when calling CachingIterator::next().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥150 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装