0

The Line 2 fetches the value fall 2017 and line 3 fetches fall 2018 and stores into the array semesters_info.

How to fetch Line 2 and line 3 data (fall 2017,fall 2018) and store into the array?

$semesters_info = (array) null;
 $semesters_info += [$semesters->fall_term_cd => $semesters->fall_term_name];
$semesters_info += [$semesters->spring_term_cd => $semesters->spring_term_name];

I have tried but errors out to array to string conversion error.-

 $test1= [$semesters->spring_term_cd => $semesters->spring_term_name];
$test2 = [$semesters->fall_term_cd => $semesters->fall_term_name];
$semesters_info += $test1.''.$test2;

Currently Semester info will store Fall 2017 and Spring 2018, I would need a third value which is Fall 2017 Spring 2018.

Naishu Babu
  • 61
  • 2
  • 14
  • 2
    You need the correct concatenation operator, `.=` – MonkeyZeus May 21 '18 at 17:46
  • Duplicate of https://stackoverflow.com/questions/11441369/php-string-concatenation – Tom Headifen May 21 '18 at 17:48
  • 1
    Why are you doing `$semesters_info = (array) null;`? What is wrong with `$semesters_info = array();` or the new-ish `$semesters_info = [];` – MonkeyZeus May 21 '18 at 17:49
  • You may need to use the php doc http://php.net/manual/en/language.types.array.php – Oluwatobi Samuel Omisakin May 21 '18 at 17:54
  • $semesters_info += [$semesters->fall_term_cd => $semesters->fall_term_name.=$semesters->spring_term_cd => $semesters->spring_term_name]; I get Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ']' error – Naishu Babu May 21 '18 at 17:54
  • Edit your post and describe what is in input and how should look output. – Pyton May 21 '18 at 18:01
  • It's either `$semesters_info = array(); $semesters_info[$semesters->fall_term_cd] = $semesters->fall_term_name; $semesters_info[$semesters->spring_term_cd] = $semesters->spring_term_name;` or `$semesters_info = $semesters->fall_term_name." ".$semesters->spring_term_name;` depending if you want string or array with the values – user3647971 May 22 '18 at 10:59
  • If you use the latter one (concatenate string) you can add it into array like this: `$semester_info[] = $string;` – user3647971 May 22 '18 at 11:06

0 Answers0