Returns an object out of an array. Note that only fields corresponding to the array keys will be returned.
array_to_object($array, $object)
Where:
- $array is the array to convert
- $object is the object or class to be returned
- $add_not_existing_properties is used to choose whether to add properties not defined in the class to the object
class Person {
public $name;
public $age;
}
$array = [
"name" => "MyName",
"surname" => "MySurname",
"age" => 27
];
print_r(array_to_object($array, "Person"));
echo PHP_EOL;
print_r(array_to_object($array, "Person", true));
This will print:
Person Object ( [name] => MyName [age] => 27 )
Person Object ( [name] => MyName [age] => 27 [surname] => MySurname )
Returns an object out of a json string. Note that only fields corresponding to the json keys will be returned unless you pass $add_not_existing_properties to true.
json_to_object($json, $object, $add_not_existing_properties = false)
Where:
- json is the json to convert
- $object is the object or class to be returned
- $add_not_existing_properties is used to choose whether to add properties not defined in the class to the object
class Person {
public $name;
public $age;
}
$json = '{
"name": "MyName",
"surname": "MySurname",
"age": 27
}';
print_r(array_to_object($json, "Person"));
echo PHP_EOL;
print_r(array_to_object($json, "Person", true));
This will print:
Person Object ( [name] => MyName [age] => 27 )
Person Object ( [name] => MyName [age] => 27 [surname] => MySurname )
Build a new array out of the $options one with defaults values taken from $defaults when nothing is found.
parse_args($options, $defaults)
Where:
- $options is the array to be parsed and used to build the new array
- $defaults is the array containing default values to use when no correspondence is found
$defaults = [
"name" => "MyName",
"surname" => "MySurname",
"age" => 27
];
$person = [
"name" => "MyRealName",
"surname" => "MyRealSurname"
];
print_r(parse_args($person, $defaults));
This will print:
Array ( [name] => MyRealName [surname] => MyRealSurname [age] => 27 )
Checks if the provided value is empty and, if it is, the default value will be returned. The value itself will be returned otherwise.
get($value, $default = "")
Where:
- $value is the value to be checked to be empty or not
- $default is the value to pass in case the first is empty. Defaulted to empty string
$value = "";
echo get($value, "hello");
This will print:
hello
Disposes any item.
dispose(&$item)
Where:
- $item is the item to be disposes
$array = [
"Hello",
" ",
"world"
];
dispose($array);
Returns true if the script is running in the cli or not.
is_cli()
if (is_cli())
echo "This script is running in the shell";
else
echo "This script is not running in the shell";