简体   繁体   中英

How to loop through a list of products and categories on PHP

<? 
 $categoriesID = array("popular","old");
 $product => array (
                    Product 1
                    'categoryID' => $categoriesID[1],
                    'Name' => 'Product One',
                    Product 2
                    'categoryID' => $categoriesID[2],
                    'Name' => 'Product Two',
                    Product 3
                    'categoryID' => $categoriesID[2],
                    'Name' => 'Product Two',
                       Product 4
                    'categoryID' => $categoriesID[2],
                    'Name' => 'Product Two',

                   );

How can I loop through this to reflect that product 1 belongs to category 1, product 2 belongs to category 2, product 3 belongs to category 2 and so on?

I tried the following but no luck..

foreach($product as $key => $pro){
     var_dump($categoriesID[$key]);
 }

I would really appreciated any suggestions or how what i'm doing wrong.The goal is to insert the relationship into a database table where in order to insert a product a category_id is required.

Your arrays are not written correctly. You got a multi dimensional array here (arrays inside of an array). Read this to understand how they are written and how you can work with them: http://php.net/manual/en/language.types.array.php

If your categories are numeric you should also consider to use numeric values: 1 instead of '1' inside of the $categoriesID array or depending on the database auto casting capability you will get issues inserting strings as decimals.

Here is your given code modified as working example. Ive changed the var_dump output for better readability of the result.

Ive also changed the array indexes you have used since arrays start at 0. If you need the numbers still to start at 1 you could add some nonsense value at the beginning of the array or subtract 1 when accessing the array. Keep in mind that this is an quick & dirty solution to the given problem.

Nevertheless as Patrick Q said you should consider some introduction to PHP.

 <?php $categoriesID = array('1','2'); $product = array ( array( 'categoryID' => $categoriesID[0], 'Name' => 'Product One', ), array( 'categoryID' => $categoriesID[1], 'Name' => 'Product Two', ), array( 'categoryID' => $categoriesID[1], 'Name' => 'Product Two', ), array( 'categoryID' => $categoriesID[1], 'Name' => 'Product Two', ) ); foreach($product as $key => $value){ echo var_export($value, true) . '<br>'; }

You could further edit Mariusz's answer to do something like this:

foreach($product as $item){
    echo $item['Name'].' - '.$item['categoryID'].'<br>';
}

This would give you easy access to both product name and category ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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