【问题标题】:Appending an Array and Printing php附加数组并打印 php
【发布时间】:2012-12-13 09:04:01
【问题描述】:

此代码不起作用,任何人都可以创建解决方案吗?

<?php
  //Assigns 10 to test1. 20 to test2. 30 to test3.
  $array1 = array(10 => "test1", 20 => "test2", 30 => "test3");  
  //Creates a blank array.
  $blank = new ArrayObject(array()); 

  $blank->append(array("20")); //Adds the value 20, to the blank array.
  $blank->append(array("30")); //Adds the value 30, to the blank array.

  print($array1[$blank[0]]); // should print "test2"
  print($array1[$blank[1]]); // should print "test3"
?>

【问题讨论】:

    标签: php arrays string printing append


    【解决方案1】:

    您在寻找:

    <?php
      //Assigns 10 to test1. 20 to test2. 30 to test3.
      $array1 = array(10 => "test1", 20 => "test2", 30 => "test3");  
      //Creates a blank array.
      $blank = new ArrayObject();
    
      $blank->append(array("20")); //Adds the value 20, to the blank array.
      $blank->append(array("30")); //Adds the value 30, to the blank array.
    
      print($array1[$blank[0][0]]); // should print "test2"
      print($array1[$blank[1][0]]); // should print "test3"
    ?>
    

    在这里,$blank 将是多维的。

    http://ideone.com/XsgnSz

    或者更有可能:

    <?php
      //Assigns 10 to test1. 20 to test2. 30 to test3.
      $array1 = array(10 => "test1", 20 => "test2", 30 => "test3");  
      //Creates a blank array.
      $blank = new ArrayObject();
    
      $blank->append("20"); //Adds the value 20, to the blank array.
      $blank->append("30"); //Adds the value 30, to the blank array.
    
      print($array1[$blank[0]]); // should print "test2"
      print($array1[$blank[1]]); // should print "test3"
    ?>
    

    http://ideone.com/eyXL6v

    【讨论】:

    • 感谢您的回复,但这给了我错误:注意:未定义的偏移量:0
    • @user1933466 它适用于我的答案中的两个 ideone 链接。
    猜你喜欢
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 2014-03-05
    相关资源
    最近更新 更多