কোড
এই ফাংশন হিসাবে একই কাজ করে json_decode(json_encode($arr), false)
।
function arrayToObject(array $arr)
{
$flat = array_keys($arr) === range(0, count($arr) - 1);
$out = $flat ? [] : new \stdClass();
foreach ($arr as $key => $value) {
$temp = is_array($value) ? $this->arrayToObject($value) : $value;
if ($flat) {
$out[] = $temp;
} else {
$out->{$key} = $temp;
}
}
return $out;
}
পরীক্ষামূলক
পরীক্ষা 1: ফ্ল্যাট অ্যারে
$arr = ["a", "b", "c"];
var_export(json_decode(json_encode($arr)));
var_export($this->arrayToObject($arr));
আউটপুট:
array(
0 => 'a',
1 => 'b',
2 => 'c',
)
array(
0 => 'a',
1 => 'b',
2 => 'c',
)
পরীক্ষা 2: বস্তুর অ্যারে
$arr = [["a" => 1], ["a" => 1], ["a" => 1]];
var_export(json_decode(json_encode($arr)));
var_export($this->arrayToObject($arr));
আউটপুট:
array(
0 => stdClass::__set_state(array('a' => 1,)),
1 => stdClass::__set_state(array('a' => 1,)),
2 => stdClass::__set_state(array('a' => 1,)),
)
array(
0 => stdClass::__set_state(array('a' => 1,)),
1 => stdClass::__set_state(array('a' => 1,)),
2 => stdClass::__set_state(array('a' => 1,)),
)
পরীক্ষা 3: অবজেক্ট
$arr = ["a" => 1];
var_export(json_decode($arr));
var_export($this->arrayToObject($arr));
আউটপুট:
stdClass::__set_state(array('a' => 1,))
stdClass::__set_state(array('a' => 1,))