简体   繁体   English

在 PHP 中从多维数组中获取值

[英]getting values from multidimensional array in PHP

I have an array, it can have any state at this point in the program (empty or with some other arrays that have been inserted with this format).我有一个数组,它可以在程序中的这一点具有任何状态(空或其他一些已使用此格式插入的数组)。

$productName=$_POST['productname'];
if(!in_array($productName,$productArray)) {
    $productArray[]=array($productName,$_POST['price'],$_POST['desc']);
}

(It gets the values correctly from a form, I checked that) (它从表单中正确获取值,我检查过)

And basically, what I want to do is to add an array to the array so that I have a multidimensional array.基本上,我想做的是在数组中添加一个数组,这样我就有了一个多维数组。 (I know not the most beautiful solution, but hey) My browser does not give an error, so I think that is fine, but when I am trying to put it out, it does not give back anything. (我不知道最漂亮的解决方案,但是嘿)我的浏览器没有出现错误,所以我认为这很好,但是当我试图将其推出时,它没有回馈任何东西。

for($i=0;$i<count($productArray);$i++) {
    echo "Name: ",$productArray[i][0],"<br>";
    echo "Price: ",$productArray[i][1],"<br>";
    echo "Description: ",$productArray[i][2],"<br>";

} }

My website does not put out anything it all, it just says "Name: " and then nothing.我的网站没有发布任何内容,它只是说“名称:”然后什么都没有。 What have I done wrong?我做错了什么? (I am just starting to learn PHP, maybe I am missing something obvious) (我刚刚开始学习 PHP,也许我遗漏了一些明显的东西)

$productArray[i][0] => $productArray[$i][0] . $productArray[i][0] => $productArray[$i][0] 。 Set display_errors to On.将 display_errors 设置为 On。 Your in_array() test is wrong (since you search a string in an array that only contains array items).您的 in_array() 测试是错误的(因为您在仅包含数组项的数组中搜索字符串)。

The guy commenting answered it, I had this stupid oversight.评论的人回答了它,我有这个愚蠢的疏忽。 Thanks a lot!非常感谢! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM