Page 1 of 1

Searching Array values

Posted: Sat Aug 07, 2010 4:42 pm
by tomnoble
Hi,

I am in the final stages of an e-commerce project, and I want to check the cart objects. I know that I must be doing some really silly things here but I cant work out what. Here is the array of information I am given:

Array ( [0] => stdClass Object ( [cart_item_id] => 171 [cart_id] => 1 [nid] => 4058 [qty] => 1 [changed] => 1281174017 [data] => Array ( [attributes] => Array ( ) [shippable] => 1 [module] => uc_product ) [title] => PRODUCT TITLE [vid] => 4058 [cost] => 0 [price] => 62.2 [weight] => 1694 [module] => uc_product [model] => 227 ) [1] => stdClass Object ( [cart_item_id] => 172 [cart_id] => 1 [nid] => 4054 [qty] => 1 [changed] => 1281174029 [data] => Array ( [attributes] => Array ( ) [shippable] => 1 [module] => uc_product ) [title] => PRODUCT TITLE [vid] => 4054 [cost] => 0 [price] => 9.9 [weight] => 145 [module] => uc_product [model] => 223 ) [2] => stdClass Object ( [cart_item_id] => 173 [cart_id] => 1 [nid] => 3903 [qty] => 1 [changed] => 1281174064 [data] => Array ( [attributes] => Array ( ) [shippable] => 1 [module] => uc_product ) [title] => PRODUCT TITLE [vid] => 3903 [cost] => 0 [price] => 9.2 [weight] => 145 [module] => uc_product [model] => 44 ) )

Here is the array I want to perform my check on:

Code: Select all

$array = array(9, 7, 44, 17, 3);


What I want to do is the following

For each item in Products array
If uc_product[model] is not in $array
Running Total = Running total + [price]

I know this is a very bespoke question, but help would be very much appreciated.

Regards

Re: Searching Array values

Posted: Sat Aug 07, 2010 5:05 pm
by greyhoundcode
How about a foreach loop to iterate through your products[] array, and use in_array() to test for a match with uc_product[model]?

Re: Searching Array values

Posted: Sat Aug 07, 2010 5:30 pm
by tomnoble
Do you mean like this?

Code: Select all

$items = uc_cart_get_contents();
$itemsarray = (array) $items; //Product Array
$checkarray = array(9, 7, 44, 17, 3);

foreach($itemsarray as $model)
{
if (in_array(uc_product['model'], $checkarray))
{ }
else
{$Total == $Total + ['price'];}
}

Re: Searching Array values

Posted: Sun Aug 08, 2010 1:21 am
by greyhoundcode
I'l be honest and say it isn't completely clear (to me) how your code works - however:

Code: Select all

// Is $items already an array?
$items = uc_cart_get_contents();

// Set $total to zero
$total = 0;

// Your check array
$checkArray = array(9, 7, 44, 17, 3);

// Let's iterate through the items array
foreach($items as $model)
{
    // If uc_product[model] is *not* in the checkArray...
    if (!in_array($model->uc_product['model'], $checkArray))
        $total += $model->price;
}
For future reference, if you are going to show the results of print_r($array) then it would be worth formatting it as you would PHP code (imo) - this would make the structure far clearer.

Re: Searching Array values

Posted: Sun Aug 08, 2010 5:05 am
by tomnoble
Hi Thanks for help so far.

The code seems to be getting the correct information, but the check condition is never true.

For example, I have a product with model "227" which is in the array, but the condition is still evaluating as false, therefore adding to the total. Could it be something to do with

Code: Select all

get_object_vars
being needed?

Any ideas?

Kind Regards

Tom

Re: Searching Array values

Posted: Sun Aug 08, 2010 6:03 am
by greyhoundcode
Within the foreach loop, if you print_r($model->uc_product); what do you see?

Re: Searching Array values

Posted: Sun Aug 08, 2010 6:21 am
by tomnoble
I dont get anything come up.

The only time I get something come up is: asking to print the entire items array.

Re: Searching Array values

Posted: Sun Aug 08, 2010 6:51 am
by tomnoble
If it helps:

Cannot use object of type stdClass as array in

That is the error I get.

I found this earlier: http://www.ubercart.org/forum/support/1 ... m_variable , not sure if it will help. I need to get this sorted tonight (if at all possible) and I cant seem to find any working combination.

Kind Regards

Re: Searching Array values

Posted: Sun Aug 08, 2010 2:53 pm
by tomnoble
Finally have it working. Due to my poor formatting of the array, we miss interpreted what we were looking for.

The complete code is:

Code: Select all

<p><?php
// Is $items already an array?
$items = uc_cart_get_contents();

// Set $total to zero
$total = 0;

// Your check array
$checkArray = array(9, 20, 44, 17, 3);

// Let's iterate through the items array
foreach($items as $sku)
{   
 $var = $sku->model;
// If uc_product[model] is *not* in the checkArray...
    if (!in_array($var, $checkArray))
        $total += $sku->price;
}
echo $total;
?></p>
Thanks for your help.