Question

I am currently working on a CakePHP project and I am trying to access some user information. However, the subject of the question is a more PHP-related one. I would like to know what is the difference in the following two objects (say first and second). I have tried to access the 'username' value in each one, and I think that for the first one it can be accessed as $first['username'] and for the second one it is $second['User']['username']. But I cannot understand the difference.

Finally I would be glad if you could indicate a way of transforming the first one into the second one.

Note: The following format is produced by a debug() function in CakePHP.

array(
 'User' => array( 
  'username' => 'askdjsakl',
  'id' => '53698b89-cb34-41eb-931c-1859749dad86'
 )
)

Array
(
    [User] => Array
        (
            [username] => askdjsakl
            [id] => 53698b89-cb34-41eb-931c-1859749dad86
        )

)
Was it helpful?

Solution

Those arrays are the same. You said to access the first one you do $first['username'] and for the second one you do $second['User']['username']

Since they are the same, your variable $first is pointing to the inner array. Or in other words, something equivalent to this:

$first = $second['User']

Once you have that line, you will simply be able to do $first['username'] So... somewhere in your script you are assigning $first to the inner array

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top