Jump to content

Sort array into unique strings and display results..


jigsawsoul

Recommended Posts

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..


Edited by jigsawsoul
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.