1

I have a PHP form where fields are defined as item[fieldname][], where the set of fields are repeated via Jquery Clone function.

These rows post as following when submitted

[item] => Array ( [itemdate] => Array ( [0] => 2012-07-10 20:44:05 [1] => 2012-07-12 20:44:17 )
                  [flditmname] => Array ( [0] => sdc [1] => sd ) 
                  [fieldtype] => Array ( [0] => 1 [1] => 2 ) 
                  [subscribe] => Array ( [0] => X [1] => X ) 
          )

Now I need to insert an element item[id][] in each of the rows that is submitted (something like a transaction number). I'm confused as to how to accomplish this. I thought of two ways :

  1. Find the number of rows and then add the value by iteration. But for this I face a problem that the PHP count function does not return the correct value (it is counting every array as one and not the rows). I can count from the first element but I will not know it as my fields are populated dynamically.

  2. Some kind of array push which can iterate through the rows and add my element with values.

Looking for help from experts here as to how I shall accomplish this. Or is there any other way that you can suggest how I can handle a header-item transaction posting where each item takes the reference of the header transaction id with a line item number (all fields dynamically populated)? The structure roughly will be as below :

Header: doc id | field1 | field2 | ...

Item: doc id | line no | field3 | field4 | ...

Doc id will be fetched from database as lastno+1 during posting.

2 Answers 2

1

Not sure exactly what you are trying to do but would something like this not work?

foreach ($items as &$item)
{
     $item['id'][] = $id;
}

Edit:

$key = key($items);

foreach ($items[$key] as $item)
{
     $items['id'][] = $id;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. But this will iterate id 4 times (one for each element in the item array) while i want that to iterate only two times like other elements' children ([0] and [1] here).
1
foreach($items as $index => $array)
{
    $items[$index]['id']=$id;

}

I Know this is old , But still may help somebody looking for similar answers

Comments

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.