0

I have an array that looks like this:

Array
(
    [id] => 01
    [name] => johndoe   
    [fields] => Array
        (
             [19] => Array
                 (
                      [othername] => fieldname
                 )
        )
)

How can I go about getting the value from othername?

I tried:

 $array = array();
 $array[fields] = array();
 echo $array[19]['othername'] ;

But I think I need to go down another level?

1
  • 5
    Just echo $array['fields'][19]['othername'] Commented Jun 23, 2017 at 10:51

3 Answers 3

2
$array = array();         //-| 
                          // | This resets the variable to blank array. Remove it.
$array[fields] = array(); //-|

// assuming that variable dumped is $array, directly use this.
echo $array['fields'][19]['othername'];

But, I would suggest that, you should not fetch the required value using such fix syntax. My answer is just to solve your issue at hand. Implementing coding best practices is out of scope for this answer. You should try to loop recursively and fetch required value.

Sign up to request clarification or add additional context in comments.

Comments

2

Just use as simple

$array['fields'][19]['othername']

Comments

2
$data = array(
    'id' => '01',
    'name' => 'johndoe',
    'fields' => array(
        array(
            'othername' => "fieldname"
        )
    ),
);
//Inside array is array of array so foreach() is used
foreach ($data['fields'] as $row)
{
  echo $row['othername'];
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.