Solved

adjust an array to send back to an ajax call

Posted on 2014-11-17
4
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

635 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