Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.4.0+

Call a JavaScript Function Returning an Array

See more JavaScript Examples

Demonstrates how to call a JavaScript function that returns an array.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\Js;
use Chilkat2\JsonObject;
use Chilkat2\StringBuilder;

// ----------------------------------------------------------------------------------
// The Javascript functions called in this example are shown at the bottom of this page.
// -----------------------------------------------------------------------------------

$sbScript = new StringBuilder();
try {
    $sbScript->LoadFile('js_function_returning_array.js', 'utf-8');
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$js = new Js();

$result = new JsonObject();
$result->EmitCompact = false;

// Call Eval to add the function to the context's global object
try {
    $js->Eval($sbScript, $result);

    // ------------------------------------------------------------------------------
    // Call each function

    $funcCall = new JsonObject();

    // Create JSON specifying the function name and arguments
    // The function has no arguments, so we only specify the name.

    $funcCall->UpdateString('name', 'getDays');

    $js->CallFunction($funcCall, $result);
} catch (\Chilkat2\Exception $e) {
    // Examine the result for an exception.
    echo $result->Emit(), "\n";

    // Also examine the LastErrorText.
    echo $e->getMessage(), "\n";
    exit;
}

echo $result->Emit(), "\n";

// Output:
// {
//   "type": "array",
//   "value": [
//     "Monday",
//     "Tuesday",
//     "Wednesday",
//     "Thursday",
//     "Friday"
//   ]
// }

// Access each array value..
$count = $result->SizeOfArray('value');
$i = 0;
while ($i < $count) {
    $result->I = $i;
    echo $result->StringOf('value[i]'), "\n";
    $i = $i + 1;
}

// ------------------------------------------------------------------------------
// Call the getRange(start,end) function

$funcCall->Clear();
$funcCall->UpdateString('name', 'getRange');
$funcCall->UpdateInt('args[0]', 14);
$funcCall->UpdateInt('args[1]', 21);
$js->CallFunction($funcCall, $result);
echo $result->Emit(), "\n";

// Output:
// {
//   "type": "array",
//   "value": [
//     14,
//     15,
//     16,
//     17,
//     18,
//     19,
//     20,
//     21
//   ]
// }

$count = $result->SizeOfArray('value');
$i = 0;
while ($i < $count) {
    $result->I = $i;
    echo $result->IntOf('value[i]'), "\n";
    $i = $i + 1;
}

// ------------------------------------------------------------------------------
// Call the getEmployees() function

$funcCall->Clear();
$funcCall->UpdateString('name', 'getEmployees');
$js->CallFunction($funcCall, $result);
echo $result->Emit(), "\n";

// Output:
// {
//   "type": "array",
//   "value": [
//     {
//       "id": 101,
//       "name": "Alice",
//       "role": "Dev"
//     },
//     {
//       "id": 102,
//       "name": "Bob",
//       "role": "Manager"
//     }
//   ]
// }

$count = $result->SizeOfArray('value');
$i = 0;
while ($i < $count) {
    $result->I = $i;
    echo 'name: ', $result->StringOf('value[i].name'), "\n";
    echo 'role: ', $result->StringOf('value[i].role'), "\n";
    echo 'id: ', $result->IntOf('value[i].id'), "\n";
    echo "\n";
    $i = $i + 1;
}
JavaScript

function getDays() {
    return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
}

function getRange(start, end) {
    var arr = [];
    for (var i = start; i <= end; i++) {
        arr.push(i);
    }
    return arr;
}
// Usage: getRange(1, 5) -> [1, 2, 3, 4, 5]

function getEmployees() {
    return [
        { id: 101, name: "Alice", role: "Dev" },
        { id: 102, name: "Bob", role: "Manager" }
    ];
}