Skip to content

Instantly share code, notes, and snippets.

@umidjons
Last active February 13, 2022 11:10
Show Gist options
  • Save umidjons/6349938 to your computer and use it in GitHub Desktop.
Save umidjons/6349938 to your computer and use it in GitHub Desktop.
Read JSON string from file, convert PHP stdClass object to array
<?php
// file_get_contents - get file content (JSON string in it)
// json_decode - decode JSON string to PHP object
// get_object_vars - convert PHP object to array
$data = get_object_vars( json_decode( file_get_contents( 'data.txt' ) ) );
$data = array_slice( $data, 0, 10 ); // now you can array functions
echo json_encode( $data );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment