I have an array $this->getcolumnname which have
Array
(
[0] => Array
(
[column_id] => 1
[Column_name] => Backlog
[fk_sprint_id] => 1
[fk_team_id] => 1
)
[1] => Array
(
[column_id] => 2
[Column_name] => WIP
[fk_sprint_id] => 1
[fk_team_id] => 1
)
[2] => Array
(
[column_id] => 3
[Column_name] => DOD
[fk_sprint_id] => 1
[fk_team_id] => 1
)
[3] => Array
(
[column_id] => 4
[Column_name] => COMP
[fk_sprint_id] => 1
[fk_team_id] => 1
)
[4] => Array
(
[column_id] => 5
[Column_name] => treat
[fk_sprint_id] => 1
[fk_team_id] => 1
)
[5] => Array
(
[column_id] => 6
[Column_name] => asa
[fk_sprint_id] => 1
[fk_team_id] => 1
)
[6] => Array
(
[column_id] => 7
[Column_name] => test
[fk_sprint_id] => 1
[fk_team_id] => 1
)
)
and i have another Array $this->getstories
Array
(
[0] => Array
(
[i_description] => s2
[Column_name] => Backlog
[column_id] => 1
[Tm_id] => 0
[name] =>
[spid] => 1
[fk_back_id] => 102
[u_pos_is] => 1
[s_id] => 6
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[1] => Array
(
[i_description] => s1
[Column_name] => WIP
[column_id] => 2
[Tm_id] => 0
[name] =>
[spid] => 1
[fk_back_id] => 101
[u_pos_is] => 2
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
And i have a foreach loop like
foreach ($this->getcolumnname as $columnname):
foreach ($this->getstories as $key => $restories)
<div class="panel-heading panel-bgwhite"><b><?php echo $columnname['Column_name'] ?></b>
<p><?php echo $restories['i_description']; ?></p>
</div>
endforech;
endforeach;
i got result as double.actually count is 6 but i got 12 instead .My issue is foreach inside another foreach when i print the result it is increasing,ie value is repeating .
Expected output:
I want echo '<pre>';print_r($columnname); 6 times only.And $restories 2 times only.I want to print stories under respective column name .
Please help me.Any help would be appreciated.
In a simple way you can do:
foreach ($this->getcolumnname as $columnname):
echo '<pre>';print_r($columnname);
foreach ($this->getstories as $restories):
if ($columnname['Column_name'] === $restories['Column_name']):
print_r($restories);
endif;
endforeach;
endforeach;
Related
I'm stuck on this problem and I hope someone can help me on that.
I have an array which I want to group by a specific key. The only problem is that I want to have a new array whenever the value of the key changes during the loop. Here is an example of the array.
Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[2] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
[3] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[5] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[6] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
I did
foreach($report_items as $item)
{
$groupedItems[$item['grouped_by']][] = $item;
}
and I got
Array
(
[1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
)
What I'm actually looking for is something like this, where the arrays are separated during the loop and a suffix is added to the key whenever the value changes.
Array
(
[1.1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4.1] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
[1.2] => Array
(
[0] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
)
I'm not really aware of a simple way to solve this, so I would appreciate any help. Thank you!
Not going to bother to recreate your full array here, I am using a reduced version with just the id and the grouped_by value, but the principle is of course exactly the same if you do it with your "full" array.
I am using a counter array to keep track of which "iteration" of a specific group we are currently dealing with. In classic control break implementation logic, I am comparing the grouped_by of the current record, with that of the previous one - if those differ, then the counter for this grouped_by value has to be incremented by 1. And then, I am simply creating the array key to use, by combining the grouped_by value, and the current count for it.
$data = [['id' => 972, 'grouped_by' => 1], ['id' => 664, 'grouped_by' => 4], ['id' => 631, 'grouped_by' => 4], ['id' => 630, 'grouped_by' => 1], ['id' => 971, 'grouped_by' => 1], ['id' => 973, 'grouped_by' => 1], ['id' => 974, 'grouped_by' => 1]];
$grouped_result = $grouped_by_counts = [];
$previous_grouped_by = null; // a value that will never occur in the data
foreach($data as $datum) {
if($datum['grouped_by'] !== $previous_grouped_by) {
$grouped_by_counts[$datum['grouped_by']] =
isset($grouped_by_counts[$datum['grouped_by']]) ?
$grouped_by_counts[$datum['grouped_by']] + 1 : 1;
}
$grouped_result[
$datum['grouped_by'].'.'.$grouped_by_counts[$datum['grouped_by']]
][] = $datum;
$previous_grouped_by = $datum['grouped_by'];
}
print_r($grouped_result);
Live example: https://3v4l.org/odtie
I have a query
SELECT bs.i_description,col.Column_name,bs.Tm_id,ad.UserName as name,
a.fk_sprint_id as spid,b.fk_back_id,b.u_pos_is,b.s_id,b.color,b._left,b._top,
b.wiptime as wiptime,b.dodtime as dodtime,b.Dep_status
FROM backToSprint a
JOIN backToSprint b ON a.`s_id` = b.`fk_f_id`
join backlog bs on bs.b_id=b.fk_back_id
left join admin ad on bs.Tm_id=ad.adminID
left join admin_column col on col.column_id=b.u_pos_is
where a.fk_sprint_id=3 and a.teamid=1
ORDER BY a.`fk_f_id`
Where I got output like
Array
(
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[1] => Array
(
[i_description] => Story 2
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 1
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
Here Column name is same so I expect output like
Array
(
['c'] => Array
(
[Column_name] => Backlogs
)
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[1] => Array
(
[i_description] => Story 2
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 1
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
Then i tried
$this->view->getstories['c'] = ['Column_name' => array_unique(array_column( $this->view->getstories, 'Column_name'))];
which didn't get actual output.i got output like
Array
(
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[1] => Array
(
[i_description] => Story 2
[Column_name] => WIP
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 2
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[c] => Array
(
[Column_name] => Array
(
[0] => Backlogs
[1] => WIP
)
)
)
(Here i have another column name WIP)
But i want a nested array like
Array
(
[c1] => Array
(
[Column_name] => Array
(
[0] => Backlogs
)
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
[c2] => Array
(
[Column_name] => Array
(
[0] => WIP
)
[0] => Array
(
[i_description] => Story 2
[Column_name] => WIP
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 2
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
)
Please help me to solve this issue
Any help would be appreciated.
What you want is nested arrays. Do it while fetching the rows from the query, using $row['Column_name'] as the key of the main array, and pushing each row onto that nested array.
$results = [];
while ($row = $stmt->fetch()) {
$results[$row['Column_name']][] = $row;
}
This will create a result like:
Array
(
[Backlogs] => Array
(
[0] => Array
(
[i_description] => Story 1
[Column_name] => Backlogs
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 408
[u_pos_is] => 1
[s_id] => 5
[color] => 2
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
[WIP] => Array
(
[0] => Array
(
[i_description] => Story 2
[Column_name] => WIP
[Tm_id] => 0
[name] =>
[spid] => 3
[fk_back_id] => 409
[u_pos_is] => 2
[s_id] => 6
[color] => 3
[_left] => 18
[_top] => -9
[wiptime] =>
[dodtime] =>
[Dep_status] => 0
)
)
)
)
There's no need for the c1 and c2 keys, just use the Column_name values as the keys of the main array.
The $snippet variable has the following value:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 2
[title] => Title
[detail] => null
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 02:40:03
)
[1] => Array
(
[id] => 2
[user_id] => 2
[title] => Some other title
[detail] => null
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 02:40:03
)
[2] => Array
(
[id] => 3
[user_id] => 2
[title] => Huh!
[detail] => ...
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 03:04:48
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 2
[title] => Title
[detail] => null
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 02:40:03
)
[1] => Array
(
[id] => 2
[user_id] => 2
[title] => Some other title
[detail] => null
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 02:40:03
)
[2] => Array
(
[id] => 3
[user_id] => 2
[title] => Huh!
[detail] => ...
[lang_id] => 19
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 03:04:48
)
)
[2] => Array
(
[0] => Array
(
[id] => 4
[user_id] => 2
[title] => apache
[detail] => ...
[lang_id] => 1
[edited] => false
[last_edited] =>
[created_date] => 2016-09-25 03:05:06
)
)
)
I wanted my result to be something like:
Title
Some other title
Huh!
Title
Some other title
Huh!
apache
So I tried the following:
<?php foreach ($snippet as $key => $value): ?>
<?= $value[$key]['title'] ?>
<br />
<?php endforeach ?>
But the results were:
Title
Some other title
Notice: Undefined offset: 2
I can't figure out what I'm doing wrong.
Try this:
<?php
foreach ($snippet as $value):
foreach($value as $title):
echo $title['title']."<br>";
endforeach;
endforeach;
?>
Using the array_column() you can print your title's with the help of implode(). This is based on your try, cause you try with one foreach().
<?php
foreach ($snippet as $key => $value):
echo implode("<br/>", array_column($value[$key], 'title'));
endforeach;
?>
Just try this and let me know is it works or not.
I have following two different array
First array:
Array
(
[0] => stdClass Object
(
[course_id] => 21
[session_id] => 17
[course_name] => Session 1 Course 1
[course_description] => Session 1 Course 1
[course_days] => 2
[current_rate] => 1000
[old_rate] =>
[primary_tutor_id] => 18
[secondary_tutor_id] => 25
[additional_time] =>
[location] =>
[course_active] => 1
)
[1] => stdClass Object
(
[course_id] => 22
[session_id] => 17
[course_name] => Session 1 Course 2
[course_description] => Session 1 Course 2
[course_days] => 3
[current_rate] => 1000
[old_rate] =>
[primary_tutor_id] => 24
[secondary_tutor_id] => 25
[additional_time] =>
[location] =>
[course_active] => 1
)
[2] => stdClass Object
(
[course_id] => 23
[session_id] => 17
[course_name] => Session 1 Course 3
[course_description] => Session 1 Course 3
[course_days] => 5
[current_rate] => 2000
[old_rate] =>
[primary_tutor_id] => 26
[secondary_tutor_id] => 27
[additional_time] =>
[location] =>
[course_active] => 1
)
[3] => stdClass Object
(
[course_id] => 26
[session_id] => 19
[course_name] => Session 3 Course 2
[course_description] => Session 3 Course 2
[course_days] => 2
[current_rate] => 400
[old_rate] =>
[primary_tutor_id] => 29
[secondary_tutor_id] => 29
[additional_time] =>
[location] =>
[course_active] => 1
)
[4] => stdClass Object
(
[course_id] => 27
[session_id] => 19
[course_name] => Session 3 Course 3
[course_description] => Session 3 Course 3
[course_days] => 1
[current_rate] => 200
[old_rate] =>
[primary_tutor_id] => 26
[secondary_tutor_id] =>
[additional_time] =>
[location] =>
[course_active] => 1
)
)
Second Array:
Array
(
[0] => stdClass Object
(
[discount_id] => 104
[session_id] => 17
[no_course] => 3
[discount] => 20
)
[1] => stdClass Object
(
[discount_id] => 106
[session_id] => 19
[no_course] => 2
[discount] => 20
)
)
I am trying to combine second array all key and value into first array like following example
Array
(
[0] => stdClass Object
(
[course_id] => 21
[session_id] => 17
[course_name] => Session 1 Course 1
[course_description] => Session 1 Course 1
[course_days] => 2
[current_rate] => 1000
[old_rate] =>
[primary_tutor_id] => 18
[secondary_tutor_id] => 25
[additional_time] =>
[location] =>
[course_active] => 1
[discount_id] => 104
[session_id] => 17
[no_course] => 3
[discount] => 20
)
)
I have have tried following code and many other code but i cant get perfect solution. In this two array there is one exception if both [session_id] is same than need array merging other wise as it is array with out merging :
$main=array();
foreach ($courses as $key => $val) {
foreach ($session_discount as $se) {
if ($se->session_id == $val->session_id)
$main[$key] = $val;
array_push($main[$key], $session_discount[0]);
}
}
Need help..! Thanks in Advance.
I fetch data of particular table by stored procedure ,demo code is
Array
(
[0] => Array
(
[object_types] => Array
(
[ID] => 11
[Code] => Item001
[Name] => Item
[Description] => Items
[DisplayName] => Items
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] =>
[DefaultNarration] =>
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-02 00:00:00
[ModifiedDate] => 2014-04-08 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] =>
[IsDelete] => 1
)
)
[1] => Array
(
[object_types] => Array
(
[ID] => 12
[Code] => Uom001
[Name] => Uom
[Description] => Uom
[DisplayName] => Uom
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] => 1
[DefaultNarration] => 1
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-02 00:00:00
[ModifiedDate] => 2014-04-02 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] => 1
[IsDelete] => 1
)
)
[2] => Array
(
[object_types] => Array
(
[ID] => 13
[Code] => Role
[Name] => Role
[Description] => Role
[DisplayName] => Role
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] => 1
[DefaultNarration] =>
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-03 00:00:00
[ModifiedDate] => 2014-04-03 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] =>
[IsDelete] => 1
)
)
[3] => Array
(
[object_types] => Array
(
[ID] => 14
[Code] => User
[Name] => User
[Description] => Use
[DisplayName] => User
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] => 1
[DefaultNarration] => 71
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-03 00:00:00
[ModifiedDate] => 2014-04-09 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] =>
[IsDelete] => 1
)
)
[4] => Array
(
[object_types] => Array
(
[ID] => 15
[Code] => AccountMaster
[Name] => AccountMaster
[Description] => AccountMaster
[DisplayName] => Account
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] => 1
[DefaultNarration] => 1
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-05 00:00:00
[ModifiedDate] => 2014-04-05 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] =>
[IsDelete] =>
)
)
[5] => Array
(
[object_types] => Array
(
[ID] => 16
[Code] => Contact
[Name] => Contact
[Description] => Contact
[DisplayName] => Contact
[ObjectTypeIdentifier] => 1
[CheckPermissions] => 1
[DefaultLedgerType_002] => 103
[DefaultNarration] => 71
[CopyTaxesFromParent] => 1
[CreatedBy] => 1
[ModifiedBy] => 1
[CreatedDate] => 2014-04-07 00:00:00
[ModifiedDate] => 2014-04-08 00:00:00
[RevisionNumber] => 1
[IsAdd] => 1
[IsEdit] => 1
[IsDelete] => 1
)
)
)
I want to retrun all data without foreach looping statement.
$data = array();
foreach($modelData as $row){
$data[] = $row[$model->name];
}
return $data;
I didnt want to use this foreach loop. so please suggest me appropriate solution.
You can use array_map function
do something like :
$data = array_map(function($model){
return $model['object_type']['Name'];
},$modelData);
the code is not tested, but it should be something like this
Based on what your var_dump response was to the other solution, try :
$data = array_map(function($model){
return $model['object_type']['Name']['text'];
},$modelData);
How about JSON? You can get a string from array.
http://www.php.net/manual/en/function.json-encode.php
I'm not sure if this is what you wanted but in case it is, I tried doing the same thing, to get an array without the alias like: $array['first_name'] instead of $array['User']['first_name'] in order to print it in JSON format, but unfortunatelly there is no way to natively do that, so I sticked to the foreach
If not foreach, then try for because to iterate arrays you have to use some loops and the fastest is for as below
$data = array();
$size = sizeof($modelData);
for($i=0; $i<=$size; $i++)
{
$data[] = $modelData[$i][$model->name];
}
return $data;
try
$final_array = array_map(
function ($a) {
return $a['object_types']['Name'];
},
$modelData
);
print_r($final_array);