array_splice 与多维数组?

发布于 2024-10-07 19:22:16 字数 787 浏览 0 评论 0原文

好的,我对 PHP 还很陌生,目前正在尝试数组。举个例子,假设这是我的数组:

    $t1 = array (
  "basicInfo" => array (
   "The Sineps",
   "December 25, 2010",
   "lemonpole_1g"
  ),
  "overallRecord" => array (
   "23",
   "12",
   "19",
   ""
  )
);

根据我收集到的信息,我发现函数 array_splice 允许我指向数组中的特定索引并添加/删除数据。从我见过的使用此函数的所有示例中......仅使用了数字数组。现在我的问题是如何指向 ["overallRecord"][3](为空),并更新该字段?

为了进一步理解空字段代表“总分”:

$wins = $t1["overallRecord"][0] * 3;
$loss = $t1["overallRecord"][1];
$draw = $t1["overallRecord"][2];
$total = $wins + $draw;

所以总而言之,我想将变量 $total 添加到 ["overallRecord"][3]< /强>。它不一定必须使用array_splice,但是,如果您想出一种不同的方法来实现此尝试并保持简单或添加注释,请:)

提前致谢!

Okay so I'm fairly new to PHP and I am currently experimenting with arrays. As an example, lets assume this is my array:

    $t1 = array (
  "basicInfo" => array (
   "The Sineps",
   "December 25, 2010",
   "lemonpole_1g"
  ),
  "overallRecord" => array (
   "23",
   "12",
   "19",
   ""
  )
);

From what I could gather, I found out that the function array_splice allows me to point to a specific index in the array and add/remove data. From all of the examples that I've seen using this function...only numeric arrays were used. Now my question is how would I point to ["overallRecord"][3](which is empty) for example, and update that field?

For further understanding that empty field is for "total points":

$wins = $t1["overallRecord"][0] * 3;
$loss = $t1["overallRecord"][1];
$draw = $t1["overallRecord"][2];
$total = $wins + $draw;

So to sum it all up, I'd like to add the variable $total to ["overallRecord"][3]. It doesn't necessarily have to be with array_splice, however, if you come up with a different method to achieve this try and keep it simple or add comments please :)

Thanks in advance!

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

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

发布评论

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

评论(2

酒几许 2024-10-14 19:22:16

如果我理解正确的话,只需做

$t1["overallRecord"][3] = $total;

If I understand correctly, simply do

$t1["overallRecord"][3] = $total;
梦回旧景 2024-10-14 19:22:16
$t1["overallRecord"][3] = $total;
$t1["overallRecord"][3] = $total;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文