0

How to access the values in associative array in yii2. Note that this array is return by the ArrayHelper. I want to access the value of project_id . I tried to use getColumn($array, 'project_id') property from the ArrayHelper class. But it throws: unidentified index project_id.

This below shown array is dumped using VarDumper of yii2:

[ 
  [ 
    [ 
 'plot_id' => '9',

 'plot_no' => '4',

 'project_id' => '1', 

 'project' =>
      [ 
   'project_id' => '1',
   'project_name' => 'City Dubaiq', 

      ],
    ], 
  ], 
]
5
  • 1
    Well you have two instances of project_id in your example, and you have not specified which one you need. Do you need all project_id values in a given array recursively? Commented Dec 6, 2014 at 12:13
  • wow. how did they manage to actually have a worse var_dump then the very correct var_dump(). you don't even see the two first [0] keys... you should try var_dump($array), you would have understood right away how to access your variable Commented Dec 6, 2014 at 13:30
  • @Félix Gagnon-Grenier who are the "they" you talk about. Commented Dec 7, 2014 at 5:32
  • The people who designed the var dumper of yii2. If you look closely, we should be seeing a nested array in which the two first level are at key 0, however we do not see the numeric keys in their var dumper (or at least in ops code) Commented Dec 7, 2014 at 6:57
  • Hi, Thanks I solved it by using ArrayHelper. First I did not use array helper instead I got results as Array(). Commented Dec 8, 2014 at 15:41

1 Answer 1

1

You can get the value of project_id in Yii2's ArrayHelper like below(with your code):

echo \yii\helpers\ArrayHelper::getValue($array[0][0], 'project_id'); //1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.