Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array represent by $details_sort.
I want to use my array in foreach loop.
like foreach (`$details_sort` as $detail)
{
}
Array
(
[0] => Array
(
[id] => 1
[score] => 233
[user_id] => 4
[date] => 2014-02-03 00:00:00
)
[1] => Array
(
[id] => 2
[score] => 1256
[user_id] => 5
[date] => 2014-02-05 00:00:00
)
[2] => Array
(
[id] => 4
[score] => 123
[user_id] => 7
[date] => 2014-03-04 00:00:00
)
[3] => Array
(
[id] => 3
[score] => 100
)
[4] => Array
(
[id] => 5
[score] => 8
[user_id] => 2
[date] => 2014-03-13 00:00:00
)
)
foreach($details_sort as $detail) {
foreach($detail as $attribute => $value) {
echo "$attribute => $value";
}
}
or
foreach($details_sort as $detail) {
echo $detail['id'];
echo $detail['score'];
echo $detail['userid'];
echo $detail['date'];
}
This really basic of php. you should have some efforts.
see php foreach
foreach ($details_sort as $detail)
{
echo $detail['id'];
echo $detail['score'];
echo $detail['user_id'];
echo $detail['date'];
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Array output generated from database values.
Array ( [0] => Array ( [seldate] => 2019-04-28 [count] => 268 )
[1] => Array ( [seldate] => 2019-04-29 [count] => 366 )
[2] => Array ( [seldate] => 2019-04-30 [count] => 85 )
[3] => Array ( [seldate] => 2019-04-28 [count] => 93 )
[4] => Array ( [seldate] => 2019-04-29 [count] => 82 )
[5] => Array ( [seldate] => 2019-04-30 [count] => 31 )
[6] => Array ( [seldate] => 2019-04-28 [count] => 44 )
[7] => Array ( [seldate] => 2019-04-29 [count] => 44 )
[8] => Array ( [seldate] => 2019-04-30 [count] => 22 ) )
I need to create below
string output from above array for google LineChart.
"['2019-04-28', 268, 93, 44],
['2019-04-29', 366, 82, 44],
['2019-04-30', 85, 31, 22]"
Please help to create PHP Code logic.
First prepare your array as key being the date and the values being the values.
Then loop it again and write the lines to a new array with the imploded values.
Lastly output the lines with implode on comma and new line.
foreach($rows as $r){
$dates[$r['seldate']][] = $r['count'];
}
foreach($dates as $date => $vals){
$lines[] = "['" . $date . "', " . implode(", ", $vals) . "]";
}
echo implode(",\n", $lines);
Output:
['2019-04-28', 268, 93],
['2019-04-29', 366],
['2019-04-30', 85]
https://3v4l.org/14smE
You can traverse array by using array_walk as below
$result = [];
array_walk($arr, function($item) use(&$result){
if(empty($result[$item['seldate']])){ // if empty add date and count for initialisation
$result[$item['seldate']] = [$item['seldate'], $item['count']];
}else{
$result[$item['seldate']][] = $item['count']; // just append value to same key as date as we are grouping it by date
}
});
$result = array_values($result); // to remove date keys
print_r($result);
Output
Array
(
[0] => Array
(
[0] => 2019-04-28
[1] => 268
[2] => 93
[3] => 44
)
[1] => Array
(
[0] => 2019-04-29
[1] => 366
[2] => 82
[3] => 44
)
[2] => Array
(
[0] => 2019-04-30
[1] => 85
[2] => 31
[3] => 22
)
)
Demo
Note: If you want to use this data for linechart, json_encode above output.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have this array (much more longer - only part)
[366] => Array
(
[0] => 8
[1] => 7
[2] => 8
[3] => 9
[4] => 8
)
[367] => Array
(
[0] => 8
[1] => 9
[2] => 9
[3] => 10
[4] => 10
[5] => 9
[6] => 8
[7] => 9
[8] => 10
[9] => 10
[10] => 11
[11] => 10
[12] => 11
[13] => 11
[14] => 10
[15] => 9
[16] => 10
[17] => 8
[18] => 10
)
The keys must be hold (there are an id). I want to sum the values foreach (in this example the values are generations). So Generation 8 is count 3 times for 366.
Expect is for id 366: 7 - 1x, 8 - 3x, 9 - 1x or
[366] => Array
(
[7] => 1
[8] => 3
[9] => 1
)
[367] => Array
(
[8] => 3
[9] => 5
[10] => 8
[11] => 3
)
Every suggestion is welcomed and appreciated!
I´m always stopped with the numeric keys or the foreach ... one of code i´ve tried:
$outdams = array();
foreach ($dams as $key => $value) {
foreach ($value as $key2 => $value2) {
$index = $value2;
if (array_key_exists($index, $outdams)) {
$outdams[$index]++;
} else {
$outdams[$index] = 1;
}
}
}
check this code
<?php
$data = array(366=>array(8,7,8,8,9),
367=>array(8,9,13,14,17,14));
echo "<pre>"; print_r($data);
$response = array();
foreach ($data as $outerkey => $outerData){
foreach ($outerData as $innerkey => $innerData){
//echo "<pre>"; print_r($innerData);
if(array_key_exists($innerData, $response[$outerkey])){
$response[$outerkey][$innerData] ++;
}else{
$response[$outerkey][$innerData] = 1;
}
}
}
echo "<pre>"; print_r($response);
?>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need array merge with value concat if array title is same name. my array print is->
Array
(
[0] => Array
(
[id] => 7867867
[title] => Title1
)
[1] => Array
(
[id] => 3452342
[title] => Title2
)
[2] => Array
(
[id] => 1231233
[title] => Title2
)
[3] => Array
(
[id] => 5867867
[title] => Title1
)
[4] => Array
(
[id] => 7867777
[title] => Title1
)
)
and i want to this format like if title is same concat the array value in one array and other array is removing.
like that->
Array
(
[0] => Array
(
[id] => 7867867,5867867,7867777
[title] => Title1,Title1,Title1
)
[1] => Array
(
[id] => 3452342,1231233
[title] => Title2,Title2
)
)
If you know that how to solve it please help me!
Thanks
Try this,
foreach($array as $val)
{
$titlearray[] = $val['title'];
}
$titlearray = (array_unique($titlearray));
//print_r($titlearray);
foreach($array as $val)
{
$key = array_search($val['title'], $titlearray);
$newarray[$key]['id'][] = $val['id'];
$newarray[$key]['title'][] = $val['title'];
}
DEMO
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Array
(
[0] => Array
(
[religion] => Christian
[emp_count] => 4
[b_name] => tripura
[branch_id] => 7
)
[1] => Array
(
[religion] => Christian
[emp_count] => 2
[b_name] => Tirunelveli
[branch_id] => 10
)
[2] => Array
(
[religion] => Christian
[emp_count] => 4
[b_name] => Madurai
[branch_id] => 12
)
[3] => Array
(
[religion] => Hindu
[emp_count] => 3
[b_name] => tripura
[branch_id] => 7
)
[4] => Array
(
[religion] => Hindu
[emp_count] => 4
[b_name] => Tirunelveli
[branch_id] => 10
)
[5] => Array
(
[religion] =>
[emp_count] => 0
[b_name] =>
[branch_id] =>
)
[6] => Array
(
[religion] =>
[emp_count] => 0
[b_name] =>
[branch_id] =>
)
[7] => Array
(
[religion] => Muslim
[emp_count] => 1
[b_name] => Tirunelveli
[branch_id] => 10
)
[8] => Array
(
[religion] =>
[emp_count] => 0
[b_name] =>
[branch_id] =>
)
)
i need to group the array by the same key and value and create a json data with the grouped array.
For Eg: i need to display it as
{
name : 'Christian',
data :[4,2,4]
},
{name:'Hindu',
data:[3,2,1]
} ....
Give this a go!
Create your own array
Loop through your current array ($arrArray)
Group elements, by adding them to $arrGrouped
The code
$arrGrouped = array();
foreach($arrArray as $arrElement) {
if( array_key_exists($arrElement['religion'], $arrGrouped) ) {
$arrGrouped[$arrElement['religion']] += $arrElement['emp_count'];
} else {
$arrGrouped[$arrElement['religion']] = $arrElement['emp_count'];
}
}
echo '<pre>'. json_encode($arrGrouped, true) .'</pre>';
I would do this in two steps:
Group the same religion together and gather the counts
Map the result into the final data array
So:
$result = array_reduce($array, function(&$result, $current) {
$result[$current['religion']][] = $current['emp_count'];
return $result;
}, []);
$data = [];
foreach ($result as $religion => $emp_counts) {
$data[] = [
'name' => $religion,
'data' => $emp_counts,
];
}
echo json_encode($data);
See: array_reduce() json_encode()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have an php array
Array
(
[results] => Array
(
[0] => Array
(
[birthday] => 11-Apr-2014
[category] => Array
(
[0] => 204
[1] => 300
[2] => 304
)
[city] => Dhaka
[country] => Bangladesh
[email] => javaorjava#gmail.com
[fullName] => biplob
[gender] => Male
[inspirational] => Run to dream
[phone] => aapbd1
[photo] => Array
(
[__type] => File
[name] => 8bef9bc3-ee64-45df-9698-0466e255c1bd-profilePhoto.jpg
[url] => http://files.com/c2dcf728-e2a3-4b2d-a8c8-6ec9b3c6502a/8bef9bc3-ee64-45df-9698-0466e255c1bd-profilePhoto.jpg
)
[username] => aapbd
[website] => http://ss.com
[createdAt] => 2014-04-10T19:01:16.396Z
[updatedAt] => 2014-04-28T07:36:18.459Z
[objectId] => IQSCdXE2hI
)
[1] => Array
(
[birthday] => 09-Apr-1982
[category] => Array
(
[0] => 204
[1] => 307
[2] => 311
[3] => 313
[4] => 102
[5] => 103
[6] => 105
[7] => 107
)
[city] => Madrid
[country] => Spain
[coverPhoto] => Array
(
[__type] => File
[name] => aa53cf65-47af-464d-aa49-88202f91388f-coverPhoto.jpg
[url] => http://files.com/c2dcf728-e2a3-4b2d-a8c8-6ec9b3c6502a/aa53cf65-47af-464d-aa49-88202f91388f-coverPhoto.jpg
)
[description] => a lazy man
[email] => skio#yahoo.com
[fullName] => Sjun
[gender] => Male
[inspirational] => Honesty is the best policy
[phone] => 135469
[photo] => Array
(
[__type] => File
[name] => a1aec283-f3c7-484c-a8b2-a0b09c5f3023-profilePhoto.jpg
[url] => http://files.com/c2dcf728-e2a3-4b2d-a8c8-6ec9b3c6502a/a1aec283-f3c7-484c-a8b2-a0b09c5f3023-profilePhoto.jpg
)
[username] => asa
[website] =>
[createdAt] => 2014-04-09T07:58:19.043Z
[updatedAt] => 2014-05-07T11:13:40.671Z
[objectId] => iVb6olefaT
)
)
)
I'm trying to practice/learn foreach loops in PHP. I understand basic foreach. But I struggle with multi-dimensionals.
I want to get my every array birthday,photo name,photo url,category.
But I cant retrieve those correctly with foreach loop
Use a simple foreach() for iterating over MD arrays..
foreach($yourarray['results'] as $k=>$arr)
{
echo $arr['birthday'];
echo $arr['photo']['name'];
echo $arr['photo']['url'];
}
Let's say your array is $data,
foreach ($data['results'] as $key => $value)
echo $value['birthday'];
echo $value['photo']['name'];
echo $value['photo']['url'];
}
use $key as index value.
This is how you can access your nested array, regardless the amount of categories:
// Loop over all elements of main array element
foreach($array['results'] as $arr_key => $arr_value) {
// Loop over children
foreach($arr_value as $key => $value) {
// Element is array (like 'category' or 'photo')
if(is_array($value)) {
foreach($value as $sub_key => $sub_value) {
echo $key."[".$sub_key."] = ".$sub_value;
}
// Element is no array
} else {
echo $key." = ".$value;
}
}
}
(Example)