Fatal error: Cannot use string offset as an array - arrays

Array
(
[0] => Array
(
[auth_id] => 1
[auth_section] => Client Data Base
[auth_parent_id] => 0
[auth_admin] => 1
[sub] => Array
(
[0] => Array
(
[auth_id] => 2
[auth_section] => Client Contact
[auth_parent_id] => 1
[auth_admin] => 1
)
)
)
[1] => Array
(
[auth_id] => 6
[auth_section] => All Back Grounds
[auth_parent_id] => 0
[auth_admin] => ,4
[sub] => Array
(
[0] => Array
(
[auth_id] => 7
[auth_section] => Edit Custom
[auth_parent_id] => 6
[auth_admin] => 1
)
)
)
[2] => Array
(
[auth_id] => 20
[auth_section] => Order Mail
[auth_parent_id] => 0
[auth_admin] => 1
[sub] =>
)
}
When I process the sub inner array
for($in=0 ; $in < count($auth); $in++){
$autsub = $auth[$in]["sub"];
for($g=0 ; $g<count($autsub); $g++){
echo $autsub[$g]["auth_id"];
}
}
it shows this error
Fatal error: Cannot use string offset as an array.........
how can I avoid that :(

The problem is that the last entry in the array (2) does not have a sub array, but you're trying to access it anyway. You'll need to check if the entry exists and if it's an array before looping over it. Here an example using foreach:
foreach ($array as $auth) {
if (!empty($auth['sub']) && is_array($auth['sub'])) {
foreach ($auth['sub'] as $sub) {
if (!empty($sub['auth_id'])) {
echo $sub['auth_id'];
}
}
}
}

You can test the offset type with the is_array() function. If you want a better answer, post the processing code.
Test if $auth[$in] and $autsub[$g] are arrays.

Related

CakePHP 4 Tree Behaviour manual setting left end right (JQuery ordering)

I have used the nestedSortable jQuery plugin (https://github.com/ilikenwf/nestedSortable) to order my categories with drag and drop. Every time a category is been ordered an Ajax request is going to the controller with a post of an array with the complete data of the categories. Everything is working as expected so for;
Array received in controller by Ajax post:
Array
(
[0] => Array
(
[item_id] =>
[parent_id] =>
[depth] => 0
[left] => 1
[right] => 8
)
[1] => Array
(
[id] => 1
[parent_id] =>
[depth] => 0
[left] => 2
[right] => 3
)
[2] => Array
(
[id] => 4
[parent_id] =>
[depth] => 0
[left] => 4
[right] => 5
)
[3] => Array
(
[id] => 2
[parent_id] =>
[depth] => 0
[left] => 6
[right] => 7
)
)
In my controller I have the following function to process:
public function reorder() {
$list = isset($_POST['_list']) ? json_decode($_POST['_list'], true) : null;
foreach ($list as $item) {
if (!empty($item['id'])) {
$category = $this->Categories->get($item['id']);
if (!empty($category)) {
empty($item['parent_id']) ? $category->parent_id = null : $category->parent_id = $item['parent_id'] ;
$category->lft = $item['left'];
$category->rght = $item['right'];
$this->Categories->save($category);
}
}
}
}
When I debug the categories before saving the lft and rght are set correctly, however after saving the lft and rght contain completly different values.
I assume that since TreeBehavior is implemented the save function is doing some checks and resetting lft and rght to other values? Anyway to skip this?

Where does phTagr set the requests 'search' parameter?

I am fairly new to cakePHP, i am trying to work with phtagr which uses cakePHP. I am trying to fix a pagination problem it has.
when i see the output of $this->request->params['search'] it has the pagecount value.
function beforeRender($viewFile) {
$this->search->iniitialize();
if(isset($this->request->params['search'])){
$this->data = am($this->defaults, $this->request->params['search']);
$this->data['page'] = 1;
}
} else {
$this->data = $this->defaults;
}
}
printing $this->request->params['search'] gives
Array ( [pageCount] => 27683 [current] => 0 [nextPage] => 1 [prevPage] => [baseUri] => /explorer/query [afterUri] => [defaults] => Array ( [page] => 1 [pos] => [show] => 24 [sort] => -date [view] => small ) [data] => Array ( ) [page] => )
How can i find out where the pageCount value is set?
Just search the source files for the string 'search'. For example using grep:
grep -r "'search'" /path/to/phTagr/
Results:
...
Controller/Component/SearchComponent.php: $this->controller->request->params['search'] = $params;
...
ie it's being set in the search component.

How to apply triming in beforevalidate method in cakephp appmodel

I use cakephp framework ,in that i post parent and child model data but i want to remove triming from that. My demo code is
Array
(
[Currency] => Array
(
[Code] => fgfg
[Name] => fg
[Description] =>
[NoOfDecimalPrecision] => 3
[CurrencyName] => bnjmhj
[CurrencySymbol] => hjh
[DecimalPrecisionName] => bjmh
[DecimalPrecisionSymbol] =>
[CurrencyScaleType_058] => 368
[IsPrefixSymbol] => 0
[IsInActive] => 0
[IsExchangeCurrency] => 0
[RevisionNumber] =>
)
[CurrencyDenomination] => Array
(
[0] => Array
(
[LineNo] => 1
[RowState] => 435
[DenominationName] => jhgjk
[DenominationDetails] => kgk
[DenominationValue] => 1
[RevisionNumber] => 1
[DenominationSequence] => 1
)
)
)
In my code currency is parent model and currencydenimination in child model so i want in both case.my data post dyanamic not static.so please suggest solution.

impresspages parse the renturn data from getMenuItems class

I want to build a custom menu output so I am calling this class:
$pages = \Ip\Menu\Helper::getMenuItems('menu1');
It returns this:
Array
(
[0] => Ip\Menu\Item Object
(
[title:protected] => Home
[pageTitle:protected] =>
[url:protected] => http://porto.gigaweb.me/home
[target:protected] =>
[selected:protected] =>
[current:protected] => 1
[children:protected] => Array
(
[0] => Ip\Menu\Item Object
(
[title:protected] => Lorem ipsum
[pageTitle:protected] =>
[url:protected] => http://porto.gigaweb.me/lorem-ipsumy
[target:protected] =>
[selected:protected] =>
[current:protected] =>
[children:protected] =>
[depth:protected] => 2
[disabled:protected] => 0
[blank:protected] => 0
)
[1] => Ip\Menu\Item Object
(
[title:protected] => about
[pageTitle:protected] =>
[url:protected] => http://porto.gigaweb.me/about
[target:protected] =>
[selected:protected] =>
[current:protected] =>
[children:protected] =>
[depth:protected] => 2
[disabled:protected] => 0
[blank:protected] => 0
)
)
[depth:protected] => 1
[disabled:protected] => 0
[blank:protected] => 0
)
)
I have tried to turn it into an array but it returns value like this [*title].
I want to loop through it and output the values but cant seem to do it.
It is an array of objects. Not an array of arrays. So you have to do something like this:
foreach ($pages as $menuItem) {
$page->getTitle();
}
You can find other methods of $page object here http://www.impresspages.org/menu-item
Please keep in mind, that you are getting MenuItem objects, not Page objects.

Wordpress store widget id's in an array

I'm trying to store widget id's from a specific sidebar in an array. But I can't seem to get the first part done.
I have the following array:
$results = get_option('sidebars_widgets');
print_r($results); /* Gives me the following array */
Array
(
[orphaned_widgets_1] => Array
(
[0] => search-2
[1] => recent-posts-2
[2] => recent-comments-2
[3] => archives-2
[4] => categories-2
[5] => meta-2
)
[sidebar-footer] => Array
(
)
[wp_inactive_widgets] => Array
(
)
[sidebar-main] => Array
(
)
[Shortcodes] => Array
(
[0] => pages-2
[1] => widget-id
[2] => another-id
[3] => yet-another
)
[array_version] => 3
)
Then, I want all of the Shortcodes widget id's in array. Like doing:
$shortcode_widget_id = array();
foreach { code here to put the 4 id's in the $shortcode_widget_id array }
But how do I get the list of only the Shortcode id's, not the other ones?
$shortcode_array = $presentarray['Shortcodes'];
$shorcode_keys = array_keys( $shortcode_array);
print_r($shorcode_keys );
is this you are after ?

Resources