Retrieving only value within an array with one element

Retrieving only value within an array with one element

$arr = array(‘3’=>‘1’);

$key = key($arr);

I get keys like this, no problem
However, I need to get value

How do I get value only?

Simple enough…
echo $arr[3];

Thank you for the answer
However, the key is variable

end() or reset() returns value but fails
Notice: Only variables should be passed by reference in

I solved the problem

$variable = end(json_decode($_POST[‘name’, true]));
The problem is solved as follows
$variable = json_decode($_POST[‘name’, true]);
$variable = end($variable);

What is the real problem you are trying to solve with this code?

I create the selected product ID and product quantity json_encode and put it in session
Then I’ll POST it with ajax elsewhere, I used end () here because only the product quantity is required

key() works like this, but end() doesn’t work, it is necessary to separate variables
3 => Product ID
1 => Product amount
$variable = key(json_decode($_POST[‘name’], true));

For end()
$variable = json_decode($_POST[‘name’], true);
$variable2 = end($variable);

Sponsor our Newsletter | Privacy Policy | Terms of Service