We help IT Professionals succeed at work.

Array - insertion of data not working as intended

Gaute Rønningen
on
198 Views
Last Modified: 2008-03-06
I got a problem with this code:

-- Start of Code --

          if (is_numeric($this->get_shoesize($products_id))) {
                  $shoe_size = (float)$this->get_shoesize($products_id);
          } else {
                  $shoe_size = (float)0;
          }
          print 'shoesize: '.$shoe_size.'<br/>';
          $products_array[] = array('id' => $products_id,
                                    'name' => $products['products_name'],
                                    'model' => $products['products_model'],
                                    'image' => $products['products_image'],
                                    'price' => $products_price,
                                    'quantity' => $this->contents[$products_id]['qty'],
                                    'weight' => $products['products_weight'],
                                    'shoe_max_size' => (int)$products['shoe_max_size'],
                                    'shoe_min_size' => (int)$products['shoe_min_size'],
                                    'size_interval' => (float)$products['size_interval'],
                                    'chosen_size' => $shoe_size, // <--- Faulty field
                                    'final_price' => ($products_price + $this->attributes_price($products_id)),
                                    'tax_class_id' => $products['products_tax_class_id'],
                                    'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''));
            print var_dump($products_array['chosen_size']);

-- End of Code --

And the following is being dumped by the prints:

shoesize: 36
NULL

I have no idea why the $products_array['chosen_size'] becomes NULL, whilst $shoe_size obviously has a value. Anyone have an idea?
eX.
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Gaute RønningenOwner, Developer

Author

Commented:
array(1) { [0]=> array(14) { ["id"]=> int(65) ["name"]=> string(18) "Tamaris modell 396" ["model"]=> string(3) "396" ["image"]=> string(16) "Stovelett_37.jpg" ["price"]=> string(9) "1099.0000" ["quantity"]=> string(1) "1" ["weight"]=> string(4) "0.00" ["shoe_max_size"]=> int(42) ["shoe_min_size"]=> int(32) ["size_interval"]=> float(0.5) ["chosen_size"]=> float(36) ["final_price"]=> float(1099) ["tax_class_id"]=> string(1) "1" ["attributes"]=> string(0) "" } }


Think I'm missing some []'s as in:
$products_array['chosen_size'];

Needs to be replaced with:
$products_array[$products_id]['chosen_size'];

Which solved it :) The error wasn't in the array, it was in the testing line.....
Thanks for helping me see this Andrewm1986

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.