Search
 
SCRIPT & CODE EXAMPLE
 

PHP

web api return json example in php

$option = $_GET['option'];

if ( $option == 1 ) {
    $data = [ 'a', 'b', 'c' ];
    // will encode to JSON array: ["a","b","c"]
    // accessed as example in JavaScript like: result[1] (returns "b")
} else {
    $data = [ 'name' => 'God', 'age' => -1 ];
    // will encode to JSON object: {"name":"God","age":-1}  
    // accessed as example in JavaScript like: result.name or result['name'] (returns "God")
}

header('Content-type: application/json');
echo json_encode( $data );
Comment

PREVIOUS NEXT
Code Example
Php :: laravel convert eloquent collection to collection 
Php :: What does "as" keyword mean in Laravel route ? 
Php :: laravel create new migration 
Php :: changing created_at to short date time 
Php :: call model function in controller laravel 
Php :: create storage link laravel without terminal server 
Php :: laravel checkbox checked 
Php :: how to insert date in mysql using php 
Php :: php best debugging functions 
Php :: The configuration file now needs a secret passphrase (blowfish_secret). 
Php :: register sidebar wordpress 
Php :: ile_put_contents(/opt/bitnami/apache2/htdocs/bootstrap/cache/services.php): failed to open stream: Permission denied 
Php :: laravel collection put 
Php :: codeigniter 4 limit query 
Php :: get if bowser supports webp php 
Php :: wp get post id by slug 
Php :: laravel tree category 
Php :: Displaying the category name of a custom post type 
Php :: filename php 
Php :: wordpress get order 
Php :: unique array 
Php :: phone number validation, laravel 
Php :: foreach in laravel 
Php :: php keep only digitts 
Php :: date format in wordpress post 
Php :: laravel eloquent search json column 
Php :: write test case in react native 
Php :: php realpath 
Php :: increase php memory 
Php :: searchable dropdown laravel blade 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =