If in_array on multidimensional array and put value into first array - php

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

Related

php merge arrays based on matching column name with multiple entries or duplicates

I have an array of arrays like this :
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '103',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Balance',
1 => 'ID',
),
1 =>
array (
0 => '4533',
1 => '101',
)
),
'data3' => array (
0 =>
array (
0 => 'Active',
1 => 'ID',
),
1 =>
array (
0 => 'Yes',
1 => '101',
),
2 =>
array (
0 => 'No',
1 => '103',
)
),
);
Here is the current working answer by user #Nigel Ren I have to this question here.
$store = [];
$headers = [];
foreach ( $data as $set ) {
$headerRow = array_shift($set);
// Collect all header columns
$headers = array_merge($headers, $headerRow);
foreach ( $set as $index => $list ){
// Create associative list of data so they can be combined (i.e. ID fields)
$list = array_combine($headerRow, $list);
// Use ID value as key and create if needed
if ( !isset($store[$list["ID"]]) ) {
$store[$list["ID"]] = $list;
}
else {
$store[$list["ID"]] = array_merge($store[$list["ID"]], $list);
}
}
}
$headers = array_unique($headers);
$output = [ 'output' => [$headers]];
// Create template array, so that missing fields will be set to null
$blank = array_fill_keys($headers, null);
foreach ( $store as $dataRow ) {
// Fill in the fields for this ID and then change to numeric keys
$output['output'][] = array_values(array_merge($blank, $dataRow));
}
print_r($output);
Above code works perfectly for the above given array.
Here are the new cases I need to handle:
CASE 1 :
When the data contains only one array where the ID are same :
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '101',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Balance',
1 => 'ID',
),
),
'data3' => array (
0 =>
array (
0 => 'Active',
1 => 'ID',
),
),
);
In this case Nigel's answer doesn't output both the both records for the same ID. It outputs just a single record.
Expected output when there's a single array with duplicate ID's present :
Array
(
[output] => Array
(
[0] => Array
(
[0] => ID
[1] => PinCode
[2] => Date
)
[1] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
)
[2] => Array
(
[0] => 101
[1] => 786075
[2] => 2012-09-05
)
)
)
CASE 2 :
When any of the array's other than the first array contain's entries for multiple same ID.
$data = array (
'data1' => array (
0 =>
array (
0 => 'ID',
1 => 'PinCode',
2 => 'Date',
),
1 =>
array (
0 => '101',
1 => '454075',
2 => '2012-03-03',
),
2 =>
array (
0 => '103',
1 => '786075',
2 => '2012-09-05',
),
),
'data2' => array (
0 =>
array (
0 => 'Order',
1 => 'ID',
),
1 =>
array (
0 => 'Colgate',
1 => '101',
),
2 =>
array (
0 => 'Waffles',
1 => '101',
)
),
);
Expected output in this case :
Array
(
[output] => Array
(
[0] => Array
(
[0] => ID
[1] => PinCode
[2] => Date
[3] => Order
)
[1] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
[3] => Colgate
)
[2] => Array
(
[0] => 101
[1] => 454075
[2] => 2012-03-03
[3] => Waffles
)
[3] => Array
(
[0] => 103
[1] => 786075
[2] => 2012-09-05
[3] =>
)
)
)
How do I do it?
you can get the desired output by using a loop:
$desired_array = array();
$n_data1 = count($data['data1']);
$n_data2 = count($data['data2']);
$n_data3 = count($data['data3']);
for ($i=0; $i < $n_data1; $i++) {
if ($n_data2 > $i) {
foreach ($data['data2'][$i] as $key => $value) {
if(!in_array($value,$data['data1'][$i])){
$data['data1'][$i][]=$value;
}
}
} else {
$data['data1'][$i][]= null;
}
}
for ($i=0; $i < $n_data1; $i++) {
if ($n_data3 > $i) {
foreach ($data['data3'][$i] as $key => $value) {
if(!in_array($value,$data['data1'][$i])){
$data['data1'][$i][]=$value;
}
}
} else {
$data['data1'][$i][]= null;
}
}
$desired_array = $data['data1'];

In for loop how to check current array value with all previous array in php

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);

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 insert batch codeigniter

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'
)
);
?>

PHP array merge

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..

Categories