Access MySql array by column name - php

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

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'];
}

Find the difference from a multi-dimencional array to a normal array in php

I have 2 arrays one contains just a list of email address and the other contains emails and names.
The list of emails in array1 are stored in a database and the other array is a list of new and current users.
I have tried using array_diff however this only seems to work if I specify in my loop that I just want emails.
$array1 = array(
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
);
$array2 = array(
0 => array(
0 => 'tom',
1 => 'tom#tomshouse.com'
),
1 => array(
0 => 'james',
1 => 'james#jameshouse.com'
),
2 => array(
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
),
3 => array(
0 => 'jane',
1 => 'jane#janeshouse.com'
),
);
$array2emails = array();
foreach ($array2 as $item) {
$array2emails[] = $item[1];
}
$result = array_diff($array2emails, $array1);
$results gives me
Array
(
[1] => james#jameshouse.com
[2] => marvin#marvinshouse.com
[3] => jane#janeshouse.com
)
however I need to get this:
Array
(
[1] => array(
[0] => james
[1] => james#jameshouse.com
)
[2] => array(
[0] => marvin
[1] => marvin#marvinshouse.com
)
[3] => array(
[0] => jane
[1] => jane#janeshouse.com
)
)
Any help would be much appreciated. Thanks
Since the array returned by array_diff has the same keys as the original array, you can use that to get the elements of the original array back, with array_intersect_key.
$array1 = array(
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
);
$array2 = array(
0 => array(
0 => 'tom',
1 => 'tom#tomshouse.com'
),
1 => array(
0 => 'james',
1 => 'james#jameshouse.com'
),
2 => array(
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
),
3 => array(
0 => 'jane',
1 => 'jane#janeshouse.com'
),
);
$array2emails = array_column($array2, 1);
$keys = array_diff($array2emails, $array1);
$result = array_intersect_key($array2, $keys);
print_r($result);
Result:
Array
(
[1] => Array
(
[0] => james
[1] => james#jameshouse.com
)
[2] => Array
(
[0] => marvin
[1] => marvin#marvinshouse.com
)
[3] => Array
(
[0] => jane
[1] => jane#janeshouse.com
)
)
$array1 = array(
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
);
$array2 = array(
0 => array(
0 => 'tom',
1 => 'tom#tomshouse.com'
),
1 => array(
0 => 'james',
1 => 'james#jameshouse.com'
),
2 => array(
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
),
3 => array(
0 => 'jane',
1 => 'jane#janeshouse.com'
),
);
$diffs = array();
foreach ($array2 as $item) {
if (!in_array($item[1], $array1)) {
$diffs[] = $item;
}
}
Since you are using a foreach loop why not create the array there?
As long as the arrays are not to long this will word nicely. I even took the original key into the new array. So you will be able to do other comparison methods to it.
$array1 = [
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
];
$array2 = [
0 => [
0 => 'tom',
1 => 'tom#tomshouse.com'
],
1 => [
0 => 'james',
1 => 'james#jameshouse.com'
],
2 => [
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
],
3 => [
0 => 'jane',
1 => 'jane#janeshouse.com'
],
];
$arrayExits = [];
foreach ($array2 as $key => $item) {
if(in_array($item[1], $array1)) {
$arrayExits[$key] = $item;
}
}
print_r($arrayExits);
Use array_filter function.
function filterEmail($value, $key){
global $array1;
return !in_array($value[1], $array1);
}
$emailDiff = array_filter($array2, 'filterEmail' , ARRAY_FILTER_USE_BOTH );
print_r($emailDiff );

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

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

Get multiple arrays from multidimensional array based on common IDs

I have one large array and I want to group this into other arrays based on a common ID so that I can use array_splice to get only the first and last occurrence of that ID.
array(
[0] => array(id => 34, name = "walter"),
[1] => array(id => 25, name = "walter jr"),
[2] => array(id => 34, name = "saul"),
[3] => array(id => 25, name = "jesse"),
[4] => array(id => 25, name = "todd")
)
What I want to end up with is something like this.
array(
[0] => array(
id => 34, name = "walter",
id => 34, name = "saul"
),
[1] => array(
id => 25, name = "walter jr",
id => 25, name = "jesse",
id => 25, name = "todd"
)
)
I'm having a really hard time trying to wrap my head around how to accomplish this and have searched all over. I've found some solutions using array_unique and array_diff but i'm never able to get the result i'm looking for.
You can use array_reduce to group array elements, see below:
$data = array(
0 => array('id' => 34, 'name' => "walter"),
1 => array('id' => 25, 'name' => "walter jr"),
2 => array('id' => 34, 'name' => "saul"),
3 => array('id' => 25, 'name' => "jesse"),
4 => array('id' => 25, 'name' => "todd")
);
$result = array_reduce($data, function ($result, $item){
if (!isset($result[$item['id']])) {
$result[$item['id']] = array();
}
$result[$item['id']][] = $item;
return $result;
}, array());
print_r(array_values($result));
and result is:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 34
[name] => walter
)
[1] => Array
(
[id] => 34
[name] => saul
)
)
[1] => Array
(
[0] => Array
(
[id] => 25
[name] => walter jr
)
[1] => Array
(
[id] => 25
[name] => jesse
)
[2] => Array
(
[id] => 25
[name] => todd
)
)
)
If you want the key 0,1,2,3,4 ... just go through the entire array and overwrite the keys.
But depending on what you are doing, use foreach to traverse the array without the key. =p
$all_array = array(
0 => array( 'id' => 34 , 'name' => "walter" ) ,
1 => array( 'id' => 25 , 'name' => "walter jr" ) ,
2 => array( 'id' => 34 , 'name' => "saul" ) ,
3 => array( 'id' => 25 , 'name' => "jesse" ) ,
4 => array( 'id' => 25 , 'name' => "todd" )
) ;
$array_sort = array() ;
foreach ( $all_array as $piece_array ) {
$array_sort[$piece_array['id']][] = $piece_array ;
}
ksort( $array_sort ) ;
var_dump( $array_sort ) ;

Categories