how to insert batch codeigniter - php

Ii everyone, I have an post data in array like this, I'm so confused how create the logic in controller:
POST Data:
Array
(
[nama_agenda] => blalala
[kilasan] => asdsadsadasd
[tgl_agenda] => 2014-06-01
[jam_agenda] => 13:27:30
[komisi] => Array
(
[0] => 1
[1] => 3
)
[fraksi] => Array
(
[0] => 1
[1] => 4
)
[badan] => Array
(
[0] => 1
[1] => 3
)
[anggota] => Array
(
[0] => 1
[1] => 4
)
[bagian] => Array
(
[0] => 2
[1] => 4
)
)
My question is how to insert into database, in controller? Thank's for help. I'll appreciate.

Since your structure is not well formed for insert_batch method. Your need to restructure it first. Consider this example:
$original_values = array(
'nama_agenda' => 'blalala',
'kilasan' => 'asdsadsadasd',
'tgl_agenda' => '2014-06-01',
'jam_agenda' => '13:27:30',
'komisi' => array(1, 3),
'fraksi' => array(1, 4),
'badan' => array(1, 3),
'anggota' => array(1, 4),
'bagian' => array(2, 4),
);
$new_values = array();
for($x = 0, $size = count($original_values['komisi']); $x < $size; $x++) {
foreach($original_values as $key => &$value) {
if(!is_array($value)) {
$new_values[$x][$key] = $value;
} else {
$new_values[$x][$key] = array_shift($value);
}
}
}
echo '<pre>';
print_r($new_values);
Should yield something like:
Array
(
[0] => Array
(
[nama_agenda] => blalala
[kilasan] => asdsadsadasd
[tgl_agenda] => 2014-06-01
[jam_agenda] => 13:27:30
[komisi] => 1
[fraksi] => 1
[badan] => 1
[anggota] => 1
[bagian] => 2
)
[1] => Array
(
[nama_agenda] => blalala
[kilasan] => asdsadsadasd
[tgl_agenda] => 2014-06-01
[jam_agenda] => 13:27:30
[komisi] => 3
[fraksi] => 4
[badan] => 3
[anggota] => 4
[bagian] => 4
)
)
Now you can use insert_batch() method.
$this->db->insert_batch('table_name', $new_values);

get all the data in array using $this->input->post() eg:
$bagian= $this->input->post('bagian');
and create a array()
$arr=array(
'db_table_col_1'=>$bagian,
'db_table_col_2'=>$post_data,
'db_table_col_2'=>$post_data
);
pass this array to model
$this->your_model_name->function_name($arr);
then in model create function
function_name($arg){
$this->db->insert('table_name',$arr);
}
if you want to insert multiple row then just use foreach
<?php
$arr1=array();
$arr= array(
'nama_agenda' => 'blalala',
'kilasan' => 'asdsadsadasd',
'tgl_agenda' => '2014-06-01',
'jam_agenda' => '13:27:30',
'komisi' => array
(
'0' => 1,
'1' => 3
),
'fraksi' => array
(
'0' => 1,
'1' => 4
),
'badan' => array
(
'0' => 1,
'1' => 3
),
'anggota' => array
(
'0' => 1,
'1' => 4
),
'bagian' => array
(
'0' => 2,
'1' => 4
)
);
foreach($arr as $row){
if(is_array($row)){
array_push($arr1,$row);
}
}
print_r($arr1);
and then pass this array to batch_insert
function_name($arr1){
$this->db->insert_batch('table_name',$arr1);
}
note arr1 syntax must be
$arr1 = array(
array(
'table_col1' => 'My title' ,
'table_col2' => 'My Name'
),
array(
'table_col1' => 'other title' ,
'table_col2' => 'other Name'
)
);
?>

Related

Combine value array in php

i have problem to combine values based on id.
I have data like this :
Array(
[0] => Array(
[id] => 1,
[id_name] => a
[id_vales] => 5
)
[1] => Array(
[id] => 1
[id_name] => a
[id_vales] => 4
)
[2] => Array(
[id] => 3
[id_name] => b
[id_vales] => 4
)
[3] => Array(
[id] => 3
[id_name] => b
[id_vales] => 3
)
)
then, i want combine [id_values] based on id, so i can get data like this in php
Array(
[0] => Array(
[id] => 1
[id_name] => a
[id_vales] => 5, 4
)
[1] => Array(
[id] => 3
[id_name] => b
[id_vales] => 4, 3
)
)
You can use the following example to merge your array
<?php
$mainArray = array(array('id' => 1, 'id_name' => 'a', 'id_vales' => 5),
array('id' => 1,'id_name' => 'a','id_vales' => 4),
array('id' => 3, 'id_name' => 'b','id_vales' => 4),
array('id' => 3,'id_name' => 'b','id_vales' => 3)
);
$result = array();
$tempArray = array();
foreach($mainArray as $key => $value)
{
if(isset($tempArray[$value['id']]))
{
$tempArray[$value['id']] .= ", ".$value['id_vales'];
$result[] = array('id' => $value['id'], 'id_name' => $value['id_name'], 'id_vales' => $tempArray[$value['id']]);
}
else
{
$tempArray[$value['id']] = "".$value['id_vales'];
}
}
echo "<pre>";
print_r($result);
?>
You can find running example here https://paiza.io/projects/3sS3GXH7GHqoipH8k-YtBQ
Output:
Array
(
[0] => Array
(
[id] => 1
[id_name] => a
[id_vales] => 5, 4
)
[1] => Array
(
[id] => 3
[id_name] => b
[id_vales] => 4, 3
)
)
I have created an array for you. from this array you can easily create your array and get the result.
$data = array();
foreach($array as $key=>$value){
$data[$value['id']]['id'] = $value['id'];
$data[$value['id']]['id_vales'][] = $value['id_vales'];
$data[$value['id']]['id_name'] = $value['id_name'];
}

combine two array and get new one

i had an associative array like this : i need to merge two array and last hr rating i need to add hrrating1 = , hr rating 2 = like this
array (
0 =>
array (
'skill_name' => 'JDK (Java Development Kit)',
'desc' => '',
'req_rating' => '2',
'user_rating' => '3',
'hrRating' => '2',
),
1 =>
array (
'skill_name' => 'Java Servlets',
'desc' => '',
'req_rating' => '4',
'user_rating' => '3',
'hrRating' => '3',
),
2 =>
array (
'skill_name' => 'JDK (Java Development Kit)',
'desc' => '',
'req_rating' => '2',
'user_rating' => '3',
'hrRating' => '2',
),
3 =>
array (
'skill_name' => 'Java Servlets',
'desc' => '',
'req_rating' => '4',
'user_rating' => '3',
'hrRating' => '4',
),
)
Needed Output :
array (
0 =>
array (
'skillName' => 'JDK',
'comments' => '',
'jobRating' => '2',
'userRating' => '3',
'skillGap' => '-1',
'hrRating1' => '7',
'hrRating2' => '2',
),
1 =>
array (
'skillName' => 'Java Servlets',
'comments' =>'',
'jobRating' =>'4',
'userRating' =>'3',
'skillGap' => '1',
'hrRating1' => '2',
'hrRating2' => '3',
),
)
i need to combine both array with last column hrRating should different like hrRating1 & hrrating# like this any help regarding????
Updated (single array input)
I do not know if I can use skill_name as a array key (probably not), hence $tmp_skill, $tmp_index.
function process_collection($data) {
$tmp_skill = [];
$tmp_index = [];
$result = [];
foreach ($data as $k => $item) {
$skill = $item['skill_name'];
echo $skill.'<br>';
$idx = array_search($skill, $tmp_skill);
if ($idx === false) {
//$result[$idx]['_ratings'] = $item['hrRating'];
//continue;
$idx = count($tmp_skill);
$tmp_index[] = $idx;
$tmp_skill[] = $skill;
$result[$idx] = $item;
$result[$idx]['_ratings'] = [];
}
$result[$idx]['_ratings'][] = $item['hrRating'];
}
// conversion array of ratings to individual values 'hrRating1', 'hrRating2', ...
foreach($result as &$item) {
$i = 0;
foreach( $item['_ratings'] as $rate)
$item['hrRating' . (++$i)] = $rate;
unset($item['_ratings']);
unset($item['hrRating']);
}
return $result;
}
$array_2 = max($arra_1,$arra) ;
print_r($array_2);
Array ( [0] => Array ( [skillName] => JDK [comments] => [jobRating] => 2 [userRating] => 3 [skillGap] => -1 [hrRating] => 2 ) [1] => Array ( [skillName] => Java Servlets [comments] => [jobRating] => 4 [userRating] => 3 [skillGap] => 1 [hrRating] => 3 ) )
You can use the array_merge() merge function to combine arrays as following:
$comined_array=array_merge($array1,$array2);
Now the $combined_array will be containing the value of both $array1 and $array2.
What you are looking for is array merge. The function array combine is an array of keys and then values whereas array merge append arrays onto the first.
As mentioned in the other answers, change the array variables to match your own.
$array_1 = ['a'];
$array_2 = ['b'];
$new_array = array_merge($array_1, $array_2);
print_r($new_array);
// ['a', 'b']
Find more array functions here.
You can try any from these alternatives:-
Assuming that both the array's has same elements and keys and named it as $arr1 and $arr2
Alternative 1 :- Make an array of all hrRating and asign it to main array
foreach ($arr1 as $key => $data) {
$arr1[$key]['hrRating'] = array($data['hrRating'], $arr2[$key]['hrRating']);
}
print_r($arr1);
Output :-
Array
(
[0] => Array
(
[skillName] => JDK
[comments] =>
[jobRating] => 2
[userRating] => 3
[skillGap] => -1
[hrRating] => Array
(
[0] => 2
[1] => 7
)
)
[1] => Array
(
[skillName] => Java Servlets
[comments] =>
[jobRating] => 4
[userRating] => 3
[skillGap] => 1
[hrRating] => Array
(
[0] => 3
[1] => 2
)
)
)
Alternative 2 :- Assign every hrRating values from different array with new sequence key
foreach ($arr1 as $key => $data) {
unset($arr1[$key]['hrRating']);
$arr1[$key]['hrRating1'] = $data['hrRating'];
$arr1[$key]['hrRating2'] = $arr2[$key]['hrRating'];
}
print_r($arr1);
Output :-
Array
(
[0] => Array
(
[skillName] => JDK
[comments] =>
[jobRating] => 2
[userRating] => 3
[skillGap] => -1
[hrRating1] => 2
[hrRating2] => 7
)
[1] => Array
(
[skillName] => Java Servlets
[comments] =>
[jobRating] => 4
[userRating] => 3
[skillGap] => 1
[hrRating1] => 3
[hrRating2] => 2
)
)
Just to add to the variety: Here is another way of re-arranging your array. I took the liberty of combining all arrays into a single master array $in. By doing a sequence of foreach calls will get your result fairly quickly.
I also added the feature that I named the individual rating arrays after their skill_name. If you don't like that, just comment out the line below:
// $j=$aa['skillName'];unset($aa['skillName']);
Here is the input data I used for testing:
$in=array(
array (
array (
'skillName' => 'JDK',
'comments' => '',
'jobRating' => '2',
'userRating' => '3',
'skillGap' => '-1',
'hrRating' => '2',
), array (
'skillName' => 'Java Servlets',
'comments' =>'',
'jobRating' =>'4',
'userRating' =>'3',
'skillGap' => '1',
'hrRating' => '3',
),
),
array (
array (
'skillName' => 'JDK',
'comments' => '',
'jobRating' => '2',
'userRating' => '3',
'skillGap' => '-1',
'hrRating' => '7',
), array (
'skillName' => 'Java Servlets',
'comments' =>'',
'jobRating' =>'4',
'userRating' =>'3',
'skillGap' => '1',
'hrRating' => '2',
)
),
array (
array (
'skillName' => 'JDK',
'comments' => 'not bad',
'jobRating' => '3',
'userRating' => '3',
'skillGap' => '-1',
'hrRating' => '4',
), array (
'skillName' => 'Java Servlets',
'comments' =>'',
'jobRating' =>'8',
'userRating' =>'3',
'skillGap' => '1',
'hrRating' => '1',
),
));
And this is the complete code for re-arranging:
foreach ($in as $i => $na) foreach($na as $j => $aa) {
$j=$aa['skillName'];unset($aa['skillName']);
foreach ($aa as $k => $v) $ret[$j][$k][]=$v;
}
print_r($ret);
And this is what I get as output:
Array
(
[JDK] => Array
(
[comments] => Array
(
[0] =>
[1] =>
[2] => not bad
)
[jobRating] => Array
(
[0] => 2
[1] => 2
[2] => 3
)
[userRating] => Array
(
[0] => 3
[1] => 3
[2] => 3
)
[skillGap] => Array
(
[0] => -1
[1] => -1
[2] => -1
)
[hrRating] => Array
(
[0] => 2
[1] => 7
[2] => 4
)
)
[Java Servlets] => Array
(
[comments] => Array
(
[0] =>
[1] =>
[2] =>
)
[jobRating] => Array
(
[0] => 4
[1] => 4
[2] => 8
)
[userRating] => Array
(
[0] => 3
[1] => 3
[2] => 3
)
[skillGap] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[hrRating] => Array
(
[0] => 3
[1] => 2
[2] => 1
)
)
)
See the demo here: http://rextester.com/WEUQ12234
you can combine arrays in two ways:
$third_array = combine_array($array1,$array2);
and the other way is:
$array = $array1 + $array2;
change the $array1 & $array2 as per your array names.

How to get Php multidimensional array same key’s same value’s related total in new array?

Php multidimensional array same key’s same value’s related total in
new array. I have an array of following mentioned. i need new array
as total qty of same item_id. anyone can help would be appreciate.
My Original Array is as following
Array
(
[a] => Array
(
[item] => Array
(
[item_id] => 1
)
[qty] => 0
),
[b] => Array
(
[item] => Array
(
[item_id] => 2
)
[qty] => 35
),
[c] => Array
(
[item] => Array
(
[item_id] => 2
)
[qty] => 15
),
[e] => Array
(
[item] => Array
(
[item_id] => 3
)
[qty] => 20
),
);
I want array Output like following :
Array(
[0] => Array (
[item_id] => 1,
[item_total_qty] => 0,
)
[1] => Array (
[item_id] => 2,
[item_total_qty] => 50,
)
[2] => Array (
[item_id] => 3,
[item_total_qty] => 20,
)
);
Hope it help
$arrays = array(
'a' => array(
'item' => array(
'item_id' => 1
),
'qty' => 0
),
'b' => array(
'item' => array(
'item_id' => 2
),
'qty' => 35
),
'c' => array(
'item' => array(
'item_id' => 2
),
'qty' => 15
),
'd' => array(
'item' => array(
'item_id' => 3
),
'qty' => 20
)
);
$result = array();
foreach ($arrays as $key => $array) {
if (is_array($result) && !empty($result)) {
foreach ($result as $key => $r) {
if ($r['item_id'] == $array['item']['item_id']) {
$result[$key]['item_total_qty'] += $array['qty'];
continue 2;
}
}
$result[] = array(
'item_id' => $array['item']['item_id'],
'item_total_qty' => $array['qty']);
} else {
$result[] = array(
'item_id' => $array['item']['item_id'],
'item_total_qty' => $array['qty']);
}
}
Simple foreach on your original table:
$sorted = array();
foreach ($original as $item) {
$id = $item['item']['item_id'];
$sorted[$id]['item_total_qty'] = $sorted[$id] ? $sorted[$id] + $item['qty'] : item['qty'];
$sorted[$id]['item_id'] = $id;
}
$sorted = array_values($sorted);

Access MySql array by column name

My query looks like this:
SELECT * FROM tblName WHERE btn_group_id = 1;
Also I have array of languages $langs = array(5,7,19) the same in btn_lang_id
$btn = Array
(
[0] => Array
(
[btn_id] => 1
[btn_group_id] => 1
[btn_lang_id] => 5
[btn_text] => aaa1
)
[1] => Array
(
[btn_id] => 2
[btn_group_id] => 1
[btn_lang_id] => 7
[btn_text] => bbb2
)
[2] => Array
(
[btn_id] => 3
[btn_group_id] => 1
[btn_lang_id] => 19
[btn_text] => ccc3
)
)
My question is how I can use this array to echo data by using btn_lang_id
foreach ($langs as $lang){
echo $btn[$lang['lang_id']]['btn_text'];
}
I want the above 3 arrays of $btn accessed by language id not by index 0,1,2. I there any way?
You can create an array with the "btn_lang_id" as a key using the following code:
<?php
$langs = array(5,7,19);
$btn = array(
'0' => array(
'btn_id' => 1,
'btn_group_id' => 1,
'btn_lang_id' => 5,
'btn_text' => 'aaa1',
),
'1' => array(
'btn_id' => 2,
'btn_group_id' => 1,
'btn_lang_id' => 7,
'btn_text' => 'bbb2',
),
'2' => array(
'btn_id' => 3,
'btn_group_id' => 1,
'btn_lang_id' => 19,
'btn_text' => 'ccc3',
),
);
$customArr = array();
foreach($langs as $key=>$value){
$customArr[$value] = $btn[$key];
}
print_R($customArr);
?>

How to convert a recursive multidimensional array to a straight multidimensional array in PHP?

I have search for a solution for this but couldn't find anything giving me a "straight" multidimensional array back. Flatten is probably not the solution as long as i want to preserve the original sub structure?
In additional i want to summarize qty when the key is repeating.
This is my original array:
Array
(
[60002] => Array
(
[50001] => Array
(
[50002] => Array
(
[10001] => Array
(
[flag] => B
[qty] => 1
)
[10002] => Array
(
[flag] => B
[qty] => 1
)
[10003] => Array
(
[flag] => B
[qty] => 2
)
[flag] => M
[qty] => 1
)
[flag] => M
[qty] => 1
)
[flag] => G
[qty] => 1
)
[10001] => Array
(
[flag] => B
[qty] => 1
)
)
What i basically want is to create a new array looking like this:
Array
(
[10001] => Array
(
[flag] => B
[qty] => 2
)
[10002] => Array
(
[flag] => B
[qty] => 1
)
[10003] => Array
(
[flag] => B
[qty] => 2
)
[50001] => Array
(
[flag] => M
[qty] => 1
)
[50002] => Array
(
[flag] => M
[qty] => 1
)
[60002] => Array
(
[flag] => G
[qty] => 1
)
)
This is tested.
The key is intval().
$value['qty'] += intval($newArray[$key]['qty']);
If the [$key]['qty'] does not exist the intval() will return a zero. This is much faster than using an if else to check if a [$key]['qty'] already exists.
The only possible problem I could anticipate is if the Flag value is different when the key value is the same:
[10001] => Array(
[flag] => M
[qty] => 1
),
[10001] => Array(
[flag] => B
[qty] => 1
)
When this is an issue I resolve the priority with a logic table in an array.
$priority['M']['B'] = 'M'
$priority['B']['M'] = 'M'
$priority['']['M'] = 'M'
$priority['M'][''] = 'M'
$priority['B'][''] = 'B'
$priority['B'][''] = 'B'
settype($newArray[$key]['flag'],'string');
[$newArray[$key]['flag'] = $priority[$value['flag']][$newArray[$key]['flag']]
Data:
$array = array('60002' => Array('50001' => Array('50002' => Array('10001' => Array('flag' => 'B','qty' => 1),'10002' => Array('flag' => 'B','qty' => 1),'10003' => Array('flag' => 'B','qty' => 2),'flag' => 'M','qty' => 1),'flag' => 'M','qty' => 1),'flag' => 'G','qty' => 1),'10001' => Array('flag' => 'B','qty' => 1));
PHP
$newArray = array();
getValues($data);
function getValues($array){
global $newArray;
foreach ($array as $key => $value){
if(is_numeric($value['qty'])) {
$value['qty'] += intval($newArray[$key]['qty']);
$newArray[$key] = array('flag'=>$value['flag'],'qty'=>$value['qty']);
}
if (gettype($value) != 'array'){return;}
getValues($value);
}
}
ksort($newArray);
var_export($newArray);
Result:
array (
10001 =>
array (
'flag' => 'B',
'qty' => 2,
),
10002 =>
array (
'flag' => 'B',
'qty' => 1,
),
10003 =>
array (
'flag' => 'B',
'qty' => 2,
),
50001 =>
array (
'flag' => 'M',
'qty' => 1,
),
50002 =>
array (
'flag' => 'M',
'qty' => 1,
),
60002 =>
array (
'flag' => 'G',
'qty' => 1,
),
)
This seemed to work:
function extractArray(array $source, array &$destination, $originalIndex)
{
foreach($source as $index => $value)
{
if(is_array($value))
extractArray($value, $destination, $index);
else
$destination[$originalIndex][$index] = $value;
}
}
$test = array(
60002 => array
(
50001 => array
(
50002 => array
(
10001 => array
(
'flag' => 'B',
'qty' => 1
),
10002 => array
(
'flag' => 'B',
'qty' => 1
),
10003 => array
(
'flag' => 'B',
'qty' => 2
),
'flag' => 'M',
'qty' => 1
),
'flag' => 'M',
'qty' => 1
),
'flag' => 'G',
'qty' => 1
),
10001 => array
(
'flag' => 'B',
'qty' => 1
)
);
$new = array();
foreach($test as $index => $value)
extractArray($value, $new, $index);
var_dump($new);
die();
You can use a recursive approach to iterate over all levels of the array. For each item you check that it is an array and if it has any of the keys you want checked, add the array consisting of the found attributes.
function flattenArray($array, $keysToCheck) {
$result = array();
foreach($array as $item) {
// check if the current array item is a candidate to
// be added to the flattened array
if(is_array($item)) {
$foundAttributes = array();
foreach($item as $key=>$value) {
if(in_array($key, $keysToCheck) {
$foundAttributes[$key] = $value;
}
}
// we found at least one matching attribute
if(count($foundAttributes)) {
array_push($result, $foundAttributes);
}
// recursively go to the next level and merge the results from there
$result = array_merge($result, flattenArray($item, $keysToCheck);
}
}
return $result;
}
// usage example
$flattenedArray = flattenArray($originalArray, array('flag', 'qty'));
The above solution allows you to customise it for other type of objects, by passing different $keysToCheck arguments to the function.
If you also need the flattened array sorted, you can use usort() to achieve this.
Please pardon any syntax errors, I don't have a PHP interpreter at hand.

Categories