배열 stdclass <-> array 변환

· 12년 전 · 5756 · 1
stdClass -> Array 로 바꾸는 법.

<?php

function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}

if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}

?>

Array -> stdClass


<?php

function arrayToObject($d) {
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return (object) array_map(__FUNCTION__, $d);
}
else {
// Return object
return $d;
}
}

?>
|

댓글 1개

좋은 팁 감사합니다~
댓글을 작성하시려면 로그인이 필요합니다. 로그인

그누4 팁자료실

그누보드4와 관련된 팁을 여러분들과 함께 공유하세요. 나누면 즐거움이 커집니다.

+
제목 글쓴이 날짜 조회
12년 전 조회 5,583
12년 전 조회 1.2만
12년 전 조회 8,522
12년 전 조회 9,684
12년 전 조회 8,727
12년 전 조회 3,952
12년 전 조회 5,901
12년 전 조회 5,605
12년 전 조회 7,628
12년 전 조회 5,757
12년 전 조회 6,107
12년 전 조회 5,935
12년 전 조회 4,313
12년 전 조회 8,425
12년 전 조회 6,384
12년 전 조회 3,909
12년 전 조회 9,053
12년 전 조회 3,702
12년 전 조회 6,676
12년 전 조회 5,589
🐛 버그신고