Combine value array in php - 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'];
}

Related

sum of unique values in array

I have below array $billitems_taxes
[0] => Array
(
[id] => 1
[tax_name] => A
[tax_value] => 12
)
[1] => Array
(
[id] => 2
[tax_name] => A
[tax_value] => 8
)
[2] => Array
(
[id] => 3
[tax_name] => B
[tax_value] => 12
)
and I want output as below, find two common tax_name and do some of same and then create a new array.
[0] => Array
(
[id] => 1
[tax_name] => A
[tax_value] => 20
)
[1] => Array
(
[id] => 3
[tax_name] => B
[tax_value] => 12
)
I tried with below code, but it did not return a correct array.
$return_array = [];
foreach($billitems_taxes as $b)
{
$return_array['tax_name'] = $b->tax_name;
$return_array['tax_value'] += $b->tax_value;
}
First off, you have an array of arrays, not objects.
Then your loop needs to know if it has already seen a this tax name which will already be in the new array to check that I used array_key_exists()
$return_array = [];
foreach($billitems_taxes as $b)
{
if ( array_key_exists($b['tax_name'], $return_array) ) {
$return_array[$b['tax_name']]['tax_value'] += $b['tax_value'];
} else {
$return_array[$b['tax_name']] = $b;
}
}
RESULT
Array(
[A] => Array
([id] => 1
[tax_name] => A
[tax_value] => 20
)
[B] => Array
([id] => 3
[tax_name] => B
[tax_value] => 12
)
)
And if its important for the array to be numerically indexed just add
$return_array = array_values($return_array);
after the end of the loop
You must group by 'tax_name' and must sum 'tax_value'.
$billitems_taxes = [
['id' => 1, 'tax_name' => 'A', 'tax_value' => 12],
['id' => 2, 'tax_name' => 'A', 'tax_value' => 8],
['id' => 3, 'tax_name' => 'B', 'tax_value' => 12]
];
$result = [];
foreach($billitems_taxes as $row){
$groupKey = $row['tax_name'];
if(array_key_exists($groupKey,$result)){
$result[$groupKey]['tax_value'] += $row['tax_value'];
} else {
$result[$groupKey] = $row;
}
}
$result = array_values($result);
echo '<pre>';
var_export($result);
/*
array (
0 =>
array (
'id' => 1,
'tax_name' => 'A',
'tax_value' => 20,
),
1 =>
array (
'id' => 3,
'tax_name' => 'B',
'tax_value' => 12,
),
)
*/
The solution with the external class tableArray is very simple. The result is the same.
$result = tableArray::create($billitems_taxes)
->filterGroupAggregate(['tax_value' => 'SUM'],['tax_name'])
->fetchAll()
;

Merge arrays together [duplicate]

This question already has answers here:
PHP - merging arrays
(2 answers)
Closed 7 years ago.
I have these two arrays and I want to combine them into one. Using the duplicate values from 0,1 in the second array. Below is an example of how I would want it to look. I hope someone can help.
Array(
[201500001] => Array
(
[0] => 1003123603
[1] => 3062226597
)
[201500002] => Array
(
[0] => 3067005512
)
)
Array(
[1127893457] => Array
(
[0] => 1003123603
[1] =>
)
[1127893467] => Array
(
[0] => 1003133106
[1] => 3067005512
)
[1127893443] => Array
(
[0] => 1004146393
[1] => 3062226597
)
[1127893246] => Array
(
[0] => 1003154423
[1] => 5149282937
)
)
Expected output:
Array(
[1127893457] => Array
(
[0] => 1003123603
[1] =>
[2] => 201500001
)
[1127893467] => Array
(
[0] => 1003133106
[1] => 3067005512
[2] => 201500002
)
[1127893443] => Array
(
[0] => 1004146393
[1] => 3062226597
[2] => 201500001
)
[1127893246] => Array
(
[0] => 1003154423
[1] => 5149282937
[2] =>
)
)
I rewrote your various arrays as valid arrays. Other than that, I think that what you are looking for is to use array_intersect. This works for me:
$source = array(
201500001 => array(
0 => 1003123603,
1 => 3062226597
),
201500002 => array(
0 => 3067005512
)
);
$target = array(
1127893457 => array(
0 => 1003123603
),
1127893467 => array(
0 => 1003133106,
1 => 3067005512
),
1127893443 => array(
0 => 1004146393,
1 => 3062226597
),
1127893246 => array(
0 => 1003154423,
1 => 5149282937
)
);
$expected = array(
1127893457 => array(
0 => 1003123603,
2 => 201500001
),
1127893467 => array(
0 => 1003133106,
1 => 3067005512,
2 => 201500002
),
1127893443 => array(
0 => 1004146393,
1 => 3062226597,
2 => 201500001
),
1127893246 => array(
0 => 1003154423,
1 => 5149282937
)
);
$newArray = [];
foreach ($target as $targetKey => $targetValue) {
foreach ($source as $sourceKey => $sourceValue) {
if (array_intersect($sourceValue, $targetValue)) {
$targetValue[2] = $sourceKey;
}
$newArray[$targetKey] = $targetValue;
}
}
echo ($newArray === $expected) ? "Match!" : "Miss." . PHP_EOL;
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

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

Create new array depending on key

My array is like that:
Array
(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
)
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
[2] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[3] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
I want to change that array like that depending on key['type']. If array key['type'] are same, I want to change array like that:
Array
(
[car] => Array(
[0]=>Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
),
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
),
[train]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[cruise]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
)
)
what I mean is that if key['type'] is car, I want to create car array or if the type is train I want to create train array or if the type is cruise I want to create cruise array. I don't know how to loop the array. Anyone please help me. Thanks a lot!
Here's a simple way to do it: loop over the data, and just append to the subarray matching the type value:
// starting data
$starting_array = array (
0 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 1,
'tran_name' => 'private',
'tran_image' => '1251961905A1.jpg',
'type' => 'car',
'troute_id' => 10
),
1 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 2,
'tran_name' => 'express',
'tran_image' => 'bus3.jpg',
'type' => 'car',
'troute_id' => 13
),
2 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 3,
'tran_name' => 'MyanmarTrain',
'tran_image' => 'Burma-Gorteikviaduct.jpg',
'type' => 'train',
'troute_id' => 16
),
3 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 4,
'tran_name' => 'Ayeyarwaddy Cruise',
'tran_image' => 'boat-ChutzpahToo1.jpg',
'type' => 'cruise',
'troute_id' => 22
)
);
// initialize the result array
$result = array();
// loop over the starting array
foreach($starting_array as $entry) {
// make sure the result array has a key matching this item's type
if(!array_key_exists($entry['type'], $result)) {
$result[ $entry['type'] ] = array();
}
// add this item to the result array
$result[ $entry['type'] ][] = $entry;
}
// this is just for testing, so you can verify the output matches your desired result
echo "<pre>";
var_dump($result);
echo "</pre>";
Try this:
<?php
$tempArr = Array
(
Array(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 1,
"tran_name" => "private",
"tran_image" => "1251961905A1.jpg",
"type" => "car",
"troute_id" => 10
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 2,
"tran_name" => "express",
"tran_image" => "bus3.jpg",
"type" => "car",
"troute_id" => 13
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 3,
"tran_name" => "MyanmarTrain",
"tran_image" => "Burma-Gorteikviaduct.jpg",
"type" => "train",
"troute_id" => 16
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 4,
"tran_name" => "Ayeyarwaddy Cruise",
"tran_image" => "boat-ChutzpahToo1.jpg",
"type" => "cruise",
"troute_id" => 22
)
);
$resultArr = array();
foreach($tempArr as $tempKey=>$temp)
{
if(!array_key_exists($temp['type'], $resultArr))
{
$resultArr[$temp['type']] = array();
}
$resultArr[$temp['type']][] = $temp;
}
echo '<pre>';
print_r($resultArr);
?>
This is working fine .....

show hierarchical select in a php function

I have an array like below:
$cats = array();
$cats[1] = array('id' => 1,'parent' => 0, 'title' => 'Tutorials');
$cats[2] = array('id' => 2,'parent' => 1, 'title' => 'PHP');
$cats[3] = array('id' => 3,'parent' => 2, 'title' => 'OOP');
$cats[4] = array('id' => 4,'parent' => 2, 'title' => 'Tips');
$cats[5] = array('id' => 5,'parent' => 1, 'title' => 'JavaScript');
$cats[6] = array('id' => 6,'parent' => 5, 'title' => 'Basics');
$cats[7] = array('id' => 7,'parent' => 5, 'title' => 'Frameworks');
$cats[8] = array('id' => 8,'parent' => 7, 'title' => 'jQuery');
$cats[9] = array('id' => 9,'parent' => 7, 'title' => 'MooTools');
$cats[10] = array('id' => 10,'parent' => 0, 'title' => 'News');
$cats[11] = array('id' => 11,'parent' => 10, 'title' => 'PHP');
$cats[12] = array('id' => 12,'parent' => 10, 'title' => 'Wordpress');
$cats[13] = array('id' => 13,'parent' => 0, 'title' => 'New');
and want to show it in a PHP function like this that call an function and give this array,
for example
$id=1;
builder_tree($id);
and give me bellow array ,please help me
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[title] => Tutorials
[children] => Array
(
[0] => Array
(
[id] => 2
[parent] => 1
[title] => PHP
[children] => Array
(
)
)
[1] => Array
(
[id] => 3
[parent] => 1
[title] => PHP
[children] => Array
(
)
)
[2] => Array
(
[id] => 5
[parent] => 1
[title] => JavaScript
[children] => Array
(
[0] => Array
(
[id] => 6
[parent] => 5
[title] => PHP
[children] => Array
(
)
)
[1] => Array
(
[id] => 7
[parent] => 5
[title] => PHP
[children] => Array
(
[0] => Array
(
[id] => 8
[parent] => 7
[title] => jQuery
[children] => Array
(
)
)
[1] => Array
(
[id] => 9
[parent] => 7
[title] => MooTools
[children] => Array
(
)
)
)
)
)
)
)
)
I realize this is an old question, but I was looking for something similar to what the original poster needed and came up with the following.
Hope this helps any future Googlers.
The build_tree_array() basically takes what the original poster posted above and coverts it to a hierarchical array.
function folder_has_children($rows, $id) {
foreach ($rows as $row) {
if ($row['child_of'] == $id)
return true;
}
return false;
}
function build_tree_array(&$rows, $parent = 0) {
foreach ($rows as $row) {
if ($row['child_of'] == $parent) {
$result = $row;
if ($this->folder_has_children($rows, $row['id'])) {
$result['children'] = $this->build_tree_array($rows, $row['id']);
}
$out[] = $result;
}
}
return $out;
}

Categories