Combine two arrays and insert into database in codeigniter [duplicate] - php

This question already has answers here:
array merge php with same index
(4 answers)
Closed 1 year ago.
I have this array:
Array
(
[0] => Array
(
[char_id] => 205
)
[1] => Array
(
[char_id] => 954
)
)
And this array:
Array
(
[0] => Array
(
[england] => 523
)
[1] => Array
(
[poland] => 9546
)
)
How I can combine this arrays for inserting in database? I use codeigniter! I want this result:
$data = Array
(
Array
(
[char_id] => 205
[england] => 523
)
Array
(
[char_id] => 954
[poland] => 9546
)
)
I want to use function insert_batch $this->db->insert_batch('mytable', $data);
Number of elements from arrays is different but same count every time!

$arrayA = array();
$arrayA[] = array("char_id"=>205);
$arrayA[] = array("char_id"=>954);
$arrayB = array();
$arrayB[] = array("england"=>523);
$arrayB[] = array("poland"=>9546);
$arrayC = array();
foreach($arrayA as $key=>$a){
$arrayC[$key] = array_merge($arrayA[$key],$arrayB[$key]);
}
print_r($arrayC);

Related

PHP add values from multidimensional array into one array [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 7 months ago.
If I have an array $array like this:
Array (
[0] => Array (
[id] => 11
[name] => scifi
)
[1] => Array (
[id] => 12
[name] => documetary
)
[2] => Array (
[id] => 10
[name] => comedy
)
)
How could I turn it into simply:
Array ( 11, 12, 10 ) with no key value pairs.
I am trying to extract only the id from each array and add them into 1 array. I am trying it with a foreach;
$ids = [];
if ( $array ) {
foreach ( $array as $item ) {
$ids[] = $term->id;
}
}
print_r($ids);
It just returns 3 empty arrays Array ( [0] => [1] => [2] => )
Using a loop, you can do this:
$arr1 = [
['id'=>11,'name'=>'scifi'],
['id'=>12,'name'=>'documentry'],
['id'=>10,'name'=>'comedy'],
];
$arr2 = [];
foreach($arr1 as $internal){
array_push($arr2,$internal['id']);
}
print_r($arr2);
Here we access all internal array's ID's and insert them into a new array.

I want to change the key of multidimensional array [duplicate]

This question already has answers here:
How do you reindex an array in PHP but with indexes starting from 1?
(12 answers)
Closed 3 years ago.
I want to change the key of the multidimensional array. the array contains key like 1,15,23,45 which should be replaced by normal index key like 0,1,2,3. I tried with below code. Something is missing in below code. Please, anyone, suggest to me.
$keys = array_keys($data);
$d = 0;
foreach($data as $row){
$key_data[$d] = $data[$keys[$d]];
unset($row[$keys[$d]]);
$d++;
}
Current Output
Array
(
[15] => Array
(
[0] => Array
(
[app_dealer_id] => 15
[dealer_name] => Sharad Thombre
[shopname] => Shivshankar Fertilizer
[contact_num] => 9049121143
[district] => Parbhani
)
)
[18] => Array
(
[0] => Array
(
[app_dealer_id] => 18
[dealer_name] => Gajanan Khapre
[shopname] => Shreyas Krishi Kendra
[contact_num] => 8007791946
[district] => Parbhani
)
)
)
Expected Output:
Array
(
[0] => Array
(
[0] => Array
(
[app_dealer_id] => 15
[dealer_name] => Sharad Thombre
[shopname] => Shivshankar Fertilizer
[contact_num] => 9049121143
[district] => Parbhani
)
)
[1] => Array
(
[0] => Array
(
[app_dealer_id] => 18
[dealer_name] => Gajanan Khapre
[shopname] => Shreyas Krishi Kendra
[contact_num] => 8007791946
[district] => Parbhani
)
)
)
use array_values()
$array = array_values($array);
Output:-https://3v4l.org/cUAdl
From php.net :
array_values() returns all the values from the array and indexes the array numerically.
So just add it after your loop to reindex you array :
$keys = array_keys($data);
$d = 0;
foreach($data as $row){
$key_data[$d] = $data[$keys[$d]];
unset($row[$keys[$d]]);
$d++;
}
$newArray = array_keys($key_data);

Merge duplicate values in array [duplicate]

This question already has answers here:
How to group subarrays by a column value?
(20 answers)
Closed 5 months ago.
I have this result from a foreach loop.
I tried looping through the array using foreach from the answers in StackOverflow, but I'm having trouble when doing it under another foreach loop.
Array
(
[0] => Array
(
[referenceUid] => FF875951-87CB-942F-84A2-46C620BF07C8
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 300.00
)
)
Array
(
[0] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
)
Array
(
[0] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
)
What I want is to merge the array with duplicate values on referenceUid column. Something like this:
Array
(
[0] => Array
(
[referenceUid] => FF875951-87CB-942F-84A2-46C620BF07C8
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 300.00
)
)
Array
(
[0] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
[1] => Array
(
[referenceUid] => D267A795-E142-C25B-E042-D8A519B57DF4
[itemUid] => 4CD23391-AD8C-C2FB-EF4A-46093D8A37AE
[total] => 150.00
)
)
You can construct a new (merged) array and loop your input to assemble the new structure.
An important consideration is to use the common key (referenceUid) as the array key in your new array so you can reference it easily. If you don't want it at the end, simply reset the array keys e.g. $out = array_values($out).
Here's an example:
$output = array();
foreach ($input as $values) {
$key = $values['referenceUid'];
$output[$key][] = $values;
}
// Don't want the referenceUid in the keys? Reset them:
$output = array_values($output);
Example
//both arrays will be merged including duplicates
$result = array_merge( $array1, $array2 );
//duplicate objects will be removed
$result = array_map("unserialize", array_unique(array_map("serialize", $result)));
//array is sorted on the bases of id
sort( $result );`

How can I implode() only one column from a multidimensional array? [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I have array in following format:
Array
(
[sales] => Array
(
[0] => Array
(
[0] => 1
[1] => 6
)
[1] => Array
(
[0] => 2
[1] => 8
)
[2] => Array
(
[0] => 3
[1] => 25
)
[3] => Array
(
[0] => 4
[1] => 34
)
)
)
Using:
foreach ($data['sales'] as $k => $row) {
$list = implode(",",$row);
}
I get the following as output:
1,62,83,254,34
But I only need the second values from each subArray. The expected result needs to be:
6,8,25,34
How can I remove the first set of values?
Just grab the first column from your array with array_column(), so that you end up with an array, e.g.
Array (
[0] => 6
[1] => 8
[2] => 25
[3] => 34
)
And implode() it then as you already did, e.g.
echo implode(",", array_column($data["sales"], 1));
output:
6,8,25,34
I like array_column() but if you don't have PHP >= 5.5.0:
$list = implode(',', array_map(function($v) { return $v[1]; }, $data['sales']));
Or with the foreach:
foreach ($data['sales'] as $row) {
$list[] = $row[1];
}
$list = implode(',', $list);

Set one array value as parameter to another array [duplicate]

This question already has answers here:
Push elements from one array into rows of another array (one element per row)
(4 answers)
Closed 8 years ago.
Hi i have an array like this
Array
(
[0] => Array
(
[employeename] => abc
)
[1] => Array
(
[employeename] => def
)
)
Array
(
[0] => 1
[1] => 3
)
I need the second array value to be set in first array like this
Array
(
[0] => Array
(
[employeename] => abc
[othername] => 1
)
[1] => Array
(
[employeename] => def
[othername] => 3
)
)
Any Help Will be appreciated , Thanks In Advance :)
Try this :
<?php
$array1 = array(array("employeename" => "abc"),
array("employeename" => "def")
);
$array2 = array(1,3);
foreach($array1 as $key=>$val){
$array1[$key]["othername"] = $array2[$key];
}
echo "<pre>";
print_r($array1);
?>

Categories