Stdclass object to array php codeigniter

i'm using codeigniter, here's my view :

           

and here's app_model -> get_promo_air()

function get_promo_air()
{
    $sql = "
                SELECT
                    c.PRO_PRICE_CODE,
                    a.PRO_MST_NM,
                    c.PRO_AMT_CUST
                FROM
                    promo_master a
                inner join  promo_type b on a.pro_type_id = b.pro_type_id
                inner join  promo_price c on a.PRO_MST_ID = c.PRO_MST_ID 
                WHERE sysdate() BETWEEN a.PRO_PRICE_DATE_FROM
                AND a.PRO_PRICE_DATE_TO
                AND b.PRO_TYPE_ID = 1       
            ";

    return $this->db->query($sql);  

}

the problem is i can't get $data->result() in my view, when i debug :

app_model->get_promo_air();  

                    echo "
";
                    print_r($data->result());
                    die();

?>              

                

i've got :

Array
(
    [0] => stdClass Object
        (
            [PRO_PRICE_CODE] => AAA001001
            [PRO_MST_NM] => Promo Air Asia
            [PRO_AMT_CUST] => 65000.00
        )

    [1] => stdClass Object
        (
            [PRO_PRICE_CODE] => AGI001001
            [PRO_MST_NM] => Promo Garuda
            [PRO_AMT_CUST] => 40000.00
        )

)

how to get the value ? coz i try with foreach($data->result() as $t) it's can't get the data... any solution would be appreciated..

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

//Codeigniter Helper function created based on PHP manual Objects, http://www.php.net/manual/en/language.types.object.php#102735
function object_to_array(stdClass $Class){
# Typecast to (array) automatically converts stdClass -> array.
$Class = (array)$Class;
# Iterate through the former properties looking for any stdClass properties.
# Recursively apply (array).
foreach($Class as $key => $value){
if(is_object($value)&&get_class($value)==='stdClass'){
$Class[$key] = self::object_to_array($value);
}
}
return $Class;
}

Use this function to convert stdClass Objects into Arrays:

Test Results:

stdClass Object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

stdClass Object
(
    [foo] => Test data
    [bar] => stdClass Object
        (
            [baaz] => Testing
            [fooz] => stdClass Object
                (
                    [baz] => Testing again
                )

        )

    [foox] => Just test
)

Converted to Array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Array
(
    [foo] => Test data
    [bar] => Array
        (
            [baaz] => Testing
            [fooz] => Array
                (
                    [baz] => Testing again
                )

        )

    [foox] => Just test
)



Do you need help with a project? or have a new project in mind that you need help with?

Contact Me

How do you access the stdClass object as an array?

Linked.
PHP: How to access array element values using array-index..
echo a value in an stdClass Object within an array within an object..
PHP - Convert stdClass Object as String to Array..
Iterate through multidimensional object/array..
ErrorException htmlspecialchars() expects parameter 1 to be string, array given..

What is stdClass object in codeigniter?

StdClass means an object that has no specific class. It is "just a basic object". Every object in php start out from a 'basic object' which is then extended with all the characteristics of the specific class that it is an instance of (like Model, Controller, etc).

How do I print a stdClass object?

If you just want to print you can use var_dump() or print_r() . var_dump($obj); print_r($obj); If you want an array of all properties and their values use get_object_vars() .