Solved

adjust an array to send back to an ajax call

Posted on 2014-11-17
4
109 Views
Last Modified: 2014-11-20
hi
i have a results from a python file ( i don't know python it is from someone else) and it looks like that:

["title1" =>1442,"title2" =>16664,"title3" =>555,"title4" =>65,"title5" =>234]

i need to make this into that to show in morris chart:

        $donutvalues = array(
            array( 'label' => "air" , 'value' => 125) ,
            array( 'label' => "light" , 'value' => 40),
            array( 'label' => "rest" , 'value' => 10 )
            );
        echo json_encode($donutvalues );

Open in new window


when i return this to the ajax call it works. so i need to prepare what comes back from the python file to this sort of array.
so i need the titles to be the values of the label, and the numbers to be the values of the value.
0
Comment
Question by:derrida
  • 2
4 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40448920
There is no correlation between

["title1" =>1442,"title2" =>16664,"title3" =>555,"title4" =>65,"title5" =>234]

and the output you posted!?!?!?!
0
 
LVL 1

Author Comment

by:derrida
ID: 40449124
maybe i didn't explained myself correctly, sorry.

title1,2,3, and so on should be placed in the inner arrays where at the moment there is "air","light","rest" and so on.

the numbers should be placed in the inner arrays as the values of the value key.

i need a loop over what comes in and make it return an array like $donutvalues .

thanks for answering
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 40452894
greetings derrida, this is about taking apart the String from the python file in PHP and pushing the string pieces into an array, You can try this code below to see if it does it -

$str1 = '["title1" =>1442,"title2" =>16664,"title3" =>555,"title4" =>65,"title5" =>234]';
  // string above is what you gave

// below I take out all string content not relevant and add some ','
$order = array('[', ']','"',' ','=>');
$replace = array('', '','','',',');
$str1 = str_replace($order, $replace, $str1);

// get all of the separate pieces into an array
$array1 = explode(',', $str1);
$len = count($array1);
// create the json array next
$jAry = array();
for ($i=0; $i< $len; $i+=2) { // add an array to json ary for each title
   $jAry[] = array('label' => $array1[$i] , 'value' => (int) $array1[$i + 1]);
   }
echo json_encode($jAry);

Open in new window


ask questions if you need more info.
0
 
LVL 1

Author Closing Comment

by:derrida
ID: 40454542
full answer and well commented. so it is clear. thanks
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question