how to convert associative array to different array?
This is my array
$array=Array (
services => Array ( [0] => 6, [1] => 1, [2] => 3 ),
subservices => Array ( [0] => 'No data',[1] => 2 ,[2] => 'No data' ),
price=> Array ( [0] => 124, [1] => 789, [2] => 895 ),
);
and i want convert to
Array (
[0] => Array ( [services] => 6, [subservices] => 'No data', [price] => 124 )
[1] => Array ( [services] => 1, [subservices] => 2, [price] => 789 )
[2] => Array ( [services] => 3, [subservices] => 'No data', [price] => 895 )
)
How to do?
$outArray=array();
for($i=0;$i<count($sourceArray['services']);$i++)
{
$outArray[]=array('services'=>$sourceArray['services'][$i],'subservices'=>$sourceArray['subservices'][$i],'price'=>$sourceArray['price'][$i]);
}
Here is a dynamic approach. This will also allow for additional values in your sub arrays.
Hope it helps:
$array = array (
'services' => Array ( '0' => 6, '1' => 1, '2' => 3),
'subservices' => Array ( '0' => 'No data', '1' => 2, '2' => 'No data'),
'price' => Array ( '0' => 124, '1' => 789, '2' => 895)
);
//Get array keys.
$keys = array_keys($array);
//Iterate through the array.
for($i = 0; $i < count($array); $i++){
//Iterate through each subarray.
for($j = 0; $j < count($array[$keys[$i]]); $j++){
//Here we are checking to see if you have more data per element than your initial key count.
if($keys[$j]){
$index = $keys[$j];
} else {
$index = $j;
}
//Append results to the output array.
$results[$i][$index] = $array[$keys[$i]][$j];
}
}
echo '<pre>';
print_r($results);
echo '</pre>';
This will output:
Array
(
[0] => Array
(
[services] => 6
[subservices] => 1
[price] => 3
)
[1] => Array
(
[services] => No data
[subservices] => 2
[price] => No data
)
[2] => Array
(
[services] => 124
[subservices] => 789
[price] => 895
)
)
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
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 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);
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'
)
);
?>
$series = array();
while($row = mysql_fetch_assoc($result)) {
$series[$row["data_id"]][] = $row;
}
The output from a print_r on $series yields for two example series:
Array (
[1] => Array ( [0] => Array ( [id] => 1 [data_id] => 1 [time_id] => 1
[data] => 1 ) [1] => Array ( [id] => 2 [data_id] => 1 [time_id] => 2
[data] => 3 ) )
[2] => Array ( [0] => Array ( [id] => 6 [data_id] => 2 [time_id] => 1
[data] => 7 ) [1] => Array ( [id] => 7 [data_id] => 2 [time_id] => 2
[data] => 4 ) )
My question: how do I unset the multidimensional array so it contains only [data] and none of the other keys? I still want $series to contain [1] and [2] but I do not want the respective sub-arrays to contain any other keys other than [data].
In fact, since I am reducing the subarrays to contain a single key, I would really like to get rid of the subarrays altogether so that I have two arrays:
$series[1] = array(1,3) and
$series[2] = array(7,4)
Try this :
$series = array();
while($row = mysql_fetch_assoc($result)) {
$series[$row["data_id"]][] = $row['data'];
}
I think you can loop in your array and build a new one keeping only data details
$array = array ('1' => array ( '0' => array ( 'id' => 1, 'data_id' => 1, 'time_id' => 1, 'data' => 1 ), '1' => array ( 'id' => 2, 'data_id' => 1, 'time_id' => 2, 'data' => 3 ), ),
'2' => array ( '0' => array ( 'id' => 6, 'data_id' => 2, 'time_id' => 1, 'data' => 7 ), '1' => array ( 'id' => 7, 'data_id' => 2, 'time_id' => 2, 'data' => 4 ) ));
$i= 0;
$n= 0;
$series = array();
foreach($array as $dato)
{
$series[$i] = array();
foreach($dato as $data)
{
foreach($data as $key => $value)
{
if($key == 'data')
{
$series[$i][$n] = $value;
$n++;
}
}
}
$n = 0;
$i++;
}
var_dump($series);
This will output
array (size=2)
0 =>
array (size=2)
0 => int 1
1 => int 3
1 =>
array (size=2)
0 => int 7
1 => int 4
Live demo