method jsObject documentation in bbn\X

Converts the provided iterable to a json string.

Example

$arr = [ 'a' => 1, 'b' => ['c' => 2,'d' => 3], 'c' => 'let data = "{"foo":"bar"}"' ]; X::jsObject($arr); /* (string) { "a": 1, "b": { "c": 2, "d": 3 }, "c": "let data = \"{\"foo\":\"bar\"}\"" } function(iterable $obj) { $value_arr = []; $replace_keys = []; //$obj = X::convertUids($obj); $transform = function ($o, $idx = 0) use (&$transform, &$value_arr, &$replace_keys) { foreach($o as $key => &$value) { $idx++; if (\is_array($value) || \is_object($value)) { $value = $transform($value, $idx); } elseif (\is_string($value) // Look for values starting with 'function(' && (strpos(trim($value), 'function(') === 0) ) { // Store function string. $value_arr[] = $value; // Replace function string in $foo with a ‘unique’ special key. $value = "%bbn%$key%bbn%$idx%bbn%"; // Later on, we’ll look for the value, and replace it. $replace_keys[] = '"'.$value.'"'; } } return $o; }; // Now encode the array to json format $json = json_encode($transform($obj), JSON_PRETTY_PRINT); /* $json looks like: { “number”:1, “float”:1.5, “array”:[1,2], “string”:”bar”, “function”:”%bbn%function%bbn%5%bbn%” } */ // Replace the special keys with the original string. return \count($replace_keys) ? str_replace($replace_keys, $value_arr, $json) : $json; }

Converts the provided iterable to a json string. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way

This website uses cookies to ensure you get the best experience on our website.