method export documentation in bbn\Str

Returns a variable in a mode that is directly usable by PHP.

Example

$myObj = new stdClass(); $myObj->myProp1 = 23; $myObj->myProp2 = "world"; $myObj->myProp3 = [1, 5, 6]; $myObj->myProp4 =""; X::hdump(\bbn\Str::export($myObj,true)); /*(string) "{ "myProp1" => 23, "myProp2" => "world", "myProp3" => [ 1, 5, 6, ], }" function($o, $remove_empty = false, $lev = 1) { $st = ''; $space = ' '; if (\is_object($o) && ($cls = \get_class($o)) && (strpos($cls, 'stdClass') === false)) { $st .= "Object ".$cls; /* $o = array_filter((array)$o, function($k) use ($cls){ if ( strpos($k, '*') === 0 ){ return false; } if ( strpos($k, $cls) === 0 ){ return false; } return true; }, ARRAY_FILTER_USE_KEY); */ } elseif (\is_object($o) || \is_array($o)) { $is_object = \is_object($o); $is_array = !$is_object && \is_array($o); $is_assoc = $is_object || ($is_array && X::isAssoc($o)); $st .= $is_assoc ? '{' : '['; $st .= PHP_EOL; foreach ($o as $k => $v){ if ($remove_empty && ( ( \is_string($v) && empty($v) ) || ( \is_array($v) && \count($v) === 0 ) )) { continue; } $st .= str_repeat($space, $lev); if ($is_assoc) { $st .= ( \is_string($k) ? '"'.self::escapeDquote($k).'"' : $k ). ': '; } if (\is_array($v)) { $st .= self::export($v, $remove_empty, $lev + 1); } elseif ($v === 0) { $st .= '0'; } elseif (null === $v) { $st .= 'null'; } elseif (\is_bool($v)) { $st .= $v === false ? 'false' : 'true'; } elseif (\is_int($v) || \is_float($v)) { $st .= $v; } elseif (is_string($v)) { if (self::isBuid($v)) { $st .= '0x'.bin2hex($v); } elseif (!$remove_empty || !empty($v)) { $st .= '"'.self::escapeDquote($v).'"'; } } else { try{ $cls = get_class($v); } catch (\Exception $e){ $st .= '"Unknown"'; } if ($cls) { if ($cls === 'stdClass') { $st .= self::export($v, $remove_empty, $lev + 1); } else{ $st .= 'Object '.$cls; } } } $st .= ','.PHP_EOL; } $st .= str_repeat($space, $lev - 1); $st .= $is_assoc ? '}' : ']'; } return $st; }

Returns a variable in a mode that is directly usable by PHP. 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.