新的PHP用户留下了挠头 . 此函数包含嵌套在 if/else 中的两个 foreach 循环,页面为 index.php . 在 index.php 上,代码工作正常,页面显示数组中的最后四个项目 .

但是,如果页面是 NOT index.php ,则数组似乎受到第一个和第二个 foreach 循环之间的代码的影响,并且仅显示为具有第一级键作为整数的1D数组 . 我该如何解决?几乎我找到的所有PHP文档都没有成功 .

function list_products_html($products) {
//If page is INDEX.PHP, show only last 4 products in reverse order with PayPal drop-down and Add To Cart
if ($_SERVER['PHP_SELF'] == "index.php") {
    $products_reverse = array_reverse($products, true);
    $products_new = array_slice($products_reverse, 0, 4, true);

    foreach ($products_new as $product_new_id => $product_new) {
        echo list_product_html($products_new);
        echo generate_paypal_form($products_new);
        echo '</li>';
        }

//Else, show all products
} else {

    foreach ($products as $product_id => $product) {
        echo list_product_html($products);
        echo generate_paypal_form($products);
        echo '</li>';
     }
    }
   }

在第一个 foreach 循环中, var_dump 显示我的数组如下(这是正确的):

array(8) { [101]=> array(5) { ["name"]=> string(15) "Logo Shirt, Red" ["img"]=> string(24) "img/shirts/shirt-101.jpg" ["price"]=> int(18) ["paypal"]=> string(13) "BJDE2BMPHMQ7Q" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [102]=> array(5) { ["name"]=> string(26) "Mike the Frog Shirt, Black" ["img"]=> string(24) "img/shirts/shirt-102.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "2H6VLSYHY3QC8" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [103]=> array(5) { ["name"]=> string(25) "Mike the Frog Shirt, Blue" ["img"]=> string(24) "img/shirts/shirt-103.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "MDP23L74U3F4L" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [104]=> array(5) { ["name"]=> string(17) "Logo Shirt, Green" ["img"]=> string(24) "img/shirts/shirt-104.jpg" ["price"]=> int(18) ["paypal"]=> string(13) "Q39L2XSPANB3Y" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [105]=> array(5) { ["name"]=> string(27) "Mike the Frog Shirt, Yellow" ["img"]=> string(24) "img/shirts/shirt-105.jpg" ["price"]=> int(25) ["paypal"]=> string(13) "7LVQ78CEPSKVN" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [106]=> array(5) { ["name"]=> string(16) "Logo Shirt, Gray" ["img"]=> string(24) "img/shirts/shirt-106.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "G9A9UEVWCTDQN" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [107]=> array(5) { ["name"]=> string(21) "Logo Shirt, Turquoise" ["img"]=> string(24) "img/shirts/shirt-107.jpg" ["price"]=> int(20) ["paypal"]=> string(13) "NNERMRTJSTG6Q" ["sizes"]=> array(4) { [0]=> string(5) "Small" [1]=> string(6) "Medium" [2]=> string(5) "Large" [3]=> string(7) "X-Large" } } [108]=> array(5) { ["name"]=> string(18) "Logo Shirt, Orange" ["img"]=> string(24) "img/shirts/shirt-108.jpg" ["price"]=> int(25) ["paypal"]=> string(13)

在SECOND foreach 循环中,我得到了

“警告:为foreach()提供的参数无效”

错误,因为 var_dump 对于相同的 $products 数组显示以下内容:

int(101)int(102)int(103)int(104)int(105)int(106)int(107)int(108)