English 中文(简体)
CakePHP function to convert dotted arrays to multidimensional
原标题:

In CakePHP, it seems like a lot of functions can take their arguments as nested, multidimensional arrays, or as dotted strings:

$this->MyModel->contain(array(
     Something ,  Something.Else ,  Something.Else.Entirely 
));
$this->MyModel->contain(array(
     Something  => array(
         Else  =>  Entirely 
    )
));

Therefore, I figure there must be a function somewhere in the core to switch from dotted to nested associative, but I can t find it for the life of me. Any ideas?

最佳回答

I ve actually figured my own way to get this working leveraging the built-in Set functions.

Given:

$input = array (
     Post.id  => 1,
     Post.title  =>  Some post title. ,
     Post.Tag.0.id  => 4,
     Post.Tag.0.name  =>  cakephp ,
     Post.Tag.1.id  => 7,
     Post.Tag.1.name  =>  mysql ,
);

This code will put that into a nested associative array.

$output = array();
foreach ($input as $key => $value) {
    $output = Set::insert($output, $key, $value);
}

Here s the docs for Set::insert()

问题回答

What you re looking for is Set::flatten(). It s not documented in the CakePHP manual, but take a look at the API definition.

It works something like this (the result might not be exact, this is from my head):

$array = array(
     Post  => array(
         id  => 1,
         title  =>  Some post title. ,
         Tag  => array(
            0 => array(
                 id  => 4,
                 name  =>  cakephp ,
            ),
            1 => array(
                 id  => 7,
                 name  =>  mysql ,
            ),
        ),
    );
);

$array = Set::flatten($array);
var_dump($array);

Your $array variable will now look like this:

Array (
     Post.id  => 1,
     Post.title  =>  Some post title. ,
     Post.Tag.0.id  => 4,
     Post.Tag.0.name  =>  cakephp ,
     Post.Tag.1.id  => 7,
     Post.Tag.1.name  =>  mysql ,
)

It s just a convention throughout Cake, but each part does its own, customized parsing. If you look at the function ContainableBehavior::containments() in cake/libs/model/behaviors/containable.php, you ll see a lot of preg_matching and explode( . )ing going on. At least in the case of Containable, the verbose array( name => array(...)) syntax seems to be the canonical syntax, but it can be abbreviated with the dot syntax, which will just be expanded. I d guess that the expanding itself is just too varied among different parts to be easily summarized in a central function.

That, or they just haven t gotten to it yet. :)

Great question, I was just searching for the same thing. Apparently it s coming in Cake 2.2.

The new Hash class (a new, improved version of the Set class) has an expand() function which does this. You can view the code on Github if you need to use it in the meantime:

https://github.com/cakephp/cakephp/blob/2.2/lib/Cake/Utility/Hash.php

...However the solution nickf posted works great, too. :-)





相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签