Bucles de matriz anidados de PHP con actualización de matriz

I'm from a VB; Access; SQL Server background recently introduced to WAMP. I have a problem which I have not been able to solve for a few weeks now, I think I have visited every forum on the web (including here) without success.

I have two arrays filled from MySQL recordsets, one for the site Cart and the other for one of the site departments (catalogs) see a manual interpretation of the filled arrays below.

$catalog[] = array( 
  array('ProdID'=>2,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),       
  array('ProdID'=>6,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  array('ProdID'=>7,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  array('ProdID'=>8,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  array('ProdID'=>9,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),

$cart[] = array(

What I need to do is loop through the catalog array each time a new catalog is chosen, comparing the ProdID of each row in the cart against the ProdID of each row of the catalog, when a match is found I would like to update the catalog 'InBasket' value for that row only to '1' to indicate that the item is already in the customers cart which I can then show on screen (The customer is only allowed to purchase one of any item).

There are five different departments (catalogs) which will contain up to 10 products the cart will contain say four items (If I'm lucky)

I have tried some loops but they don't seem to work.

for ($x = 0; $x < count($catalog); $x++)
  for ($y = 0; $y < count($cart); $y++)
    If ($catalog[$x]['ProductID'] == $cart[$y]['ProductID'] )
      $cart[$x]['InBasket'] = 1;

I hpoe that my arrays are correct, but, the loops seem to update 'InBasket' regardless.

Cualquier idea sería apreciada.

preguntado el 31 de julio de 12 a las 14:07

Deberías estar usando el foreach() command, rather than the for statement, which is typical of VB. -

Should it not be $catalog[$x]["inBasket"] = 1 ? -

4 Respuestas

Su $cart has an array key called ProdID, but you are attempting to use ProductID which doesn't exist. Then, you are attempting to update the InBasket clave de la $cart subarrays, when really, it is a member of the $catalog formación.

Lo primero es lo primero:

Get rid of those incremental loops. In PHP, we much more typically use foreach for iteration, which is a lot more readable:

foreach ($catalog as &$catalog_item) {
  foreach($cart as $cart_item) {
    if ($catalog_item['ProductID'] == $cart_item['ProdID']) {
       // The InBasket key is part of the catalog, not the cart!
       $catalog_item['InBasket'] = 1;

Respondido el 20 de junio de 20 a las 10:06

Thanks for your answer, much appreciated. When I looked over the question later I realised the ProdID - ProductID (Human error), however, the foreach information was what I was looking for. - johncron

Since my last reply I have tried your code against my arrays and the InBasket 'field' is not updated. Did you run your own code ? - johncron

@johncron I just noticed you have an extra [] on your array definitions which make them 3 levels instead of 2. Change them from $catalog[] = array(...) a $catalog = array(...). - Michael Berkowski

@johncron Also, you need an array reference. See the & added in the outer foreach loop above... I tested and it works -- products 2 & 8 are updated correctly. - Michael Berkowski

In your example, the Product ID in both tables is ProdID; you're checking 2 undefined/void array elements (which will be equal).

if ($catalog[$x]['ProdID'] == $cart[$y]['ProdID'] )

Respondido 31 Jul 12, 14:07

The syntax of your array example is wrong:

$catalog[] = array('whatever');

This means that the array with whatever will included into another array.

Ver este ejemplo: http://codepad.org/OMbnEEjA Then you'll see the difference.

Respondido 31 Jul 12, 14:07

Thanks for this info, however, the link produced an internal server error. - johncron

Varios problemas:

  1. you are using the wrong key name:

    If ($catalog[$x]['ProductID'] == $cart[$y]['ProductID'] )

cambiarlo a

    If ($catalog[$x]['ProdID'] == $cart[$y]['ProdID'] )
  1. in the second loop you are doing:

    $cart[$x]['InBasket'] = 1;

    but you stated in the question you would like to update the catalog, so it should be:

    $catalog[$x]['InBasket'] = 1;

You should use the method suggested by @jicd for looping, it is much easier.

vea aquí para obtener más información sobre cómo usar foreach.

Respondido 31 Jul 12, 14:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.