代码之家  ›  专栏  ›  技术社区  ›  giorgio79

包含作为数组函数输入的php字符串变量

  •  0
  • giorgio79  · 技术社区  · 15 年前

    为什么这不起作用?

      $stringhaha ="     1 => General,
          2 => Business,
          3 => Entertainment,
          4 => Health,
          5 => Politics,
          6 => Sci/Tech,
          7 => Sports,
          8 => News";
    
    $all_categories = array($stringhaha);
    
    print_r($all_categories);
    

    (将给出一个包含1项的数组。)

    $all_categories = array(1 => General,
          2 => Business,
          3 => Entertainment,
          4 => Health,
          5 => Politics,
          6 => Sci/Tech,
          7 => Sports,
          8 => News);
    
    print_r($all_categories);
    
    3 回复  |  直到 15 年前
        1
  •  2
  •   Michael Robinson    15 年前

    所发生的事情正是您应该预料到的:您已经声明了一个包含一个字符串的数组。

    对于我们人类来说,字符串看起来像数组并不重要,PHP仅仅是PHP,并且不能神奇地检测到您希望它从字符串解析数组。

    乔治79,来见我 PHP Docs

        2
  •  2
  •   dwich    15 年前

    这叫做语言语法。你不能为所欲为。你必须说出它的设计语言。

    这也不行

    message = hello
    

    这是正确的

    $message = 'hello';
    

    每种语言都有规则,你必须尊重它们。祝你好运。

        3
  •  0
  •   Erik B    15 年前

    $all_categories = array(1 => "General",
        2 => "Business",
        3 => "Entertainment",
        4 => "Health",
        5 => "Politics",
        6 => "Sci/Tech",
        7 => "Sports",
        8 => "News");
    
    print_r($all_categories);
    

    你需要一个字符串数组,对吗?

    推荐文章