Solved

adjust an array to send back to an ajax call

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 38
Make checkboxes execute queries on click 8 30
Why is my $_POST not going to results page 10 32
How do I Check for duplicate entries in mysql 15 19
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

685 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