I have a following return value from Yii framework query, using the methods and classes built in Yii:
Array
(
[0] => Array
(
[id] => 1
[title] => Developer
)
[1] => Array
(
[id] => 2
[title] => Tester
)
)
I would like to (using nothing but Yii), rearange this like:
Array
(
[1] => Developer
[2] => Tester
)
Meaning, I will not group the results in a specific index of the array, and I will enlist them all in one array, AND my key/indexes will represent the value of ID field from my table, and value of those keys will be values from my "role" field from my table.
Is this possible and how?
You can use the following method, defined here:
CHtml::listData($yourArray, 'id', 'title');
Related
I have a multidimensional array stored in $accounts variable :
Array
(
[0] => Array
(
[id] => 1
[account] => ACR016
[desc] => Salary
)
[1] => Array
(
[id] => 2
[account] => ACR017
[desc] => Bonuses
)
)
I'd like to find/get the "desc" value by using its "account" inside $accounts,
In SQL i will go like this :
SELECT desc FROM table WHERE account = 'ACR016';
How to do that with PHP without do the SQL query (because the array already stored in variable) and without looping ?
Use https://www.php.net/manual/en/function.array-column.php for this.
As this is a builtin function it should perform better than doing your own loop.
Example:
$accountDescriptions = array_column($accounts,'desc','account');
Will result in:
Array
(
[ACR016] => Salary
[ACR017] => Bonuses
)
Alternatively you can use:
Example:
$accountDescriptions = array_column($accounts,null,'account');
Then you will get all data keyed with account instead of numeric index.
This question is more of like requiring an architecture and correct approach, then actual code.
Stating it so that people shouldn't flag it as "We cannot code for you..." etc.
I get a response from the API in JSON which I converted to an associated array using json_decode as:
Array
(
[statusCode] => 200
[data] => Array
(
[objects] => Array
(
[0] => deals
[1] => contacts
[2] => accounts
)
[deals] => Array
(
[0] => dealName
[1] => ApprovedBy
[2] => ApprovedDate
[3] => CloseDate
)
[contacts] => Array
(
[0] => contectName
[1] => email
[2] => firstName
[3] => lastName
)
[accounts] => Array
(
[0] => accountName
[1] => creationDate
[2] => ApprovedDate
[3] => accountNumber
)
)
)
So the structure is as:
In response's data array against key "objects", I have an array of objects i.e deals, contacts, accounts.
And for each element in the objects array as the key, an array of fields exist.
I have to show the objects in a drop down say "Objects" and upon selection of a value say "deals" I want to show elements of deals array in a second drop-down "Fields".
[DONE] Also for the options in the dropdowns, values should be the same as actual elements and not numeric values.
json_decode(string, true) gives associated array but with numeric keys. So keys should be same as their associated values like:
[deals] => deals
[contacts] => contacts
[accounts] => accounts
In the view, I have a table with a number of rows, each row having its own pair of these drop-downs.
If each row has its own drop-down, then I also have to take care avoiding updating second drop-down in wrong places. As each drop-down is an array of objects[] & object_fields[], should I use the index of this array in DOM, get next sibling in DOM, or to assign a unique id to each row for the population of correct drop-down at correct row?
How should I achieve these, what should be the correct way?
I cannot use ajax/getJson() as to reduce the number of requests to API and I do have all the required data available. Just need to use it but needed meticulous approach regarding architecture.
Should I pass on Json object to view and process it in javascript in view? Will this be a better approach?
Thanks
UPDATE:
Point 2 of the question is DONE.
I am getting data from an api (that I cannot query agains, just get lump of data), and then I need to query against those data like I would do using database. Only It would be great if I could do it recursively.
Data example
[0] => Array
(
[id] => 1
[url] => https://domain.com/api/1.0/item/1/
[name] => some_item
[category] => some category
[created_by] => Array
(
[id] => 1
[screen_name] => tomino
)
[current_user_domain_access] => Array
(
[is_active] => 1
[is_administrator] => 1
)
[alerts_enabled] => 0
)
(much shortened version)
I receive an array of objects like that and then I need to select/filter/search by values.
Something like this
SomeModel::find(['category'=>'some category','current_user_domain_access' => ['is_administrator' => 1]]);
Is that something that would be possible in PHP? I was thinking about flattening the array, but then there might be key conflicts
1) select data : You can select data by (array_name->id),(array_name->url) and so on...
2)Filter : add conditions according to requirement
3)search : in_array(),array_search
I have a table named user. The id is generated on the fly.
I have a HABTM table. I am trying to insert a record during the user registration. For some reason I can't get the user's id and the user_id to match.
This looks correct, even my array reflects the right information, but the actual data stored is not right.
code:
$this->request->data['User']['id'] = String::uuid();
$this->request->data['Company']['Company'][0]['user_id'] = $this->request->data['User']['id'];
returned array:
Array
(
[User] => Array
(
[email_address] => asdf#asdf.com
[id] => 4fc9a939-3e24-4c79-85d1-6c28e4ca782d
)
[Company] => Array
(
[Company] => Array
(
[0] => Array
(
[id] => 4fc9a939-1840-4c1b-8bd2-6c28e4ca782d
[company_id] => 4fc990dd-edb0-4559-bb7b-6a00e4ca782d
[user_id] => 4fc9a939-3e24-4c79-85d1-6c28e4ca782d
)
)
)
)
So it appears that it would work based on the array I got, but it doesn't save like that. Help is much appreciated.
Maybe because cake autogenerates id to UUID (when field is string and length is 36) take a look at CakePHP API - Model->save()
At first sorry for my English.
I've got a problem in associative models in CakePHP. When I bind more than two models, for example
$this->Album->bindModel(
array(
'hasMany'=>array(
'Photo'=>array(
'className'=>'Photo'
),
'Album'=>array(
'className'=>'Album'
)
)
)
);
I have:
Array
(
[Album] => Array
(
[id] => 22
[f_name] => Some album
[0] => Array
(
[id] => 19
[f_name] => Another album
[id_parent] => 22
[Photo] => Array
(
....
Is it any way to set a key in parent table? I mean I don't want to have "0" as a key, there can be "Album1", "Album2" and so on.
The problem likely stems from binding a model to itself under the same name. Album hasMany Album probably trips up Cake somewhere. Use a unique name for the association, like Album hasMany SubAlbum.