PHP associative arrays -


I have some problems in specifying prices for the PHP array. I have read the help files and several articles here. And I'm even more confused

I have an HTML order form that is processed by PHP. The user enters the quantity, and, if necessary, a free text description of the item. Not all items contain a free text description. Post output is:

  [_ POST] = & gt; Array ([items_1] = & gt; [items_2] = & gt; [items_21] => [items_68] => [objects_94] => [items_501] => 2 [txt_501] = & Gt; lagerjet [items_510] = & gt; [txt_510] => item description [items_511] = & gt; [txt_511] => item description [place order] => place your order  

The item element is non-zero if the user has entered the amount on the form. The value of the .txt element can be entered by the side user or the default field description can be made.

Objects end with array of those objects In the ideal format, "array number = count, txt" where the item is part of the underscore, it is calculated> 0 and the value of the TCTT TTTT element is the part if the For this sample, the desired output item ("501") = "2, laserjet" will be

My PHP code:

  foreach ($ _ POST $ key) = $ {GO; $ value} {$ _POST [$ key] = filter ($ value); // post variable wrong input $ x = blast ("_", $ key) filtered for; $ Itemno = $ x [1]; If ($ x [0] = "txt") {$ desc = $ value; } If ($ x [0] = "item") {$ count = $ value; } $ Itemno resonates, "|", $ count, "|", $ desc, "| 
" ;;; If ($ count> 0) and ($ itemno> 0)) {$ item [$ itemno] = $ count;}}

I reduced it to the bottom Is the line of code, and it does not work.

Am I doing wrong?

Changing these two lines:

  if ($ x [0] = "txt") {$ desc = $ value; } If ($ x [0] = "item") {$ count = $ value; }  

to do something like:

  if ($ x [0] == "txt") {$ items [$ itemno] [1 ] = $ Value; } Otherwise ($ x [0] == "item") {$ item [$ itemno] [0] = $ value; }  

and you should get an array of items like this, leaving the last line completely:

  $ items [501] = array (2, Lasetjet '); It's easy to convert  2, laserjet  then. 


Then, the whole code will look like this:

  foreach ($ _ post = & gt; $ value as the $ key) {$ _POST [$ Key] = Filter ($ value); // post variable wrong input $ x = blast ("_", $ key) filtered for; $ Itemno = $ x [1]; If ($ x [0] == "txt") {$ item [$ itemno] [1] = $ value; } Otherwise ($ x [0] == "item") {$ item [$ itemno] [0] = $ value; }} Var_dump ($ item); /  

UPDATE

A huge error - - You used the assignment operator to compare ( = ) instead ( == ).

See full working examples here:


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -