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.
Related
I have multidimensional array , I just want to push the matched value into another array: Can someone guide me to get the right array as I put into last line.
$getcount= Array
(
0 => Array
(
0 => Array
(
'pickup_trip_location' => 'Bishan',
'trip_route_id' => '1',
'c' => '5'
),
1 => Array
(
'pickup_trip_location' => 'Bukit Merah',
'trip_route_id' => 1,
'c' => 4
),
2 => Array
(
'pickup_trip_location' => 'Geylang',
'trip_route_id' => '1',
'c' => '2'
),
3 => Array
(
'pickup_trip_location' => 'Kallang',
'trip_route_id' => '1',
'c' => '3',
),
) ,
1 => Array
(
0 => Array
(
'pickup_trip_location' => 'Mandai',
'trip_route_id' => '2',
'c' => '2',
),
1 => Array
(
'pickup_trip_location' => 'Queenstown',
'trip_route_id' => '2',
'c' => '3',
),
2 => Array
(
'pickup_trip_location' => 'Toa Payoh',
'trip_route_id' => '2',
'c' => '1'
),
)
);
second array:
$array1_1 = Array
(
0 => array
(
'stoppage_points' => 'Bishan,Bukit Merah,Geylang,Kallang,Toa Payoh,Ang Mo Kio',
),
1 => array
(
'stoppage_points' => 'Queenstown,Toa Payoh,Bedok,Paya Lebar,Mandai,Changi,Yishun',
)
);
$array = [];
//$String = "Bishan,Bukit Merah,Geylang,Kallang,Toa Payoh,Ang Mo Kio";
$f = 0 ;
foreach($array1_1 as $key_1 => $arr){
$one=explode(",",$arr['stoppage_points']);
$i = 0 ;
foreach ($one as $key2=> $item){
$array[$key_1][$key2] = explode(",",$item);
$i++;
}
pr($array);
//die();
foreach($getcount as $key_1 => $arr){
//echo $key_1;
$p = 0 ;
foreach($arr as $key => $arr_1){
echo $key;
$arrayval = $arr_1['pickup_trip_location'];
$c = $arr_1['c'];
$trip_route_id = $arr_1['trip_route_id'];
//echo $id =array_search($arrayval, $array[$key_1][$key][$p]);
if( array_search( $arrayval ,$array[$key_1][$key]) ) {
$array[$key]['pickup_trip_location'] = $arrayval;
$array[$key]['trip_route_id'] = $trip_route_id;
$array[$key]['count'] = $c;
}
}
$p++;
}
pr($array);
$f++;
}
I want to output like this : Getting output for first array only :
Array
(
[0] => Array
(
[0] => Bishan
[pickup_trip_location] => Bishan
[trip_route_id] => 1
[count] => 5
)
[1] => Array
(
[0] => Bukit Merah
[pickup_trip_location] => Bukit Merah
[trip_route_id] => 1
[count] => 4
)
[2] => Array
(
[0] => Geylang
[pickup_trip_location] => Geylang
[trip_route_id] => 1
[count] => 2
)
[3] => Array
(
[0] => Kallang
[pickup_trip_location] => Kallang
[trip_route_id] => 1
[count] => 3
)
[4] => Array
(
[0] => Toa Payoh
)
[5] => Array
(
[0] => Ang Mo Kio
)
)
I have tried to push value into first array but didn't get any luck
In for loop how to check current value with each and every previous value using php
my array:
In array list [prolabelpos] =>0 having three times, how to execute a [prolabelpos] =>0 only one time in for loop . how to check current array with all previous value and [prolabelpos] =>0 execute once in the for loop
Array (
[0] => Array ( [productlabel_id] => 6 [prolabelpos] => 0 )
[1] => Array ( [productlabel_id] => 5 [prolabelpos] => 6 )
[2] => Array ( [productlabel_id] => 4 [prolabelpos] => 0 )
[3] => Array ( [productlabel_id] => 3 [prolabelpos] => 5 )
[4] => Array ( [productlabel_id] => 2 [prolabelpos] => 0 )
)
my code:
<?php
$prev = null;
foreach ($result as $key => $value) {
$label_position = $value['prolabelpos'];
if ($prev != $label_position) {
echo "my code";
}
$prev = $label_position;
}
You can approach this in foreach OR array_map
$arr =
Array (
'0' => Array ( 'productlabel_id' => 6, 'prolabelpos' => 0 ),
'1' => Array ( 'productlabel_id' => 5, 'prolabelpos' => 6 ),
'2' => Array ( 'productlabel_id' => 4, 'prolabelpos' => 0 ),
'3' => Array ( 'productlabel_id' => 3, 'prolabelpos' => 5 ),
'4' => Array ( 'productlabel_id' => 2 ,'prolabelpos' => 0 )
);
$traversed = array();
foreach($arr as $value){
if(in_array($value['prolabelpos'], $traversed)){
//This has been traversed before
}else{
/* Apply your Logic */
$traversed[] = $value['prolabelpos'];
}
}
Using array_map
$arr = Array (
'0' => Array ( 'productlabel_id' => 6, 'prolabelpos' => 0 ),
'1' => Array ( 'productlabel_id' => 5, 'prolabelpos' => 6 ),
'2' => Array ( 'productlabel_id' => 4, 'prolabelpos' => 0 ),
'3' => Array ( 'productlabel_id' => 3, 'prolabelpos' => 5 ),
'4' => Array ( 'productlabel_id' => 2 ,'prolabelpos' => 0 )
);
$traversed = array();
array_map(function($v) use (&$traversed){
if(in_array($v['prolabelpos'], $traversed)){
//This has been traversed before
}else{
/* Apply your Logic */
$traversed[] = $v['prolabelpos'];
}
}, $arr);
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.
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'
)
);
?>
I have two arrays as follows:
Array I:
Array
(
[0] => Array
(
[did] => 1
[dname] => AJAy
[dsp] => 3
[dqu] => abc
[isactive] => Y
)
[1] => Array
(
[did] => 2
[dname] => Vijay
[dsp] => 4
[dqu] => abc
[isactive] => Y
)
)
Array II:
Array
(
[0] => Array
(
[recno] => 1
[dname] => AJAy
[dsp] =>
[did]=>
[dqu] =>
[isactive] => Y
)
[1] => Array
(
[recno] => 2
[dname] => Vijay
[dsp] =>
[did]=>
[dqu] =>
[isactive] => Y
)
)
I want to update values of did, dsp, dqu from array I to array II based on match value of dname , I tried with array merge its not work out for my situation, please help me
How about:
$arr1 = Array(
Array(
'did' => 1,
'dname' => 'AJAy',
'dsp' => 3,
'dqu' => 'abc',
'isactive' => 'Y',
),
Array(
'did' => 2,
'dname' => 'Vijay',
'dsp' => 4,
'dqu' => 'abc',
'isactive' => 'Y',
),
);
$arr2 = Array(
Array(
'recno' => 2,
'dname' => 'Vijay',
'dsp' => '',
'did' => '',
'dqu' => '',
'isactive' => 'Y',
),
Array(
'recno' => 1,
'dname' => 'AJAy',
'dsp' => '',
'did' => '',
'dqu' => '',
'isactive' => 'Y',
),
);
for($i1=0; $i1<count($arr1); $i1++) {
for ($i2=0; $i2<count($arr2); $i2++) {
if ($arr1[$i1]['dname'] == $arr2[$i2]['dname']) {
$arr2[$i2]['did'] = $arr1[$i1]['did'];
$arr2[$i2]['dsp'] = $arr1[$i1]['dsp'];
$arr2[$i2]['dqu'] = $arr1[$i1]['dqu'];
}
}
}
print_r($arr2);
output:
Array
(
[0] => Array
(
[recno] => 2
[dname] => Vijay
[dsp] => 4
[did] => 2
[dqu] => abc
[isactive] => Y
)
[1] => Array
(
[recno] => 1
[dname] => AJAy
[dsp] => 3
[did] => 1
[dqu] => abc
[isactive] => Y
)
)
something like this?
<?php
$a = array
(
0 => array
(
1 => 'bat'
),
1 => array
(
10 => 'hamar'
)
);
$b = array
(
0 => array
(
2 => 'bi'
),
1 => array
(
11 => 'hamaike'
)
);
$length = count($a);
$tmp = array();
for($i=0;$i<$length;$i++)
{
$tmp[$i] = array_merge($a[$i], $b[$i]);
}
print_r($tmp);
?>
edit: it's better array_merge_recursive from comments :)
Dude you can try with array_combine() ...
it will Creates an array by using one array for keys and another for its values..
but you have to give condition.for matching values..