Home > database >  Array to string conversion using PHP end()
Array to string conversion using PHP end()

Time:09-27

I am trying to assign the last element of an array to a variable code extract as below:

92 //Get the list of numbers used from the database
93 $lastNumbersUsed = get_membership_numbers($typePrefix);
94 
95   foreach($lastNumbersUsed as $value){
96      echo $value["membershipNumber"] . "</br>";
97   }
98 //move to the last number
99 $lastNumber = end($lastNumbersUsed);
**100   echo("The last number is " . $lastNumber);

The Output for lines 92 to 97 is a follows: 2 string elements as expected F100 F101

However attempting to assign the last element at line 99 produces:

Notice: Array to string conversion in C:\xampp\htdocs....\index.php on line 100 The last number is Array

Any help gratefully received

CodePudding user response:

$lastNumbersUsed is two dimensional array and end function return last item that refer to array so the value of variable $lastNumber is array , you must say $lastNumber[index] review this code

  •  Tags:  
  • php
  • Related