1

I'm collecting all form fields on a page via jQuery and then passing them over to a php page in an ajax post, that array is passed like below (actual data is over 60 fields / arrays at present)

Array
(
    [0] => Array
        (
            [0] => main
            [1] => text
            [2] => product-name
            [3] => fieldvalue
        )
[1] => Array
    (
        [0] => main
        [1] => select
        [2] => product-range
        [3] => fieldvalue
    )

[2] => Array
    (
        [0] => main
        [1] => select
        [2] => product-year
        [3] => fieldvalue
    )

[3] => Array
    (
        [0] => main
        [1] => text
        [2] => product-type
        [3] => fieldvalue
    )

[4] => Array
    (
        [0] => main
        [1] => text
        [2] => product-sku
        [3] => fieldvalue
    )

[5] => Array
    (
        [0] => main
        [1] => text
        [2] => component-name
        [3] => fieldvalue
    )

[6] => Array
    (
        [0] => main
        [1] => text
        [2] => component-stid
        [3] => fieldvalue
    )

On the form the user can dynamically add multiple sets of the component fields, what I am looking to do is to get group the 7 component fields into an array and then add into a multidimensional array of components that i can sort through later. the way I am currently looping through the array data is as follows

foreach($formdata as $value) {

        if($value[0] == 'main') {

            if($value[2] == 'product-name') { $productname = $value[3]; }
            if($value[2] == 'product-range') { $productrange = $value[3]; }
            if($value[2] == 'product-year') { $productyear = $value[3]; }
                    }
}

I am really struggling to find a clean way of pulling this off, could anyone advise on best practice?

Thanks

4
  • what you are trying to ask here. can you explain it in little better way? Commented Oct 7, 2014 at 8:39
  • @Dhanendran I'm grabbing a large series of form fields, within that form there is a row with 6 fields for adding a component, the user can add multiple rows of those 6, im trying to filter that info from the main form data array and pass it to its own array while keep the row values grouped Commented Oct 7, 2014 at 8:44
  • whats going to be the final output anyways? Commented Oct 7, 2014 at 8:51
  • @Ghost the components will be used to do a few queries and ultimately stored in mysql. Commented Oct 7, 2014 at 9:00

1 Answer 1

1

You can use an associative array like this:

$arr = array(
    'product_range' => array("main" => "text", 'product_range' => 'val'),
    'product_type' => array("main" => "text", 'product_type' => 'val')
);
foreach ($arr as $key => $val) {
    echo $key." is ".$val[$key]."<br>";
}
Sign up to request clarification or add additional context in comments.

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.