Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ben Edge
14,844 PointsSort array into unique strings and display results..
Overview: Sort unique string from an array and display results into a variable which can be echoed into jQuery to be displayed as a graph.
I using Wordpress with a repeater from custom post fields plugin, code below:
$repeater = get_field('treatments');
foreach( $repeater as $key => $row ) {
column_id[ $key ] = $row['treatment_name'];
}
array_multisort( $column_id, SORT_ASC, $repeater );
foreach( $repeater as $row ) {
print_r($row);
}
The print_r returns..
Array ( [treatment_name] => back pain [pain_level] => 4 )
Array ( [treatment_name] => back pain [pain_level] => 5 )
Array ( [treatment_name] => back pain [pain_level] => 7 )
Array ( [treatment_name] => back pain [pain_level] => 10 )
Array ( [treatment_name] => shoulder pain [pain_level] => 3 )
Array ( [treatment_name] => shoulder pain [pain_level] => 8 )
Array ( [treatment_name] => shoulder pain [pain_level] => 10 )
I wish to be able to sort the array data into a variable I can using within JS.
{ treatment: '1', a: 4, b: 3 },
{ treatment: '2', a: 5, b: 8 },
{ treatment: '3', a: 7, b: 10 },
{ treatment: '4', b: 10 },
A = back pain B = shoulder pain
[treatment_name] - is a text field, so I looking for every unique [treatment_name] to added for example having one treatment called 'foot ache' with the pain level of 6 would be added to the start of the list as C.
e.g.
{ treatment: '1', a: 4, b: 3, c: 6 },
{ treatment: '2', a: 5, b: 8 },
{ treatment: '3', a: 7, b: 10 },
{ treatment: '4', b: 10 },
I have gone over and over trying to work the logic out but seems my level of php is not up to par, so I thought no better place to asked then the people who got me this far..
Any questions please do let me know, and anyone will to help your a star..