Skip to main content
Combining values from two arrays with same keys
I have two arrays like this:
$arr1 = Array ( [1] => aaa [2] => bbb [match] => ccc )
$arr2 = Array ( [1] => ddd [2] => xxx [match] => kl )
So I want to combine these two arrays to this array:
$arr3 = Array ( [1] => aaa-ddd [2] => bbb-xxx [at] => ccc-kl )
Here is simple solutions for that.
$arr3 = array();
foreach ($arr1 as $key => $val1) {
$val2 = $arr2[$key];
$arr3[$key] = $val1 . "-" . $val2;
}
********
If i want same for add multiple values in DATABASE with key and values.
DO following things:
$arr3 = array();
foreach (array_filter($post['qty']) as $key => $val1) {
$val2 = $post['item_id'][$key];
$arr3[$key] = $val2;
$arr4[$key] = $val1;
$sql = "INSERT INTO TABLE_NAME ( `field1`,`field2`) VALUES ('".$val1."', ".$val2."')";
$this->db->query($sql);
}
Comments
Post a Comment