Question

I've have an array called $data. Actually it's a quite big array and it gets generated dynamically. That means it could be small or large. But for your reference I'm printing out a relatively small instance of array $data:

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

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

    [pt_doc_file_iname] => Array
        (
            [0] => test_document.docx
            [1] => new_joining.pdf
        )

    [submit] => Update
)

I want to manipulate above array($data) in such a way that it should look as follows:

Array
(
    [op] => edit
    [pt_id] => 4
    [form_submitted] => yes
    [pt_documents_data] => Array        
        (
      [0] => Array
        (   
            [pt_doc_title] => Test Document
        [pt_doc_id] => 6
        [pt_doc_file_iname] => test_document.docx
        )
      [1] => Array
        (
        [pt_doc_title] => New Joining
        [pt_doc_id] => 7
        [pt_doc_file_iname] => new_joining.pdf
        )
      [2] => Array
                (
        [pt_doc_title] => Over load
        [pt_doc_id] => 
        [pt_doc_file_iname] => 
            )
          )
)

But I'm not getting how should I achieve this as I'm relatively new to Array Manipulations in PHP. Can anyone please help me in this regard?

Was it helpful?

Solution

foreach ($array['pt_doc_title'] as $key => $title) {
    $id   = isset($array['pt_doc_id'][$key])         ? $array['pt_doc_id'][$key]         : null;
    $file = isset($array['pt_doc_file_iname'][$key]) ? $array['pt_doc_file_iname'][$key] : null;

    $array['pt_documents_data'][] = array(
        'pt_doc_title'      => $title,
        'pt_doc_id'         => $id,
        'pt_doc_file_iname' => $file
    );
}

unset($array['pt_doc_title'], $array['pt_doc_id'], $array['pt_doc_file_iname']);

Something along these lines. You use the keys of $array['pt_doc_title'] to get the corresponding other two values from the other two arrays, then you group them into a new array pt_documents_data.

For a simplified example of the general operation:

$array = array(
    'foo' => array('a', 'b', 'c'),
    'bar' => array('x', 'y', 'z')
);

$result = array();

foreach ($array['foo'] as $key => $foo) {
    $result[] = array('foo' => $foo, 'bar' => $array['bar'][$key]);
}

// result: array(array('foo' => 'a', 'bar' => 'x'), array('foo' => 'b', 'bar' => 'y'), ...)

OTHER TIPS

This should do it, though not in your specific order:

foreach ($array['pt_doc_title'] as $id => $title) {
            $new_array = array('pt_doc_title' => $title,
                                'pt_doc_id' => $array['pt_doc_id'][$id-1],
                                'pt_doc_file_iname' => $array['pt_doc_file_iname'][$id-1]
                        );
            $array['pt_documents_data'][] = $new_array;
}

unset($array['pt_doc_title'],$array['pt_doc_id'],$array['pt_doc_file_iname'], $array['update']);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top