im@markvi.eu

If an array in PHP has only one val­ue, you can get that val­ue as a string by using the fol­low­ing methods:

array_​shift #

Using the array_​shift() func­tion: This func­tion removes the first ele­ment from an array and returns the val­ue. If the array has only one ele­ment, the val­ue of that ele­ment will be returned

$array = array("only_value");
$string = array_shift($array);
echo $string; 

reset #

Using the reset() func­tion: This func­tion resets the inter­nal point­er of an array to the first ele­ment and returns the val­ue of that ele­ment. If the array has only one ele­ment, the val­ue of that ele­ment will be returned

$array = array("only_value");
$string = reset($array);
echo $string;

cur­rent #

Using the cur­rent() func­tion: This func­tion returns the val­ue of the cur­rent ele­ment of an array. If the array has only one ele­ment, the val­ue of that ele­ment will be returned

$array = array("only_value");
$string = current($array);
echo $string;

array_​pop #

Using the array_​pop() func­tion: This func­tion removes the last ele­ment from an array and returns the val­ue. If the array has only one ele­ment, the val­ue of that ele­ment will be returned

$array = array("only_value");
$string = array_pop($array);
echo $string;

All of these meth­ods will work for an array with only one val­ue and will return that val­ue as a string. You can choose the one that best suits your needs.

Similar Articles