Search code examples
phparraysyiivar-dump

How to get value from var_dump as a single variable?


I hava a variable $var->VerseTranslations in yii framework. When i keep it in var_dump.

Like var_dump($var->VerseTranslations);

It gives an array which is in below.

array (size=3)
  0 => 
    object(VerseTranslations)[96]
      private '_new' (CActiveRecord) => boolean false
      private '_attributes' (CActiveRecord) => 
        array (size=5)
          'id' => string '3' (length=1)
          'verse_id' => string '1' (length=1)
          'language_id' => string '2' (length=1)
          'translation_text' => string ' "In the name of ALLAH, most Gracious, most Compassionate". ' (length=60)
          'scholar_id' => string '1' (length=1)
  1 => 
    object(VerseTranslations)[97]
      private '_new' (CActiveRecord) => boolean false
      private '_attributes' (CActiveRecord) => 
        array (size=5)
          'id' => string '4' (length=1)
          'verse_id' => string '1' (length=1)
          'language_id' => string '1' (length=1)
          'translation_text' => string 'شروع الله کا نام لے کر جو بڑا مہربان نہایت رحم والا ہے' (length=96)
          'scholar_id' => string '1' (length=1)
  2 => 
    object(VerseTranslations)[98]
      private '_new' (CActiveRecord) => boolean false
      private '_attributes' (CActiveRecord) => 
        array (size=5)
          'id' => string '5' (length=1)
          'verse_id' => string '1' (length=1)
          'language_id' => string '5' (length=1)
          'translation_text' => string 'En el nombre de Alá, el Compasivo, el Misericordioso' (length=53)
          'scholar_id' => string '1' (length=1)

Now i want to get first translation_text is in a single variable. Like $var->VerseTranslations->translation_text but it gives error,I can get all the single variable using foreach loop but i do not want to use, i just need a single variable, so can you help me, how to get a single variable from an above array.

Thanks.


Solution

  • For single variable you can use it in given below.

    $errors = array_filter($data->verseTranslations);
    if(!empty($errors)) {
        echo $data->verseTranslations[0]['translation_text'];
    }
    

    You may also write this but i am using foreach loop too.

    <?php $mydat = array();
     $errors = array_filter($data->verseTranslations);
     if(!empty($errors)) {
       foreach($data->verseTranslations as $var) 
         { $mydat[] = $var['translation_text'];  }
       echo $mydat[0];
    }
    ?>
    

    Hope it will help you