Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Sorting array into type with repeater from AFC HELP please..

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

6 Answers

Hello Ben Edge

This should get you going in the right direction.

You will want to organize you data first in PHP as an array. Then you will pass that data on to JSON using the json_encode function in PHP.

Feel free to ask questions below.

Have Fun! Hampton

Dino Kraljevic
Dino Kraljevic
16,555 Points

Hi Hampton, i know it is unrelated question but i'm wondering what color scheme you use for sublime in this video? It is looking very good.. thx

Dino Kraljevic I use Solarized Dark for most everything.

Constantine Antonakos
Constantine Antonakos
9,741 Points

var treaments = <?php echo json_encode($treatments); ?>;

I believe this is the cleanest way to transfer data from PHP to JavaScript.

Constantine Antonakos shouldnt the php code be surrounded by a JSON.parse, for it to be actually compatible with JavaScript?

Constantine Antonakos
Constantine Antonakos
9,741 Points

I believe that's the intent of json_encode: http://www.php.net/manual/en/function.json-encode.php

Taken from PHP.net: PHP implements a superset of JSON - it will also encode and decode scalar types and NULL. The JSON standard only supports these values when they are nested inside an array or an object.

Tony Li
Tony Li
976 Points

Hampton Paulk , Json format is for application easy to read/retrieve the data, I just wondering if i use a json file to store the query result from database(this result is for temporary use), and that query will request 5s/time , so it will keep updating json file again and again, if it will cause a latency on website update? or there is a better way to perform this ?

Tony Li
Tony Li
976 Points

Hampton Paulk just now failed to tag you

Tony Li this does not sound very efficient. What are you trying to accomplish overall? Are you querying your DB from PHP and then needing to process that data with JS?

Tony Li
Tony Li
976 Points

Hampton Paulk yes, I want to retrieve the data from DB and put them into a virtualization javascript, the query result are sets of data, just like the one in your sample, the JS can only have 1 input/once, so need foreach loop. working flow: query ---->getting result ----> put into json ----> read into json_object ---> waiting the loop to virtualise ---> query again ---> update json .... because of using json is consider for further develop, maybe need using it pass data to mobile application some day. but as you said, it sounds not a efficient way.