-2

I am new to php arrays and struggling to get my head around how to parse data from them. I have had a look at How can I access an array/object?

but is just beyond my grasp. Need an example to understand. So i have this array in a $result variable

Array
(
    [response] => Array
        (
            [data] => Array
                (
                    [0] => Array
                        (
                            [fieldData] => Array
                                (
                                    [CURRENT_PRIVILEGESET] => FM Data API
                                )
                            [portalData] => Array
                                (
                                )
                            [recordId] => 1
                            [modId] => 0
                        )
                    [1] => Array
                        (
                            [fieldData] => Array
                                (
                                    [CURRENT_PRIVILEGESET] => FM Data API
                                )
                            [portalData] => Array
                                (
                                )
                            [recordId] => 2
                            [modId] => 0
                        )
                )
        )
    [messages] => Array
        (
            [0] => Array
                (
                    [code] => 0
                    [message] => OK
                )
        )
)

How do i get the value for [CURRENT_PRIVILEGESET]? I have tried the below but i think i have misunderstood something. Thanks all for your patience while i try to get to grips with this.

$privilege = $result["response"]['data'][0]->fielddata["CURRENT_PRIVILEGESET"];
AymDev
  • 6,626
  • 4
  • 29
  • 52
Kevin
  • 33
  • 6

3 Answers3

2
$result['response']['data'][0]['fieldData']['CURRENT_PRIVILEGESET'];

You've used an object notation of -> but you're working with arrays the whole way down, not objects.

Royal Wares
  • 1,192
  • 10
  • 23
1

There is no object in your example. With -> you are referring to an object. These are all arrays, so you can just go on with the keys:

$privilege = $result["response"]['data'][0]["fieldData"]["CURRENT_PRIVILEGESET"];
Gobbin
  • 530
  • 3
  • 17
0

Whenever you want to traverse(move through) an object, you do that using the arrow(->) notation whereas for arrays, you do that by accessing the index([x]) notation where x could either be an int e.g [1] or a string e.g ['data'].

I am assuming you know what classes, objects as well as arrays are. Let's assume we have a class called Test and an object of Test class called $testObj, whenever we do var_dump($testobj), from top down, you would see something like this:

object(Test)[1]
public 'feeling' => string 'Love' (length=4)
public 'inverse' => string 'hate' (length=4)

This simply means that the object we are var_dumping belongs to the Test class and we can access the feeling property of $testObj like so: $testObj->feeling;

This structure is not very different for arrays. If we have an array called $testArr and do a var_dump($testArr), we would see:

array (size=2)
  'feeling' => string 'love' (length=4)
  'inverse' => string 'hate' (length=4)
  3 => int 300

Again, we see that, the type is stated; in this case Array. We can access some of the indexes like so: $testArr['feeling'] //Outputs love $testArr[3] //Outputs 300

For your use case, you can traverse down to CURRENT_PRIVILEGESET like so:

$privilege = $result["response"]['data'][0]["fieldData"]["CURRENT_PRIVILEGESET"];

Checkout these resources for more info. http://php.net/manual/en/language.types.object.php http://php.net/manual/en/language.types.array.php

Chukwuemeka Inya
  • 2,575
  • 1
  • 17
  • 23