PHP 多维数组 - 创建和读取数据

发布于 2024-11-05 12:28:23 字数 381 浏览 0 评论 0原文

我对多维数组有一个小问题。我觉得他们和JAVA里的不一样。例如;我创建了一个类似这样的数组;

$myArray = array();

然后我尝试为其分配 4 个不同的数组,如下所示。

$myArray[0] = $newArray0;
$myArray[1] = $newArray1;
$myArray[2] = $newArray2;
$myArray[3] = $newArray3;

但是,当我尝试从以下行的单元格中读取数据时,

$myArray[0][2];

我无法获得我想要的数据。你们能告诉我我哪里错了吗?

多谢!

I have a little problem with multidimensional arrays. I think they are not like those in JAVA. For instance; I have created a array something like this;

$myArray = array();

Then I try to assign 4 different arrays to it as following.

$myArray[0] = $newArray0;
$myArray[1] = $newArray1;
$myArray[2] = $newArray2;
$myArray[3] = $newArray3;

But when I try to read a data from a cell with following line,

$myArray[0][2];

I could not get the data what I was hoping for. Can you guys tell me where am I mistaken?

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以可爱出名 2024-11-12 12:28:23

您尝试执行的操作应该有效,请尝试确保 $newArray0 也是数字数组而不是关联数组。您可以使用以下代码了解如何设置数组:

<?php
  echo '<pre>';
  print_r($myArray);
  echo '</pre>';
?>

如果您以正确的方式完成了操作,则输出必须类似于:

Array (
  [0] => Array (
    [1] => Value,
    [2] => Value
  ),
  [1] => Array (
    [1] => Etc,
    [2] => Etc
  )
)

如果这就是它所说的,那么 $myArray[0][2] 应该说'ETC'。

What you're trying to do should work, try to make sure that $newArray0 is also a numeric array and not an associative array. You can find out how your array is being setup with the following code:

<?php
  echo '<pre>';
  print_r($myArray);
  echo '</pre>';
?>

If you've done it the right way, the output must be something similar to:

Array (
  [0] => Array (
    [1] => Value,
    [2] => Value
  ),
  [1] => Array (
    [1] => Etc,
    [2] => Etc
  )
)

If that's what it says, then $myArray[0][2] should say 'Etc'.

殤城〤 2024-11-12 12:28:23

你的想法是正确的

$myArray = array();
$myArray1 = array("a","b","c");
$myArray2 = array("d","e","f");
$myArray3 = array("g","h","i");

$myArray[0] = $myArray1;
$myArray[1] = $myArray2;
$myArray[2] = $myArray3;

echo($myArray[0][2]);

工作演示

you got the right idea

$myArray = array();
$myArray1 = array("a","b","c");
$myArray2 = array("d","e","f");
$myArray3 = array("g","h","i");

$myArray[0] = $myArray1;
$myArray[1] = $myArray2;
$myArray[2] = $myArray3;

echo($myArray[0][2]);

WORKING DEMO

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文