how to take index for this array - codeigniter

[file_1] => Array
(
[name] => Array
(
[file] => EmailAccounts-Testing-3-customers-120email-accounts.xlsx
)
[type] => Array
(
[file] => application/octet-stream
)
[tmp_name] => Array
(
[file] => D:\wamp\tmp\phpDB76.tmp
)
[error] => Array
(
[file] => 0
)
[size] => Array
(
[file] => 21505
)
)

Let's do this with Recursive:
public function array_print(){
$myArray = array(
'type'=>'Array outside 1',
'name'=>'Array outside 2',
array(
'file_name'=>'second array',
array(
'file_ext'=>'third array',
array('text'=>'Fourth array inside thrid array')
)
)
);
Print "<pre>";
$output=array();
$result=$this->recursive($myArray);
print_r($result);
//Output value has key and value. I used ## as separator
foreach ($result as $value) {
$data = explode('##',$value);
$output[$data[0]] = $data[1];
}
print_r($output);
}
function recursive($array){
global $temp_data;
if(!empty($array)){
foreach($array as $key => $value){
//If $value is an array.
if(is_array($value)){
//We need to loop through it.
return $this->recursive($value);
} else{
$temp_data[]= $key.'##'.$value;
}
}
}
return $temp_data;
}
Using this we can loop through multidimensional array and store it in Global array. $myArray is your array.

Related

Linking arrays according to common values

I have some array like
0: [id: '1',department: 'xyz',date: '10-10-2019',time: '12:50']
1: [id: '1',department: 'xyz',date: '11-10-2019',time: '10:30']
2: [id: '2',department: 'abc',date: '09-09-2019',time: '09:50']
3: [id: '2',department: 'abc',date: '07-07-2019',time: '03:20']
I want them to be merged according to the id and department
so the 0 and 1 array will be merged together and the output should be something like
0:[id: '1',department: 'xyz',[[date: '10-10-2019',time: '12:50'],[date: '11-10-2019',time: '10:30']]]
and the 2 and 3 array will be merged together and the output should be something like
1:[id: '2',department: 'abc',[[date: '09-09-2019',time: '09:50'],[date: '07-07-2019',time: '03:20']]]
how can I do it?
Try this..
$input = [
0 =>['id' => '1','department' => 'xyz','date' => '10-10-2019','time' => '12:50'],
1 => ['id' => '1','department' => 'xyz','date' => '11-10-2019','time' => '10:30'],
2 => ['id' => '2','department' => 'abc','date' => '09-09-2019','time' => '09:50'],
3 =>['id' => '2','department' => 'abc','date' => '07-07-2019','time' => '03:20']];
$output = [];
$id_array = [];
foreach ($input as $values)
{
$id_array [] = $values['id'];
}
$unique_id_array = array_unique($id_array);
foreach($input as $key => $in)
{
if(array_key_exists($key,$unique_id_array))
{
$output[] = [
'department' => $in['department'],
'id' => $in['id']
];
}
}
foreach($input as $in)
{
foreach($output as $key => $out)
{
if($out['id'] == $in['id'] && $out['department'] == $in['department'])
{
$output[$key]['date_time'][] = ['date' =>$in['date'],'time' => $in['time']];
}
}
}
print_r($output);die();
The output is going to be
Array
(
[0] => Array
(
[department] => xyz
[id] => 1
[date_time] => Array
(
[0] => Array
(
[date] => 10-10-2019
[time] => 12:50
)
[1] => Array
(
[date] => 11-10-2019
[time] => 10:30
)
)
)
[1] => Array
(
[department] => abc
[id] => 2
[date_time] => Array
(
[0] => Array
(
[date] => 09-09-2019
[time] => 09:50
)
[1] => Array
(
[date] => 07-07-2019
[time] => 03:20
)
)
)
)
Hello i think this will works fine,and it's easy and best way to achieve your output.
i tried many codes in my system with data given by you,just try this
i hope this will work for you as well.
<?php
$inputArr = [0 =>['id' => '1','department' => 'xyz','date' => '10-10-2019','time' => '12:50'],1 => ['id' => '1','department' => 'xyz','date' => '11-10-2019','time' => '10:30'],2 => ['id' => '2','department' => 'abc','date' => '09-09-2019','time' => '09:50'],3 =>['id' => '2','department' => 'abc','date' => '07-07-2019','time' => '03:20']];
print_r($inputArr);
$second_copy = $inputArr;
foreach ($inputArr as $key => $single)
{
$goalArr[$single['id']] = getRow($single['id'],$single['department'],$second_copy);
}
function getRow($id,$department,$second_copy)
{
$returnArr['id'] = $id;
$returnArr['department'] = $department;
foreach ($second_copy as $key => $single)
{
if($id == $single['id'] && $department == $single['department'])
{
$returnArr['date'][] = $single['date'];
$returnArr['time'][] = $single['time'];
}
}
return $returnArr;
}
echo "<br>Output is: <br>";
print_r($goalArr);
?>

How to create a single array using two iterating loop and than update_batch

How do I take id on every iteration from check_seeds array and add on each itteration into seeded[] array.
In more simple words, I want to take an item from the first iteration and add into the first iteration, take an item from the second iteration and add into the second iteration and so on...
Actually, on update_batch we need third parameter (primary key, index) to update array values in database rows where id from database rows matches with the id in update_batch.
$check_seeds = $this->tournament_model->get_seeds($tournament_id);
$seeds = $this->input->post('seed');
foreach ($seeds as $key => $value){
if(!empty($key) && !empty($value)){
$seeded[] = array(
'id' => (Add id here),
'tournament_id' => $tournament_id,
'stage_id' => $stage_id,
'seed_id' => $value,
'team_name' => $key,
);
$this->db->update_batch('tournament_seed', $seeded, 'id');
redirect('organizer/tournaments);
}
}
print_r($check_seeds)
Array
(
[0] => Array
(
[id] => 3
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
[1] => Array
(
[id] => 4
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
[2] => Array
(
[id] => 5
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
)
in your model function get_seeds() you can query the current max value of id as an alias and return it together with the query result:
function get_seeds($tournament_id) {
$this->db->select_max('id', 'max_id');
$this->db->where('tournament_id', $tournament_id);
$result = $this->db->get('tournament_seed');
return $result->result();
}
then in your controller's for_each() you increment that value:
$i=0;
foreach ($seeds as $key => $value){
$i++;
$seeded[] = array(
'id' => $value->max_id + $i,
//....
);
}
Codeigniter docs: Selecting Data, scroll down to select_max(), as there is no internal bookmark

How to create insert_batch array in Codeigniter

I'm trying to insert array data using insert_batch in active record as shown in below: Any ideas to prepare the array to insert using insert_batch or any other way?
$detailBill=array(
'TraineeID'=>$inputall['ID'],
'wDate'=>$inputall['wDate'],
'ACH'=>$inputall['Hour'],
'CRA'=>$inputall['Retention'],
'Amount'=>$inputall['PaybleAmt'],
'forMonth'=>$inputall['monthid']
);
$this->db->insert_batch("tbl_submit_coursefee", $detailBill);
Output:
Array
(
[TraineeID]=> Array
(
[0]=>3001
[1]=>3002
[2]=>3003
[3]=>3004
[4]=>3005
[5]=>3006
[6]=>3007
[7]=>3008
[8]=>3009
[9]=>3010
[10]=>3011
[11]=>3012
[12]=>3013
)
[wDate]=> Array
(
[0]=>
[1]=>
[2]=>
[3]=>
[4]=>
[5]=>
[6]=>
[7]=>
[8]=>
[9]=>
[10]=>
[11]=>
[12]=>
)
more field here....
)
I am assuming you want to reorganize arrays. Try this:
$a = array(
'TraineeID' => array(
3001,
3002
),
'wDate' => array(
'123',
'234'
),
'Hour' => array(
12,
13
)
);
$keys = array_keys($a);//counting outer array
if(count($keys[0]) > 0)//checking if inner array has values at all
{
$new_a = [];//initializing expecting array
foreach($a[$keys[0]] as $k => $v)
{
for($i = 0; $i < count($keys); $i++)
{
$new_a[$k][$keys[$i]] = $a[$keys[$i]][$k];
}
}
}
//echo '<pre>', var_dump($new_a);

The reason why Cannot access empty property ?

I'm new to this...help me please
$data is object
stdClass Object ( [menu_id] => 38 [menu_code] => M062 [menu_name] => BAP (RICE) [price] => Rp 6.364 [total] => 1
and $fields is array, this is not all..
Array ( [0] => Array ( [code] => menu_id [title] => ID [width] => 5 ) [1] => Array ( [code] => menu_code [title] => Kode [width] => 8 ) [2] =>
and this is my function :
function writeRowAsli($row, $startChar, $fields, $data){
$i=$startChar; $j=''; $k='';
foreach($fields as $field){
$k = $j.$i;
$this->excel->getActiveSheet()->setCellValue($k.$row, $data->$field['code']);
$last = $k;
if($i == 'Z'){
$i='A';
$j.=$i;
} else $i++;
}
$this->excel->getActiveSheet()->setCellValue($j.$i.$row, '=SUM(C'.$row.':'.$k.$row.')');
}
i know the bad line is $this->excel->getActiveSheet()->setCellValue($k.$row, $data->$field['code']);
thanks all
Change your function as below and check if its work or not:
function writeRowAsli($row, $startChar, $fields, $data){
$i=$startChar; $j=''; $k='';
$fields = array_filter($fields);
foreach($fields as $field){
$k = $j.$i;
$this->excel->getActiveSheet()->setCellValue($k.$row, $data->$field['code']);
$last = $k;
if($i == 'Z'){
$i='A';
$j.=$i;
} else $i++;
}
$this->excel->getActiveSheet()->setCellValue($j.$i.$row, '=SUM(C'.$row.':'.$k.$row.')');
}

Adding elements to array?

How can I add more than one element to my keyed array?
array_add($myArray, 'key', 'a');
array_add($myArray, 'key-2', 'b');
Is there a better way?
There is no need for any other custom function because in PHP there is a built-in function for this and it's array_merge and you can use it like this:
$myArray = array('one' => 'TheOne', 'two' => 'TheTwo');
$array = array_merge($myArray, array('three' => 'TheThree', 'four' => 'TheFour'));
print_r($array);
Output:
Array
(
[one] => TheOne
[two] => TheTwo
[three] => TheThree
[four] => TheFour
)
You can also use this:
$myArray1 = array('one' => 'TheOne', 'two' => 'TheTwo');
$myArray2 = array('three' => 'TheThree', 'four' => 'TheFour');;
$array = $myArray1 + $myArray2;
print_r($array);
Output:
Array
(
[one] => TheOne
[two] => TheTwo
[three] => TheThree
[four] => TheFour
)
I prefer:
$myArray['key'] = 'a';
$myArray['key-2'] = 'b';
But this is not really better, because you're not adding more than one in a single command.
And if you really need to add multiple, you can always create a helper:
function array_add_multiple($array, $items)
{
foreach($items as $key => $value)
{
$array = array_add($items, $key, $value);
}
return $array;
}
And use it as
$array = array_add_multiple($array, ['key' => 'a', 'key-2' => 'b']);
or, if you're not using PHP 5.4:
$array = array_add_multiple($array, array('key' => 'a', 'key-2' => 'b'));
My custom "on the fly" method:
function add_to_array($key_value)
{
$arr = [];
foreach ($key_value as $key => $value) {
$arr[] = [$key=>$value];
}
return $arr;
}
dd(add_to_array(["hello"=>"from this array","and"=>"one more time","what"=>"do you think?"]));

Resources