I have this array and I don't know how to get the key/value.
$respuesta = array(
'metodo_pago' => "Efectivo",
'total' => 16500.00,
'estado' => "pendiente",
'fecha_venta' => "2021-09-07 19:18:43",
'seccion_id' => 1015484599958
);
Try print_r($respuesta);
showing me the array and var_dump($respuesta);
telling me what type of data they are, but if I want to get the sale date, I don't know how to get the value.
I appreciate the help!
You can iterate the values of the array obtaining its key and value
exit:
but if you want to get a value inside the array, you can refer to the key and get its value, for example to get the value of "sale_date" inside the array:
you would have as output:
If you want to get all keys from an array or a subset of keys from an array you can use array_keys() :
and you can get the value of a key from its index, for example for the fourth element:
If you want to get all the values of an array you can use array_values()
and you can get the value from its index, for example for the fourth element:
For your example, the way to access the value would be Singular :
as the documentation indicates: Link
example#6:
You can also implement a
foreach
:exit:
also to access all the data another way can be with
array_walk
:exit:
It is also important since we are talking about array that you know the following:
1. You can briefly declare arrays with
brakets []
:2. you can cast your arrays into objects:
after casting you can access its properties as follows:
3. implement
array_keys
andarray_value
:Example: https://wtools.io/php-sandbox/b6WK
array_keys and array_value documentation
Code: