0

I am trying to loop around an array of return values from a 'POST form' and to then place those values into a database.

the problem that I have is to determine the best way to loop around those values. I tried using the array_key_exists(). but it appears that this function only works with an If clause.

I am working in ZendFrameWork 1.

I enclose my code below and would really appreciate any help and advice.

 foreach(array_key_exists('id', $ReturnedPostvalues))

     $product = EP3D::getSource('EP3D/Products')->retrieve($productId);
     {  
      $product->quantity = $ReturnedPostvalues['quantity'];
      $product->price = $ReturnedPostvalues['price'];
      $product->rrp = $ReturnedPostvalues['rrp'];

      $product->save();
     }
    }

the var_dumped array values returned from the post

 array(6) {
    ["quantity"]=>
    string(3) "222"
    ["price"]=>
    string(3) "220"
    ["rrp"]=>
    string(2) "22"
    ["sampleId"]=>
    string(5) "42960"
    ["id"]=>
    string(1) "5"
    ["delete"]=>
    string(1) "0"
  }
  [6]=>
  array(7) {
    ["quantity"]=>
    string(4) "7777"
    ["price"]=>
    string(4) "2022"
    ["rrp"]=>
    string(2) "22"
    ["sampleId"]=>
    string(5) "42960"
    ["id"]=>
    string(1) "6"
    ["delete"]=>
    string(1) "0"
  }

I basically need to loop around this array and input the data into the database.

2
  • 1
    I can't figure out what you're trying to do. The syntax of foreach is foreach($array as $element). array_key_exists just returns true or false, not an array. And you don't have a $element iteration variable. Commented Aug 22, 2013 at 9:18
  • i have an array of returned values; each should have an Id. i want to be able to calulate how many arrays there are and then loop around the array and extract the information for each instance of that Id Commented Aug 22, 2013 at 9:21

1 Answer 1

1

Maybe this is what you want:

foreach($ReturnedPostvalues as $value) {
    if (array_key_exists('id', $value)) {
        $product = EP3D::getSource('EP3D/Products')->retrieve($value['id']);
        $product->quantity = $value['quantity'];
        $product->price = $value['price'];
        $product->rrp = $value['rrp'];

        $product->save();
    }
}

You need to refresh your understanding of multi-dimensional arrays. Your problem is that you seemed to confuse accessing the top-level array and accessing the sub-arrays.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for your kind advice Barmar. but less of the put down next time. we are all in the process of learning; nobody was born as an expert programmer. WE all had to start from somewhere
you want free assistance, sometimes you have to take the editorializing that comes along with it. This is basic competence, not an advanced technique that only experts would know. I joined SO to give and get help with tricky programming problems, but it seems like it's mostly teaching newbies basic programming techniques. Isn't that what schools are for?
i take your point; however this forumn is for programmers of all levels. the problem with putting people down is that you make others afraid to ask questions; it then becomes a bullying forumn as opposed to a place for programmers to exchange ideas and knowledge. No one has to right to bully others.
We all make mistakes: i am sure that if u looked back at your code from 3 years ago, you wont be happy with all of it. i made a valid mistake and appreciate you taking the time to point this out to me; i however took accept ion to the put down.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.