2

I have an Object like below

stdClass Object
(
    [0] => stdClass Object
        (
            [className] => stdClass Object
                (
                    [attribute] => stdClass Object
                        (
                            [name] => Aabir Hussain
                            [phone] => 9555555985
                            [email] => aabir@gmail.com
                            [create_on] => 12-10-2016 12:12:12
                            [status] => Active
                        )

                    [validationMessage] => stdClass Object
                        (
                            [name] => no_error
                            [phone] => no_error
                            [email] => Email Already Taken, Please Choose Another One
                            [create_on] => no_error
                            [status] => no_error
                        )

                    [scenario] => create
                )

        )

)

I want to get value of name without using foreach or for loop

I have tried this link where they have given different approaches to get values from an Object like below. but nothing is working for me.

print_r($arrayMultiIndex[0]); //not working because it is not an array;
print_r($arrayMultiIndex{0}); //not working
print_r($arrayMultiIndex->0); //not working

I am using PHP 7.0.9

EDIT

print_r($arrayMultiIndex->{0}); //also not working or
echo $arrayMultiIndex->{0}->className->attribute->name //not working

EDIT For Php Fiddle php fiddle link

Community
  • 1
  • 1
Aabir Hussain
  • 1,161
  • 1
  • 11
  • 29

4 Answers4

1

Probably the below link could help you out

How to access object properties with names like integers?

Basically, Curly-brace syntax for object property access does not work for all-digit keys.

Or instead of simply typecasting the object use my modified version of the function arraytoobject. This only for your specific need. I am converting the key to a string as otherwise it won't be accesible.

$arrayMultiIndex = [
     [
    'className' => [
        'attribute' => [
            'name' => 'Aabir Hussain',
            'phone' => 955585,
            'email' => 'aabir@gmail.com',
            'create_on' => '12-10-2016 12:12:12',
            'status' => 'Active'
        ],
        'validationMessage' => [
            'name' => 'no_error',
            'phone' => 'no_error',
            'email' => 'Email Already Taken, Please Choose Another One',
            'create_on' => 'no_error',
            'status' => 'no_error'
        ],
        'scenario' => 'create',
    ] 
]
];
$arrayMultiIndexObject = arrayToObjectNew($arrayMultiIndex);
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;
    }
}
function arrayToObjectNew($array) {
if (!is_array($array)) {
    return $array;
}


if (is_array($array) && count($array) > 0) {
    foreach ($array as $name=>$value) {

        $object = new stdClass();
        if(is_numeric($name))
            $name = (string) $name;

        $name = strtolower(trim($name));
        // if (!empty($name)) {
            $object->$name = arrayToObject($value);
        // }
    }
    return $object;
}
else {
    return FALSE;
}
}

echo '<pre>'; print_r($arrayMultiIndexObject->{0}); die;
Community
  • 1
  • 1
Rohit Ailani
  • 910
  • 1
  • 6
  • 19
  • seems like we can't access this index, either we have to convert it into an array or we have to use a new stdClass then assign values to it. – Aabir Hussain Oct 04 '16 at 07:34
  • Yes that is correct. I hope you are creating this object by simply typecasting? so I would suggest that get an associated array to do that. – Rohit Ailani Oct 04 '16 at 08:41
  • 1
    yes i have created this object with typecasting.have a look at fiddle and i use `$jsonConversion = json_decode(json_encode($arrayMultiIndexJson))` then i am able to `print_r($jsonConversion[0]);` – Aabir Hussain Oct 04 '16 at 08:56
  • Please check my edit if you want to use the object only. Although this is not proper coding standard, but based on your requirement either you can use json_decode(json_encode($arrayMultiIndexJson)) but that again is creating an array which you do not want. so you can use the method specified in my edit. Please check. – Rohit Ailani Oct 04 '16 at 09:19
  • Thanks dude. it does not support integer index, so converting it to string is better option. – Aabir Hussain Oct 04 '16 at 09:32
0

If you don't want to use foreach and for loop then convert this object into array and filter out 'name' property value and access it in normal way like $arr[0], $arr[1],... i.e. code given below which is not using foreach and for loop and then getting name property value..

//Converting object into array
$arrayMultiIndex_arr = json_decode(json_encode($arrayMultiIndex), TRUE);

//Filtering out name property value from array
$name_array = array_column(array_reduce(array_reduce($arrayMultiIndex_arr, 'array_merge', array()), 'array_merge', array()), "name");

print_r($name_array);
himeshc_IB
  • 853
  • 4
  • 10
0

As i search everywhere i don't find any feasible solution for you, but i can give you one suggestion to convert the object into array.

$arrayMultiIndex = (array)$arrayMultiIndex;
echo $arrayMultiIndex[0]['className']['attribute']['name'];

Online Example of this code.

please read the whole bunch of answer form here...

Community
  • 1
  • 1
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
  • Thanks for your answer. I know i can convert into array then easily access this $arrayMultiIndex[0]['className']['attribute']['name']; but it will be an headache for me because i always have to convert it into an array. – Aabir Hussain Oct 04 '16 at 08:49
  • Yes i know, The problem is with your Object. Otherwise it will be access easily as i said / suggest earlier. – Murad Hasan Oct 04 '16 at 08:51
  • did you see the fiddle, i have just typecaste the array to make it Object. Some one said that it is not possible to access because 0 is integer. – Aabir Hussain Oct 04 '16 at 09:00
  • Yes, If you make an stdClass then it is easy to access but you have typecast it so now it is not possible. – Murad Hasan Oct 04 '16 at 09:01
-1

$name = reset($arrayMultiIndex)->className->attribute->name;

echo $name;