Solved

adjust an array to send back to an ajax call

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now