Fury
Fury

Reputation: 4776

Remove array key from array in cakephp

print array

array(
    'Order' => array(
        'id' => '1',
        'base_price' => '65',
        'min_price' => '95',            
    )
)

Is it possible to remove the key('Order') when you retrieving data? if Not how can I use array_shift or end in one line and to prevent below error?

I am getting this error Only variables should be passed by reference when I remove the key from array.

$orders = array_shift or end ($this->Order->read(null, $id));
debug($orders);

Upvotes: 0

Views: 1326

Answers (2)

Roberto Maldonado
Roberto Maldonado

Reputation: 1595

You can use Set functions for manipulating arrays:

Set::extract($array, 'Order');

Will output:

array(
    'id' => '1',
    'base_price' => '65',
    'min_price' => '95',            
)

If you need to do this on every output, you can override afterFind() method on your model.

Please see the docs:

http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::extract

http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::classicExtract

Upvotes: 2

Dhaval Rajani
Dhaval Rajani

Reputation: 311

You want only id from it then following code will help you

 $arrOrderId=Set::extract("/Order/id",$data);

here $data is your array from where you want to delete this "Order" key.

You will get following array when you do debug($arrOrderId);

[0]=>1

if you want base_price then write following code

$arrOrderId=Set::extract("/Order/base_price",$data);

Upvotes: 2

Related Questions