简体   繁体   中英

How to merge two arrays together and insert into 1 row in a database

I'm new to PHP and I need help with a few things. First I need to merge two arrays together and separate them using a comma and then I need to be able to update my database to insert the result of the merged arrays into one row. To further explain I have a database table called contents. What I want to do in this table is so I need the result of the merge array to be inserted into tierString this is a varchar and not an int but I need it to take in two numbers:

id | name     | desc     | tierString 
1     name1      blah         1,2

This is my function to update the database:

function updateTierString($tier_array){
    $result = false;
    foreach($tier_array as $content_id => $tier){
     
    $update = “UPDATE CONTENTS
               SET 
               tier_string = $tier,
               updated = NOW()
               WHERE
               id = ‘$content_id’”;
   $update = db_query($update);
   }
return $result;

So my issue is I want to merge two arrays together and then separate them using a comma and then insert it into the database. The two arrays would contain integers but I want to turn it into a string. Is it possible?

$arr1 = [1, 2, 3];
$arr2 = [4, 5, 6];

$merged_arr = array_merge($arr1, $arr2);

$comma_seprated = implode(',', $merged_arr);

Now you can use the $comma_seprated var and put in the tierString column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM