Extract a value from an ARRAY


#1

dear all,
I have this piece of code inside a controller;

$ticket_data =$this->_controller->Ticket->find(‘all’, array(
‘fields’ => array(‘turno_id’),
‘conditions’ => array(‘Ticket.id’ => 53539 )));

THE PROBLEM:
I want to extract ‘turno_id’ value only and place it into a variable $ticket_turno

THE RESULTS:
tried everything, all I could do is print out the results through the following code;

echo debug($ticket_data);
echo “
”;
var_dump(current($ticket_data));
echo “
”;
echo json_encode($ticket_data,true);
echo “
”;
echo print_r($ticket_data, true);
echo “
”;

And it outputs this:

array(
(int) 0 => array(
‘Ticket’ => array(
‘turno_id’ => ‘2265’
)
)
)

array(1) { [“Ticket”]=> array(1) { [“turno_id”]=> string(4) “2265” } }
[{“Ticket”:{“turno_id”:“2265”}}]
Array ( [0] => Array ( [Ticket] => Array ( [turno_id] => 2265 ) ) )

QUESTION:
how can I isolate inside a variable the turno_id value 2265?

many thanks!
Jordi.Preformatted text


#2

Something like this will work:

Hash::get($ticket_data, ‘0.Ticket.turno_id’);

(I did not actually try this with your data.)
However, it is not very resilient and there are other options.
Checkout: https://book.cakephp.org/3.0/en/core-libraries/hash.html