2

I've got some Json being generated from PHP but I cannot seem to get it to be the format that I need.

$metas = get_post_meta( $key, '_tribe_tickets_meta', TRUE ); 
foreach ( $metas as $metakey ){
        $metaarray[] = array('seat_id' => $metakey);
    }
}
$array1 = array(
'bookings',
array($metaarray
),  
);
$jsonSave = json_encode($array1);

This gives me this output

["bookings",[[{"seat_id":"C_22"},{"seat_id":"BB_10"}]]]

What I am trying to achieve is this.

{"bookings":[{"seat_id":"C_22"}, {"seat_id":"BB_10"}]}

Thanks in advance for help, I'm entirely new to Json.

3 Answers 3

2

then you need to use associative array, like:

$array1 = array('bookings' => $metaarray);

when using associative array, you will get the right json formating.

Sign up to request clarification or add additional context in comments.

Comments

0

Replace:

$array1 = array('bookings',array($metaarray));

With the following

$array1 = ['bookings'=>$metaarray];

Comments

0

You can do it like this as well

$array1['bookings']=array($metaarray);
$jsonSave = json_encode($array1);

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.