How to convert PHP array in diffrent kind of json objects?

JSON objects are used everywhere and very easy to implement.
Thanks to PHP it has inbuilt function but if you want different kind of json object which is possible based on requirement. In my case i wanted to use
var availableTags = [{“key”:1,”value”:”AERO 1″},{“key”:2,”value”:”AERO 3″},{“key”:3,”value”:”AIR CANADA 4″},{“key”:6,”value”:”AIR CANADA 26″},{“key”:7,”value”:”AIR CANADA JAZZzzz”}];

if have key value pair of array json_encode($yourArray) will not return
above format!

Well that was my requirement, yours could be different but here is the solution.
You can tweak little bit and ready to go!

 $value)
{
   if(empty($key) && empty($value))
   {
	  continue;
    }
	$temAccNameArray[] = array('key' => $key,'value'=>$value);
}
echo json_encode($temAccNameArray);
?>;
Share

Leave a Reply

Your email address will not be published. Required fields are marked *