Combining two input arrays into one array for input into a database with PHP

I have a jQuery function that allows the user to create a list of links. By clicking on the “add line” link, they create as many of these blocks (shown below) as they like, into which they can enter information.

<div class="links_row">

    <input type="text" name="link_name[]">
    <input type="text" name="link_url[]">

</div>

I am sending this data to a PHP function for insertion into a database that looks like this:

+---------------+
|id             |
+---------------+
|name           |
+---------------+
|url            |
+---------------+

I need to figure out how to combine the two input arrays link_name [] and link_url [] and skip the resulting resulting unit array by inserting them as a row into the database.

+3
source share
3 answers

, , . , , .

$rows = array();

$num_results = count($_POST['link_name']);
for($i = 0; $i < $num_results;  $i++) {
    $rows[$i]['url'] = $_POST['link_url'][$i];
    $rows[$i]['name'] = $_POST['link_name'][$i];
}
+2

. array_merge() ?

, .

EDIT: , array_combine(). , , - .

+1

No, you're better off with an array map, because numbers will correlate. Departure:

http://php.net/manual/en/function.array-map.php

0
source

Source: https://habr.com/ru/post/1754163/


All Articles