I have been looking how to do this and am a bit stumped.
My array is as follows:
$returndata->setup_array = array(
'General' => array(
'Main Details' => 'setup/maindets',
'Directories' => 'directories',
'Extension Allocation' => 'xtnallo',
'List Holidays' => 'setup/holidays',
'List Group VM' => 'groupvm',
'Conference Rooms' => 'confroom'
),
'Offices' => array(
'List Offices' => 'iptoffices'
),
'Users' => array(
'List Users' => 'iptusers'
),
'Phones' => array(
'List Phones' => 'iptphones'
),
);
However I have 1 item that on a certain condition(triggered by the users session) that needs to be added to the listin the general array. The section being 'View Details => setup/viewdetails'. I have tried array push (probably incorrectly) but this adds the item as another array at the end under the main array.
I want/need it to work like this:
$returndata->setup_array = array(
'General' => array(
$viewdets
'Main Details' => 'setup/maindets',
'Directories' => 'directories',
'Extension Allocation' => 'xtnallo',
'List Holidays' => 'setup/holidays',
'List Group VM' => 'groupvm',
'Conference Rooms' => 'confroom'
),
'Offices' => array(
'List Offices' => 'iptoffices'
),
'Users' => array(
'List Users' => 'iptusers'
),
'Phones' => array(
'List Phones' => 'iptphones'
),
);
$viewdets = "'View Details' => 'setup/viewdetails'";
and still be interpreted as a functioning array for use as a menu.
$returndata->setup_array['General']['View Details'] = 'setup/viewdetails'
Cheers Rick!
You can use ArrayObject to have the array as a reference:
$a = new ArrayObject();
$b = array(
"a" => $a
);
$a[] = "foo";
print_r($b);
What did you try calling array_push() on? Have you tried
array_push($returndata->setup_array['General'], $viewdets);
You would need to add the variable to the specific depth of the array you wanted it to be present. check out array_push here, there's also a short language syntax that avoids the function call:
$returndata->setup_array['General'][] = $viewdets;
Related
This question already has answers here:
PHP is there a way to add elements calling a function from inside of array
(3 answers)
Closed last month.
In the middle of declaring an array of arrays, I want to "write" an array of arrays generated by my function.
I have a working example when I:
simply store my function-generated arrays into a variable and
then call each array from that function by its key,
but I can't find a command to simply call everything at once.
Here is the code which (I hope) explains it:
<?php
// A. (THIS WORKS)
// A1: A function that returns an array of arrays
function my_arrays_building_function() {
$first_array = array(
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
);
$second_array = array(
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
);
// ... and so on, many more.
return array(
'first-array' => $first_array,
'second-array' => $second_array,
// ... and so on.
);
// NOTE there are tens or hundreds of returned arrays here.
}
// A2: Store my arrays in a variable
$my_array = my_arrays_building_function();
// A3: Inside an array (of arrays), I simply "write" my arrays INDIVIDUALLY and THAT works
array(
array(
'id' => 'dummy_preexisting_array_1',
'type' => 'some-type',
),
array(
'id' => 'dummy_preexisting_array_2',
'type' => 'some-type',
),
// HERE THERY ARE, INDIVIDUALLY, COMMA SEPARATED
$my_array[ 'first-array' ],
$my_array[ 'second-array' ],
array(
'id' => 'dummy_preexisting_array_n',
'type' => 'some-type',
)
),
/** -------------------- //
THE ISSUE
// -------------------- **/
// B: HOW DO I "write" THEM ALL AT ONCE???
// B1: The same as A1
function my_arrays_building_function() {
$first_array = array(
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
);
$second_array = array(
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
);
// NOT SURE I SHOULD RETURN LIKE THIS
return array(
'first-array' => $first_array,
'second-array' => $second_array
);
}
// B2: Same as A3, Inside an array (of arrays), I "write" my arrays BUT NOW I WANT TO "WRITE" THEM ALL AT ONCE
array(
array(
'id' => 'dummy_preexisting_array_1',
'type' => 'some-type',
),
array(
'id' => 'dummy_preexisting_array_2',
'type' => 'some-type',
),
/** >>>> I need my arrays here ALL AT ONCE aka NOT INDIVIDUALLY AS IN EXAMPLE A. <<<< **/
/**
* In other words, while I'm declaring this array,
* I simply need all my arrays from my_arrays_building_function()
* "written" here with a simple command instead of calling hundreds
* of arrays individually as in the first example
*/
array(
'id' => 'dummy_preexisting_array_n',
'type' => 'some-type',
)
), /* this goes on as it's a part of even bigger array */
Although I wouldn't recommend declaring hundreds of array variables inside a function because that's crazy, but for now, you can use get_defined_vars() to get over this issue.
You will also need to filter out the variables which are arrays and has the keys id, type and title as there are could be several other variables defined apart from this.
Snippet:
<?php
array_filter(get_defined_vars(), fn($val) => is_array($val) && isset($val['id'], $val['type'], $val['title']));
Online Demo
Not too sure if I'm understanding this correctly but from what I assume, you just want to return an array with a bunch of others inside it that you define throughout the function?
A simple approach for this would be to define your output variable immediately and add all of your other arrays to it:
function my_arrays_building_function() {
$output = [];
$output[] = [
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
];
$output[] = [
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
];
return $output;
}
Thank you to #Rylee for suggesting Array Unpacking.
The final code would look like this:
// C1: A function that returns an array of arrays BUT without keys
function my_arrays_building_function() {
$first_array = array(
'id' => 'my_array_1',
'type' => 'some-type',
'title' => 'My title 1',
);
$second_array = array(
'id' => 'my_array_2',
'type' => 'some-type',
'title' => 'My title 2',
);
// ... and so on, many more.
// Then return but now I don't assign keys, I only list the vars.
return array(
$first_array, $second_array, ... and so on.
);
}
// C2: Inside an array (of arrays), I use Array Unpacking, THAT'S WHAT I WAS LOOKING FOR, UNPACK ARRAY! SEE BELOW
array(
array(
'id' => 'dummy_preexisting_array_1',
'type' => 'some-type',
),
array(
'id' => 'dummy_preexisting_array_2',
'type' => 'some-type',
),
// HERE I UNPACK MY ARRAY BY USING ... THE THREE DOTS ARE THE KEY
... my_arrays_building_function(),
array(
'id' => 'dummy_preexisting_array_n',
'type' => 'some-type',
)
),
Hooray!
I go through this documentation "https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/journalentry"
And tried to add journal entry with line having customer as shown in the below code:
$journal_entry_create['TxnDate'] = date('Y-m-d');
$journal_entry_line = array(
'Amount' => $amount,
'DetailType' => 'JournalEntryLineDetail',
'JournalEntryLineDetail' => array(
'PostingType' => Credit,
'Entity' => array(
'Type' => 'Customer',
'EntityRef' => array(
'type' => 'Customer',
'value' => "2",
'name' => 'Abc'
)
),
'AccountRef' => array(
'name' => 'Account Name',
'value' => '1'
),
)
);
$journal_entry_lines[] = $journal_entry_line;
$journal_entry_create['Line'] = $journal_entry_lines;
$journal_entry_receipt_create = QBJournalEntry::create($journal_entry_create);
$journal_entry_receipt_create_result = $dataService->Add($journal_entry_receipt_create);
Without EntityRef its working fine but when I add EntityRef its giving me error "Message: Passed array has no key for 'Value' when contructing an ReferenceType"
Only just came across this problem myself. They did fix this issue but didn't seem to document it at all or tell anyone. Found the fix in the source code. You need to use "JournalEntryEntity" instead of "Entity" under "JournalEntryLineDetail", like so:
'JournalEntryLineDetail' => array(
'PostingType' => "Credit",
'JournalEntryEntity' => array(
'Type' => 'Customer',
'EntityRef' => array(
'value' => "2"
)
),
'AccountRef' => array(
'name' => 'Account Name',
'value' => '1'
),
)
Also I used the FacadeHelper from the V3 of the PHP SDK (I'm not sure if it'll work with the method you were using):
use QuickBooksOnline\API\Facades\FacadeHelper;
...
$journal_entry_receipt_create = FacadeHelper::reflectArrayToObject('JournalEntry', $journal_entry_create);
$journal_entry_receipt_create_result = $dataService->Add($journal_entry_receipt_create);
I know this is probably too late for you OP but hopefully it helps someone else!
I have an array like this:
return array(
'User Management -> Role Management' => array(
array(
'permission' => 'role-management.view',
'label' => 'View',
),
array(
'permission' => 'role-management.create',
'label' => 'Create',
),
array(
'permission' => 'role-management.edit',
'label' => 'Edit',
),
array(
'permission' => 'role-management.delete',
'label' => 'Delete',
),
),
'User Management -> User Management' => array(
array(
'permission' => 'user-management.view',
'label' => 'View',
),
array(
'permission' => 'user-management.create',
'label' => 'Create',
),
array(
'permission' => 'user-management.edit',
'label' => 'Edit',
),
array(
'permission' => 'user-management.delete',
'label' => 'Delete',
),
),
);
I have accessed this array successfully like this:
$permissions = Config::get('permissions');
I want to add new index below label. I have tried this but could not add.
`foreach ($permissions as $permission) {
foreach ($permission as $eachPermission) {
$encodedPermission = base64_encode($eachPermission['permission']);
// $eachPermission['encodedPermission'] = $encodedPermission;
array_push($eachPermission, "encodedPermission", $encodedPermission);
}
}
var_dump($permissions); `
I have tried these but could not set the new index. My expected result was this:
'permission' => 'role-management.view',
'label' => 'View',
'encodedPermission' => 'someencodedstring'
Am I doing it wrong or missing something.
Your array is multi-level, so you need to do double-foreach to reach the level required for your changes.
And for your changes being saved in the original array, you need to use ampersand (&) before your iterators to keep the reference to the original array. Without it, you would insert the new data only to the copy of the array that was created for your foreach loop.
This works:
foreach ($permissions as &$eachPermission) {
foreach ($eachPermission as &$singlePermission) {
$encodedPermission = base64_encode($singlePermission['permission']);
array_push($singlePermission, "encodedPermission", $encodedPermission);
}
}
var_dump($permissions);
i would do it like this just because it is easier to understand ...
foreach($permissions as $name1=>$ar1){
foreach($ar1 as $name2=>$ar2){
$permissions[$name1][$name2]['encodedPermission'] = base64_encode($ar2['permission']);
}
}
I have next variables:
$type_model = ProductTypeModel::model()->findByPk($id);
$prod = $type_model->product;
Now in $prod:
array
(
0 => ProductModel#1
(
[CActiveRecord:_new] => false
[CActiveRecord:_attributes] => array
(
'product_id' => '6'
'product_type_id' => '5'
)
...
)
1 => ProductModel#2
(
'product_id' => '8'
'product_type_id' => '5'
)
...
How i can display my products in CGridView?
Thx.
I Suppose you are using CarrayDataProvider. So in your controller
$dataProvider = new CArrayDataProvider($prod);
Here $product could be any array you want to display in CgridView. Now
In you view write this.
$gridColumns = array(
array(
'header' => 'First Name',
'value' => 'ProductTypeModel::model()->findByPk($data->product_id)->key',
'htmlOptions' => array('style' => 'text-align:center;')
),
$this->widget('zii.widgets.grid.CGridView',array('dataProvider' => $dataProvider,));
As in CarrayDataprovider array is obtained so we cant use relations in it. Thats why u have to write 'ProductTypeModel::model()->findByPk($data->product_id)->key'
Here you can display anything attribute of ProductTypeModel so u can replace above mentioned key with your desired attribute
Try this ...
it automatic convert to array data provider.
$dataProvider=new CArrayDataProvider($type_model->product);
Thanks all. By means of answers "naveen goyal" and "jailed abroad" i did like this:
$dataProvider=new CArrayDataProvider($type_model->product);
$dataProvider->keyField = 'product_id';
$this->widget('bootstrap.widgets.TbGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
array(
'header' => 'Title',
'value' => 'CHtml::encode($data["product_title"])',
),
)));
Nice work for me.
I want to store information like below with a key like key = activity window then other info under it, then another key etc..
key: 'activity window'
name: 'Recent Activity'
iconCls: 'activity'
module: 'activity-win'
any idea how to put this into a multi dimensional array?
$data = array(
'activity window' => array(
'name' => 'Recent Activity',
'iconCls' => 'activity',
'module' => 'activity-win'
)
);
Is this what you're looking for, or have I completely misunderstood your question?
Like this:
$myArray = array(
'activity window' => array(
'name' => 'Recent Activity',
'iconCls' => 'activity',
'module' => 'activity-win',
),
array(
...
),
);
You can also add on to the array:
$myArray['new key'] = array(
'name' => 'New Name',
'module' => 'New Module',
);