How to build multi dimensional array for store this? - php

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',
);

Related

How to pass custom data to EDataTables in Yii

I am using Yii EDataTables Extension : http://www.yiiframework.com/extension/edatatables/
I have Widget of EDataTables as follow
$this->createWidget(
'ext.EDataTables.EDataTables', array(
'id' => 'items',
'dataProvider' => $dataProvider,
'ajaxUrl' => '/ajax-update',
'columns' => array(array(
'user_id',
'email',
'name',
'created_at:date:Updated',
'updated_at:date:Updated',
)
),
'options' => array(
'bStateSave' => false,
'bPaginate' => true,
),
)
);
I want to pass custom ( value,array or object to the widget so i can use it in the row value )
I have looked around many places but i couldn't find way to do it.
Could you please advice how to do that ?
Assuming you have custom column Value for name for example :
$nameExp = '$this->grid->options["nameArray"][$data->user_id]';
And Array like this
$namesInArray = array(
'1'=>'FirstName LastName 1',
'2'=>'FirstName LastName 2',
'3'=>'FirstName LastName 3',
'4'=>'FirstName LastName 4',
);
The possible way to pass extra data not from data provide is to send it with options or htmloptions array ( because all other params for the widget has to have an attribute in the class referring to )
So You can pass the object or the array in the options array and use it as below
$nameExp = '$this->grid->options["nameArray"][$data->user_id]';
$namesInArray = array(
'1'=>'FirstName LastName 1',
'2'=>'FirstName LastName 2',
'3'=>'FirstName LastName 3',
'4'=>'FirstName LastName 4',
);
$this->createWidget(
'ext.EDataTables.EDataTables', array(
'id' => 'items',
'dataProvider' => $dataProvider,
'ajaxUrl' => '/ajax-update',
'columns' => array(array(
'user_id',
'email',
'name',
array('class' => 'CDataColumn', 'name' => 'Name from array options', 'sortable' => false, 'value' => $nameExp, 'type' => 'raw'),
'created_at:date:Updated',
'updated_at:date:Updated',
)
),
'options' => array(
'bStateSave' => false,
'bPaginate' => true,
'nameArray' => $namesInArray
),
)
);
You will be able to use/access the Array, value or the object with that row value expression and do the logic you need there !

Conditionally adding item to multidimensional array

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;

How to merge php array

PHP Original array is a two-dimensional array.
I want to extract all the different value(the key name is parentMenu) to be a new two-dimensional array's key name
at the same time old array will be the new array's value
what and how should id do?
below is array example
//the menuList is get from mysql result
$menuList = array(
0=>array(
'navId' =>1,
'parentMenu' => 'HOME', //Previous Menu
'subMenu' => 'SHOW USER', //Sub Menu
'ctrl' => 'Index',
'action' => 'index'
),
1=>array(
'navId' =>2,
'parentMenu' => 'HOME',
'subMenu' => 'MODIFY PASSWORD',
'ctrl' => 'Modify',
'action' => 'index'
),
2=>array(
'navId' =>3,
'parentMenu' => 'ITEM LIST',
'subMenu' => 'CURRENT LIST',
'ctrl' => 'Current',
'action' => 'index'
),
3=> array(
'navId' =>4,
'parentMenu' =>'ITEM LIST',
'subMenu' =>'HISTORY LIST',
'ctrl' =>'History',
'action' =>'index'
)
);
//After processing the menuList
//The new MenuList what I want is like below
$newMenu = array(
/*parentMenu's value to be key*/
'HOME'=>array( array('navId' =>1,'subMenu' =>'SHOW USER' ,'ctrl' =>'Index' ,'action' =>'index'),
array('navId' =>2,'subMenu' =>'MODIFY PASSWORD','ctrl' =>'Modify' ,'action' =>'index')
),
'ITEM LIST'=>array(
array('navId' =>3,'subMenu' =>'CURRENT LIST','ctrl' =>'Current' ,'action' =>'index'),
array('navId' =>4,'subMenu' =>'HISTORY LIST','ctrl' =>'History' ,'action' =>'index')
)
);
$newMenu = array();
foreach($menuList as $item) {
$key = $item['parentMenu'];
unset($item['parentMenu']); // remove the parentMenu
if(!isset($newMenu[$key])) {
$newMenu[$key]] = array($item);
} else {
//array_push($newMenu[$key], $item);
$newMenu[$key][] = $item;
}
}
UPDATE: adjusted codes according to #Anyone's suggestion

dynamically add tab in YiiBooster TbTabs widget

I have created bootstrap.widgets.TbTabs as below, how can I add a new tab dynamically on client side. Is there any event using which I can add new tab just like the "shown" event in below code.
<?php $this->widget('bootstrap.widgets.TbTabs', array(
'id' => 'mytabs',
'type' => 'tabs',
'tabs' => array(
array('id' => 'tab1', 'label' => 'Tab 1', 'content' => $this->renderPartial('tab1', null, true), 'active' => true),
array('id' => 'tab2', 'label' => 'Tab 2', 'content' => 'loading ....'),
array('id' => 'tab3', 'label' => 'Tab 3', 'content' => 'loading ....'),
),
'events'=>array('shown'=>'js:loadContent')
));?>
There is none but that doesn't mean you can't create it yourself.
Just pass the 'tabs' part via your controller, something like this:
in your controller:
$tabs => array(
array('id' => 'tab1', 'label' => 'Tab 1', 'content' => $this->renderPartial('tab1', null, true), 'active' => true),
array('id' => 'tab2', 'label' => 'Tab 2', 'content' => 'loading ....'),
array('id' => 'tab3', 'label' => 'Tab 3', 'content' => 'loading ....'),
);
$this->render('pageName', array('tabs' => $tabs));
In your view:
<?php $this->widget('bootstrap.widgets.TbTabs', array(
'id' => 'mytabs',
'type' => 'tabs',
'tabs' => $tabs,
'events'=>array('shown'=>'js:loadContent')
));?>
If you want to achieve something similar but in your main layout, you can create a Model which extends from CActiveRecord and stores the tabs in the database. After that you could use a static method in your model class to call and retrieve the tabs. Then in your layout view you can do something like this:
<?php $this->widget('bootstrap.widgets.TbTabs', array(
'id' => 'mytabs',
'type' => 'tabs',
'tabs' => Model::getTabs(),
'events'=>array('shown'=>'js:loadContent')
));?>
Add the new tab link in header, add the new tab, display the new tab
$('#tabHeaders').append($('<li>New Tab</li>'));
$('#tabContent').append($('<div class="tab-pane" id="newtab"></div>'));
$('#newtab').tab('show');
Note YiiBooster automatically generates element id, you need to use appropriate jquery selectors
I don't think it's good practice to renderPartial the included views inside the controller.
I found this to work for me in the view:
$this->widget('bootstrap.widgets.TbTabs', array(
'type'=>'tabs',
'tabs'=>array(
array( //
'label'=>'Comments ('.$model->commentCount.')',
'content'=>$this->renderPartial( '_comments', array( 'model'=>$model ), true ),
),
)
));
Obviously, for this to work you need "_comments.php" to exist...

How to create this SOAP XML request?

we are using SAOP clients for a while now without a problem. But now we are facing the following challenge and I can't find the answer. We need to send the following XML structure:
<Cards>
<CardDetails>
<Name>string</Name>
<Address>string</String>
</CardDetails>
<CardDetails>
<Name>string</Name>
<Address>string</String>
</CardDetails>
</Cards>
As you van see we need two instances of 'CardDetails'. Creating a PHP array will only allow me to send 1.
$data = array(
'Cards' => array(
'CardDetails' => array(
'Name' => 'test name',
'Address' => 'test address'
),
'CardDetails' => array(
'Name' => 'second test name',
'Address' => 'second test address'
)
)
));
Of course, only the second address will be used. But what would be the solution to make this work?
Thanks a lot!
Use php dom to create xml from an array , here is a good example of it http://www.ibm.com/developerworks/library/os-xmldomphp/
And what function/class are you using to turn array to xml?
Try this structure:
$data = array(
'Cards' => array(
'CardDetails' => array(
array(
'Name' => 'test name',
'Address' => 'test address'
),
array(
'Name' => 'second test name',
'Address' => 'second test address'
)
)
)
);
If this doesn't work and you can modify serializing function, just check if current key is numerical. If it is, use parent key name for tag.

Categories