dongwu8064 2015-05-15 19:36
浏览 11

无法在另一个函数中打印数组的项目

I have an array like this:

function func1()
{
$web( 'news' => 'yes',
      'items' => '');
// I call `$web['item']` and fill it as array:
$web['item'][] = 'item1';
$web['item'][] = 'item2';
$web['item'][] = 'item3';
foreach($web['item'] as $item) echo $item,'-';
//result: item1 - item 2 - item 3 

func2($web)
}

Now everything sound good...but when I send $web to another function it just print.

function  func2($web)
{
  foreach($web['item'] as $item) echo $item,'-';
  //result: item1 - 
}

What is my wrong?

  • 写回答

1条回答 默认 最新

  • dtamho6404 2015-05-15 19:45
    关注

    You have issues with and arrays. To declare a new array you have some options. I like the shortest way, personally:

    $web = [];
    

    Put it all together with the corrections.

    function func1()
    {
        $web = [];
        $web['item'][] = 'item1';
        $web['item'][] = 'item2';
        $web['item'][] = 'item3';
        foreach($web['item'] as $item)
        {
            echo $item.'-';
        }
    
        echo "<br>";
        func2($web);
    }
    
    function func2($web)
    {
        foreach($web['item'] as $item)
        {
            echo $item.'-';
        }
    }
    

    I added an echo "<br>"; so results would appear on separate lines. I didn't include lines that weren't too useful for this example, you may add those back in.

    评论

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题