0

Need to insert PHP array into mysql database as each array as column.

$test_array = array((1,2,3),(test1,test2,test3));

And output like

1   test1

2   test2

3   test3

UPDATE

foreach($test_array AS $test){
              $info_item = array(
                  "name"   => $test[0],
                  "name1"   => $test[1],
               );   
  $id = $fun_obj->insert($info_item,'test_table');
}

like this inserting each array as row.but i need each array as column

Thank you

5
  • 1
    Welcome to SO, please consider the following article to get some help. Commented Jan 22, 2018 at 11:56
  • 1
    Please add the code you use for that and specify the problem you have with it. Commented Jan 22, 2018 at 11:56
  • You just need a loop for your array to insert it's not that hard. What have you tried so far? Commented Jan 22, 2018 at 12:00
  • yes i tried.using loop each array inserting as new row.i need each array as coulmn Commented Jan 22, 2018 at 12:05
  • I don't understand your latest comment. Commented Jan 22, 2018 at 12:19

1 Answer 1

3
<?php

$test_array = array(
                    array(1,2,3),
                    array('test1','test2','test3')
                   );

$new_array = array_map(null, $test_array[0], $test_array[1]);

print_r($new_array);

?>

Outputs:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => test1
        )

    [1] => Array
        (
            [0] => 2
            [1] => test2
        )

    [2] => Array
        (
            [0] => 3
            [1] => test3
        )

)
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.