I have an array in a variable if i print that array means the array will look like below :
Array
(
[Body] => This is TestDrive
[Attachment] =>
)
Array
(
[Body] => Testing to an Loops
[Attachment] =>
)
(
[Body] => Okay
[Attachment] =>
)
I want to remove the first array
`Array(
[Body] => This is TestDrive
[Attachment] =>
)`
How to do that ? Please any one help me
Related
how to get an array list into json format like json_encode([[money,1872416],[moneytransfer,1382619],[api,1986920]]).
My array looks like this:
Array
(
[0] => Array
(
[0] => Array
(
[money] => 1
[moneytransfer] => 4340
[api] => 6546
)
)
[1] => Array
(
[0] => Array
(
[money] => 2
[moneytransfer] => 546546
[api] => 6546
)
)
[2] => Array
(
[0] => Array
(
[money] => 3
[moneytransfer] => 6546
[api] => 6546
)
)
)
This question has answered before : "Can an array be top-level JSON-text?" –
Per the JSON RFC, an array is indeed legal top-level JSON-text: "A JSON text is a serialized object or array."
When I try to create a new entry of Template with the $data array, the models of Template, Group and Product are saved correctly. But the nested Calcfield (which is a hasMany of Group) is not saved. :( Group and Product are hasMany of Template.
Is this possible at all?
$this->Template->create();
$this->Template->saveAll($data, array('validate' => false));
// $data looks like this:
Array
(
[Template] => Array
(
[title] =>
[shorttitle] => Wie auch immer
[place] => Hannover
[size] =>
)
[Group] => Array
(
[0] => Array
(
[title] => Hosting
[order] =>
[Calcfield] => Array
(
[0] => Array
(
[title] => Hosting
[value] => 0
[description] =>
)
[1] => Array
(
[title] => Strom
[value] => 0
[description] =>
)
)
)
)
[Product] => Array
(
[0] => Array
(
[share] => 10
[businessunit] => Marketing
)
[1] => Array
(
[share] => 30
[businessunit] => intl. CRM
)
)
)
No, recursive saves are not possible, as far as I know. You would need to stick those in a separate array and then save them after the initial save.
Since CakePHP 2.1 it's possible to save deeply-nested models using the deep parameter in saveAll (since CakePHP 2.1)
ref. http://book.cakephp.org/2.0/en/models/saving-your-data.html?highlight=saveall#model-saveassociated
I am experiencing an odd behaviour:
In the $results['capital'] each contained model is fetched (therefore, CapitalCategory, CapitalCategory->Category and Picture) for the Capital model.
But in the $results['category'] the Picture model is not fetched (only Capital and Category are fetched) for the CapitalCategory model.
I have attached a screenshot for clarity:
What could be the problem? Where should I look? Thank you!
EDIT
Here is the output for the $results['capital'] array,
[0] => Array
(
[Capital] => Array
(
[name] => N'Djamena
[id] => 81
)
[CapitalCategory] => Array
(
[0] => Array
(
[value] => Ciad
[category_id] => 2
[capital_id] => 81
[Category] => Array
(
[name] => Ţară
)
)
)
[Picture] => Array
(
[0] => Array
(
[picture] => http://farm2.static.flickr.com/1270/879755600_126f8824db_s.jpg
[capital_id] => 81
)
)
)
and here is the output for the $results['category'] array (I've put only one record since they are similar):
[0] => Array
(
[CapitalCategory] => Array
(
[value] => America de Sud
)
[Capital] => Array
(
[name] => Asuncion
[id] => 56
)
[Category] => Array
(
[name] => Continent
[id] => 1
)
)
It looks like the problem was that CakePHP expects to have all foreign keys in the fields array. Therefore, after I have added the 'CapitalCategory.capital_id' in the fields array, the Pictures are shown. Still don't get why the first query worked while this one not, but I'm glad I figured it out.
Let's say I have an index action where I want to get a list of projects:
$this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
It works well and returns the following array:
Array ( [0] => Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) ) )
How do I modify the find function, so it returns the array in the following format:
Array ( [projects] => Array ( [0] => Array ( [id] => 2 [title] => test project ) [1] => Array ( [id] => 1 [title] => first project ) ) )
Thank you!
You could use the Set::combine() utility method to do this. I've used it for similar means as so:
public function groupByMenu() {
return Set::combine (
$this->find (
'all',
array (
'conditions' => array ( 'NavItem.active' => 1 ),
'order' => 'NavMenuItem.display_order'
)
),
'{n}.NavItem.title',
'{n}',
'{n}.NavMenu.id'
);
}
The code above takes a set of navigation items and reorganizes them so that they're grouped by the menu(s) they are displayed within.
It's not really clear if it's the fact that the result is under 'Project' rather than 'projects', but if you don't like that it's under [0] I believe you could use PHPs array_shift:
$result = $this->Project->find('all', array('order' => 'Project.modified DESC', 'conditions' => array('Project.user_id' => 1)));
$result = array_shift($result);
The result will be:
Array ( [Project] => Array ( [id] => 2 [title] => test project ) ) [1] => Array ( [Project] => Array ( [id] => 1 [title] => first project ) )
I have the output array from a $Model->find() query which also pulls data from a hasMany relationship:
Array(
[Parent] => Array
(
[id] => 1
)
[Child] => Array
(
[0] => Array
(
[id] => aaa
[score] => 3
[src] => stage6/tn~4bbb38cc-0018-49bf-96a9-11a0f67883f5.jpg
[parent_id] => 1
)
[1] => Array
(
[id] => bbb
[score] => 5
[src] => stage0/tn~4bbb38cc-00ac-4b25-b074-11a0f67883f5.jpg
[parent_id] => 1
)
[2] => Array
(
[id] => ccc
[score] => 2
[src] => stage4/tn~4bbb38cc-01c8-44bd-b71d-11a0f67883f5.jpg
[parent_id] => 1
)
)
)
I'd like to transform this output into something like this, where the child id is the key to additional child attributes:
Array(
[aaa] => Array
(
[score] => 3
[src] => stage6/tn~4bbb38cc-0018-49bf-96a9-11a0f67883f5.jpg
)
[bbb] => Array
(
[score] => 5
[src] => stage0/tn~4bbb38cc-00ac-4b25-b074-11a0f67883f5.jpg
)
[ccc] => Array
(
[score] => 2
[src] => stage4/tn~4bbb38cc-01c8-44bd-b71d-11a0f67883f5.jpg
)
}
Is there an easy way to use Set::extract, Set::combine, Set::insert, etc. to do this efficiently? I cannot figure it out.
I've used Set::combine to do this a few times, but I can't make an argument for efficiency. As an example, I had a list of volunteers and each of those volunteers belonged to a group. I wanted to retrieve each committee and its volunteers, but as an associative array keyed by the committee name. Here's the code I used to do that:
$volunteers = Set::combine (
$this->Volunteer->Committee->find (
'all',
array ( 'order' => 'display_order' )
),
'{n}.Committee.title',
'{n}.Volunteer'
);
Hopefully that helps. The documentation for Set::combine may help elaborate on the details.
As Rob says, Set::combine is the answer to your question. Here's a sample solution, using the XPath 2.0 syntax:
$parents = $this->Parent->find('all');
$assocChildren = Set::combine( $parents, '/Child/id', '/Child' );