php array to object

Php dizi nesnelerini object nesnelerine dönüştürmek için aşağıdaki fonksiyonu kullanabilirisniz.
Dizi (array)

$dizim['cicekler']
$dizim['agaclar']

Nesne (object)

$dizim->cicekler
$dizim->agaclar

Diziyi Nesneye çevirme (array_to_object)


function array_to_object($array = array()) {
if (!empty($array)) {
$data = false;
foreach ($array as $akey => $aval) {
$data -> {$akey} = $aval;
}
return $data;
}
return false;
}
Facebook Twitter Friendfeed

2 Responses to “php array to object”

  1. Teşekkür ederim
    basit hatalar veriyordu
    aşağıdaki şekilde kullanılabilir

    $dizim['cicekler']=”ccccccc”;
    $dizim['agaclar']=”sssssss”;

    function array_to_object($array = array()) {
    if (!empty($array)) {
    $data = (object)false;
    foreach ($array as $akey => $aval) {
    $data->$akey = $aval;
    }
    return $data;
    }
    return false;
    }

    echo array_to_object($dizim)->cicekler; //BASİT KULLANIMI

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>