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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化