Question

I've an associative array called $data as follows:

Array
(
    [op] => edit
    [pt_id] => 4
    [form_submitted] => yes
    [pt_doc_title] => Array
        (
            [1] => Test Document
            [2] => New Joining
            [3] => Hallo Jolly
        )

    [pt_doc_id] => Array
        (
            [0] => 6
            [1] => 7
        )



    [submit] => Update
)

In order to keep all the package type documents data together I've manipulated the above array as follows:

foreach ($data['pt_doc_title'] as $key => $title) {

      $id   = isset($data['pt_doc_id'][$key-1])         ? $data['pt_doc_id'][$key-1]         : null;
      $data['pt_documents_data'][] = array(
        'pt_doc_title'      => $title,
        'pt_doc_id'         => $id
      );          
    }
unset($data['pt_doc_title'], $data['pt_doc_id']);

After manipulation I'm getting following array $data as follows:

Array
    (
        [op] => edit
        [pt_id] => 4
        [form_submitted] => yes

        [submit] => Update
        [pt_documents_data] => Array
            (
                [0] => Array
                    (
                        [pt_doc_title] => Test Document
                        [pt_doc_id] => 6
                    )

                    [1] => Array
                        (
                            [pt_doc_title] => New Joining
                            [pt_doc_id] => 7
                        )

                    [2] => Array
                        (
                            [pt_doc_title] => Hallo Jolly
                            [pt_doc_id] => 
                        )

                )

        )

My issue is I'm haivng another array called $_FILES as follows and I want to merge one of it's key(name) into above array in a same manner.

Array
(
    [document_file_name_1] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [document_file_name_2] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

    [document_file_name_3] => Array
        (
            [name] => FAQ.doc
            [type] => application/msword
            [tmp_name] => /tmp/phpFiBYKB
            [error] => 0
            [size] => 35840
        )

)

That is if there exists a value under [name] then the final array should be as follows. As there is a value present only in last array element of array $_FILES

    Array
    (
        [op] => edit
        [pt_id] => 4
        [form_submitted] => yes
        [submit] => Update
        [pt_documents_data] => Array
            (
                [0] => Array
                    (
                        [pt_doc_title] => Test Document
                        [pt_doc_id] => 6
                        [pt_doc_file_iname] => 
                    )

            [1] => Array
                (
                    [pt_doc_title] => New Joining
                    [pt_doc_id] => 7
                    [pt_doc_file_iname] => 
                )

            [2] => Array
                (
                    [pt_doc_title] => Hallo Jolly
                    [pt_doc_id] => 
                    [pt_doc_file_iname] => FAQ.doc
                )

        )

)

Can anyone please help me in creation of such final array?

Was it helpful?

Solution

Simplest way would be to walk the $_FILES array and extract the id from the last segment of the field name... then use that -1 as the basis for adding the file to your result array.

Something like this should get you there (untested):

foreach($_FILES as $k => $file){
    $index_to_update = trim(substr($k, strrpos($k, "_")+1))-1;
    $res["pt_document_data"][$index_to_update]["pt_doc_file_iname"] = isset($file["name"])?$file["name"]:"";
}

That is assuming $res is the array that is the parent of the pt_document_data element.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top