【问题标题】:How to manipulate tables in php如何在php中操作表格
【发布时间】:2012-10-18 13:27:41
【问题描述】:

请给我以下代码:

foreach($global as $item) 
    {   
        $item_array[] = array
                        (
                            '0'  => value($this->elem[0], $item),
                            '1'   => value($this->elem[1], $item),
                            '2' => value($this->elem[2], $item),
                            '3'  => value($this->elem[3], $item),
                            '4' => value($this->elem[4], $item)
                        );
    }

我想将其简化为以下内容:

foreach($global as $item) 
    {       
        $i=0;
        array_push($item_array[], value($this->elem[$i], $item) );
        $i++; 
             }

这段代码给了我一个错误,因为“$item_array[]”对我的脚本来说是必需的。我能做什么?

注意:我是 PHP 的新手。谢谢。

【问题讨论】:

  • 在您的array_push 电话中删除[]。在向 PHP 数组添加元素(但不能合并)时,您可以使用 array_push、$array[]` 或 $array[index]
  • 什么错误给你?
  • 错误是:警告:array_push() 期望参数 1 为数组,给定为空
  • 当我删除 [] 时,我的脚本不起作用!
  • 你能serialize($global) 在这里添加内容吗……让我看看你在做什么

标签: php arrays list


【解决方案1】:

您需要删除 [] 并将 $i=0 放在 for 循环之外。

【讨论】:

    猜你喜欢
    • 2017-02-21
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    相关资源
    最近更新 更多