1

I would change the shape of the array into json but difficulty in parsing the data, I have an array like :

Array ( [id] => 1
        [fisrt_name] => raul
        [last_name] => gonzales
        [tgl_booking_start] => 2013-11-04 00:00:00 
        [tgl_booking_finish] => 2013-11-30 00:00:00 
      ),
      ( [id] => 2
        [fisrt_name] => chirtiano
        [last_name] => ronaldo
        [tgl_booking_start] => 2013-11-04 00:00:00 
        [tgl_booking_finish] => 2013-11-30 00:00:00 
      ) 

I want to change it into a form json like this, how do I start the code?

   [{
   "id":1,
   "title":"raul gonzales",
   "start":"2013-11-04",
   "end":"2013-11-30"
   },
   {
   "id":2,
   "title":"chirtiano ronaldo",
   "start":"2013-11-04",
   "end":"2013-11-30"
  }]

thanks for answering

4
  • 2
    What's wrong with json_encode()? Doesn't it do what you want? Commented Nov 28, 2013 at 8:28
  • What have you tried so far? Why json_encode us1.php.net/json_encode is not suitable? Commented Nov 28, 2013 at 8:28
  • You'll need to write your own loop to change the keys and combine first and last name into title, there's nothing built-in to do that. Commented Nov 28, 2013 at 8:29
  • I've tried it but I am not parsing the data associated array Commented Nov 28, 2013 at 8:30

7 Answers 7

3

Sample Link

 <?php
        $array = array ( 
              array( 'id' => '1',
                'fisrt_name' => 'raul',
                'last_name' => 'gonzales',
                'tgl_booking_start' => '2013-11-04 00:00:00' ,
                'tgl_booking_finish' => '2013-11-30 00:00:00' ,
              ),
              array( 'id' => '2',
                'fisrt_name' => 'chirtiano',
                'last_name' => 'ronaldo',
                'tgl_booking_start' => '2013-11-04 00:00:00', 
                'tgl_booking_finish' => '2013-11-30 00:00:00', 
              ) 
          );

        $new_arr = array();

        foreach($array as $arr){
                $process_array = array();
                $process_array['id'] = $arr['id'];
                $process_array['title'] = $arr['fisrt_name'].' '.$arr['last_name'];
                $process_array['start'] = $arr['tgl_booking_start'];
                $process_array['end'] =  $arr['tgl_booking_finish'];

                array_push($new_arr,$process_array);

         }

         echo json_encode($new_arr);

    ?>

output would be

[{
"id":"1",
"title":"raul gonzales",
"start":"2013-11-04 00:00:00",
"end":"2013-11-30 00:00:00"
},
{
"id":"2",
"title":"chirtiano ronaldo",
"start":"2013-11-04 00:00:00",
"end":"2013-11-30 00:00:00"
}]
Sign up to request clarification or add additional context in comments.

Comments

3

Use json_enconde($array) for convert array to json and and json_decode($json) for convert json to array.

In javascript access json elements for index example: $json['title']

Comments

2

use json_encode() See the docu for usage.

https://www.php.net/manual/en/function.json-encode.php

Comments

0

Try the following function.

json_encode($array)

Comments

0

You can use json_decode

echo json_encode($array);

Comments

0

And if you want a more powerful solution than the built-in json_encode function (mainly for object serialization), try the JmsSerializer library (used a lot in Symfony2 projects) : http://jmsyst.com/libs/serializer

Comments

0

Consider using both stripslashes and json_encode functions to avoid unexpected problems.

echo stripslashes(json_encode($array));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.