Solved

adjust an array to send back to an ajax call

Posted on 2014-11-17
4
110 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 34

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

839 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