我有2个2d数组(array1和array2),这是2个表,每个表有5列。
我需要通过添加数组2的列来编辑array1,以获得一个包含10列的表的arry
示例:
array1 [1,1,1,1,1]
[2,2,2,2,2]
[3,3,3,3,3]
array2 [a,a,a,a,a]
[b,b,b,b,b]
[c,c,c,c,c]
new array1 [1,1,1,1,1,a,a,a,a,a]
[2,2,2,2,2,b,b,b,b,b]
[3,3,3,3,3,c,c,c,c,c]
Thnak对于任何建议都很重要
答案 0 :(得分:1)
您可以迭代并使用Array#splice
来连接其他值。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$mysql_host = "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_database = "medvedgrad";
// First we create the connection
$pdo = new PDO("mysql:host=".$mysql_host .";dbname=".$mysql_database .";charset=utf8", $mysql_username, $mysql_password);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = json_decode(file_get_contents("php://input"));
$zm = $data->zlatni_medvjed;
$ck = $data->crna_kraljica;
$gv = $data->gricka_vjestica;
$dk = $data->dva_klasa;
$fk = $data->fakin;
//Let's do like Jack the ripper, lets divide into parts :p
$updateparts = array();
$zm = trim($zm);
if($zm != "")
{
$zmpart = "`zlatni_medvjed`=`zlatni_medvjed`+:zm";
$updateparts[] = $zmpart;
}
$ck = trim($ck);
if($ck != "")
{
$ckpart = "`crna_kraljica`=`crna_kraljica`+:ck";
$updateparts[] = $ckpart;
}
$gv = trim($gv);
if($gv != "")
{
$gvpart = "`gricka_vjestica`=`gricka_vjestica`+:gv";
$updateparts[] = $gvpart;
}
$dk = trim($dk);
if($dk != "")
{
$dkpart = "`dva_klasa`=`dva_klasa`+:dk";
$updateparts[] = $dkpart;
}
$fk = trim($fk);
if($fk != "")
{
$fkpart = "`fakin`=`fakin`+:fk";
$updateparts[] = $fkpart;
}
$updatepartstring = implode(",",$updateparts);
$update_query="UPDATE `stanje_piva`
SET
$updatepartstring
WHERE `id`=1";
// Then we prepare, and execute the query
$stmt = $pdo->prepare($update_query);
$stmt->execute(array("zm" => $zm, "ck" => $ck, "gv" => $gv, "dk" => $dk, "fk" => $fk));
?>
&#13;
答案 1 :(得分:0)
如果两个数组具有相同的长度,那么您可以安全地使用此
var arr1 = [[1,2,3], [4,5,6], [7,8,9]];
var arr2 = [["a","b","c"], ["d","e","f"], ["g","h","i"], ["j", "k","l"]];
var narr = [];
for(var ii = 0, len = Math.min(arr1.length, arr2.length); ii < len; ii++) {
narr[ii] = arr1[ii].concat(arr2[ii]);
}
这将创建一个新数组,其长度等于两个数组的最小长度 arr1 &amp;的 ARR2 强>
结果将是:
[1,2,3,"a","b","c"]
[4,5,6,"d","e","f"]
[7,8,9,"g","h","i"]
如果您不确定它们的长度是否相同,则可以直接更改为此代码
var arr1 = [[1,2,3], [4,5,6], [7,8,9]];
var arr2 = [["a","b","c"], ["d","e","f"], ["g","h","i"], ["j", "k","l"]];
var narr = [];
for(var ii = 0, len = Math.max(arr1.length, arr2.length); ii < len; ii++) {
narr[ii] = arr1[ii] ? arr1[ii].concat(arr2[ii]) : arr2[ii];
}
结果将是
[1,2,3,"a","b","c"]
[4,5,6,"d","e","f"]
[7,8,9,"g","h","i"]
["j","k","l"]