PHP | Ds\Vector jsonSerialize() Function Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The Ds\Vector::jsonSerialize() function is an inbuilt function in PHP which is used to return the element which can be converted to JSON. Syntax: mixed public JsonSerializable::jsonSerialize( void ) Parameters: This function does not accepts any parameter. Return Value: This function returns the values of the vector in the form which can be converted to JSON. Below programs illustrate the Ds\Vector::jsonSerialize() function in PHP: Program 1: PHP <?php class vector implements JsonSerializable { public function __construct(array $arr) { $this->array = $arr; } public function jsonSerialize() { return $this->array; } } // Declare an array $arr = [1, 2, 3, 4, 5]; echo("Elements after converting to JSON convertible form\n"); echo json_encode(new vector($arr), JSON_PRETTY_PRINT); ?> Output: Elements after converting to JSON convertible form [ 1, 2, 3, 4, 5 ] Program 2: PHP <?php class vector implements JsonSerializable { public function __construct(array $arr) { $this->array = $arr; } public function jsonSerialize() { return $this->array; } } // Declare an array $arr = ["geeks", "for", "geeks"]; echo("Elements after converting to JSON convertible form\n"); echo json_encode(new vector($arr), JSON_PRETTY_PRINT); ?> Output: Elements after converting to JSON convertible form [ "geeks", "for", "geeks" ] Reference: https://www.php.net/manual/en/ds-vector.jsonserialize.php Comment More infoAdvertise with us Next Article PHP | Ds\Vector insert() Function B barykrg Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-ds_vector Similar Reads PHP | Ds\Vector join() Function The Ds\Vector::join() function is an inbuilt function in PHP which is used to join all the elements of vector as a string using the separator provided as the argument. Syntax: string public Ds\Vector::join( $glue ) Parameters: This function accepts a single parameter $glue which is used to hold the 1 min read PHP | Ds\Vector join() Function The Ds\Vector::join() function is an inbuilt function in PHP which is used to join all the elements of vector as a string using the separator provided as the argument. Syntax: string public Ds\Vector::join( $glue ) Parameters: This function accepts a single parameter $glue which is used to hold the 1 min read PHP | Ds\Vector insert() Function The Ds\Vector::insert() function is an inbuilt function in PHP which is used to insert the element into the vector at the given index. Syntax: void public Ds\Vector::insert( $index, $values ) Parameters: This function accepts two parameter as mentioned above and described below: $index: This paramet 2 min read PHP | Ds\Vector insert() Function The Ds\Vector::insert() function is an inbuilt function in PHP which is used to insert the element into the vector at the given index. Syntax: void public Ds\Vector::insert( $index, $values ) Parameters: This function accepts two parameter as mentioned above and described below: $index: This paramet 2 min read PHP | Ds\Vector merge() Function The Ds\Vector::merge() function is an inbuilt function in PHP which is used to merge all the elements to the vector. Syntax: Ds\Vector public Ds\Vector::merge( $values ) Parameters: This function accepts a single parameter $values which is the traversable object or array.Return Value: This function 2 min read PHP | Ds\Vector merge() Function The Ds\Vector::merge() function is an inbuilt function in PHP which is used to merge all the elements to the vector. Syntax: Ds\Vector public Ds\Vector::merge( $values ) Parameters: This function accepts a single parameter $values which is the traversable object or array.Return Value: This function 2 min read Like