Replace array value in multidimensional array - basic Q - multidimensional-array

my question seems pretty basic, but i can't get around it. I try to replace the second value in a multidimensional array with another array, which happens, but i loose it in my foreach loop. Everything is just an example. So failures like missing quotes happend in here. The original is a function inside a class. Thanks!
This is how the config array looks like:
$config_arr = array(
'foo_a' => array( 'foo_a_singular', 'foo_a_plural', array('assign_me_aa', 'assign_me_ab') );
'foo_b' => array( 'foo_b_singular', 'foo_b_plural', 'assign_me_b')
);
This how the processing happens:
function process_foo( $config_arr ) {
foreach ( $config_arr as $config_data ) {
$replacement_data = array( 'bar_me', 'bar_her', 'bar_some' );
$config_data[1] = $replacement_data;
var_dump($config_data);
}
print_r( $config_arr );
}
The var_dump shows me that i replaced/added the data, but the print_r tells me that nothing happend.

I'll brush up on this answer in a minute, but either the scope is local for config_data or you can only read and not write. I've had this problem before. you have to use the actual config_arr in the loop, this can be accomplished with a counter variable. someone else might know a better/different way to do this.

Point is that I tried to use a numerical index $config_data[1] for a associative array: 'foo_b' => array( 'foo_b_singular', 'foo_b_plural', 'assign_me_b'), which will never work. You have to use stuff like key() or array_keys().

Related

Filtering WooCommerce Products by $_GET

What I need to be able to do is apply a value for an attribute in the URL, so only the products match that attribute are displayed. I don't want to do any widgets or other visible filters on the page.
For this I presume I would need to use one of the webhooks, and filtering out all products that are about to be displayed.
Can anyone advise which hook will be best in this case and a simple explanation on how the triggered function will return the new array of products?
Thanks in advance!
NB: I also want to query a custom attribute, which does not have any terms, just a straight key/value.
UPDATE 1
I'm playing with two techniques; one is very reliable, and that's basically to use:
if (!$product->attributes || $product->get_attribute( 'testKey' ) != $_GET["testKey"]) {
//return;
}
at the top of content-product.php, but of course WooCommerce will still say the original value for found_posts. Certainly not ideal.
I've come across that something like this should work in functions.php:
function testFilter($meta_query) {
$meta_query[] = array (
'key' => 'testKey',
'value' => 'testVal',
'compare' => '='
);
return $meta_query;
}
add_filter( 'woocommerce_product_query_meta_query', 'testFilter', 9 );
Except it doesn't, returns no results, doesn't matter if I use LIKE, EXISTS etc. Am I using it wrong?
UPDATE 2
I'm not going to say this is the answer, as this only seems to look for one value within a group of custom attributes, but this result has helped.
add_filter( 'wpv_filter_query', 'wpv_filter_color_attribute' );
function wpv_filter_color_attribute( $query_args) {
$tax_query = array();
$tax_query['taxonomy'] = 'pa_size';
$tax_query['field'] = 'term_id';
$tax_query['terms'] = $_GET['pa_size'];
$query_args['tax_query'] = array($tax_query);
return $query_args;
}
You should replace "pa_size" with your attribute taxonomy slug, also $_GET['pa_size'] with the right URL parameter.
You can filter products by attributes using above code. I have not tried this. But, this may help you.

bbpress user-roles or capabilities per forum

I'm trying to setup a bbpress with extended user capabilities.
The problem
My goal is that users need to have different capabilities in each forum, i.e:
UserA can't access ForumW
UserA can only read topics and replies in ForumX
UserA can create topics and write replies in ForumY
UserA can moderate ForumZ
Plugins
These are the plugins I tried so far, but without success:
Ultimate Member, official 1.7 and the new 2.0 version
https://ultimatemember.com/
They claim that they're working on a groups extension for UltimateMember v2, which somehow looks promising, but as of now there's no release date and I still don't know if this extension is going to solve my problem.
itthinx Groups plugin
http://docs.itthinx.com/document/groups/
Allows me to assign multiple groups to users and forums, but there's still a catch.
First attempt
Since itthinx Groups plugin allows me to assign multiple groups to UserA, which is great, it's still not solving my issue.
So, I tried something like this:
ForumX has the following groups assigned: ForumX_readers, ForumX_writers, ForumX_moderators
UserA has the following groups assigned: ForumX_readers, ForumY_writers, ForumZ_moderators
But the problem is, since UserA belongs to groups that have publish_replies and moderate capabilities, he has full access to ForumX.
So what I need is an intersection of the forum-groups and the user-groups - which in this example is ForumX_readers.
The promising part, but...
I digged into the code of the plugin and found the line that handles the capabilities of the user based on his assigned groups and quickly tried to get the current forum groups, to implement the intersection.
Unfortunatelly I was not able to access the global $post, the $_GLOBALS['post'] nor the $_REQUEST[] variables in this part of code. Neither directly nor with an apply_filters() function, that I implemented into the part of the code myself.
UPDATE:
I was able to get the ID with get_posts() and the slug of the current forum/topic.
So, my question
Is there any solution to my first attempt, which I may have overseen?
If not, is there maybe any other plugin that can solve my problem that I'm not aware of?
Or is something like that even impossible in bbpress?
After some further research and trial & error, I finally figured it out.
First step to do is to set up the capabilities, which in my case look something like this.
In the plugins directory, there is the file core/class-groups-user.php. The init_cache() function retrieves the assigned groups to the user, and sets the according capabilities.
To not mess around to much with the core-plugin, I applied a filter to the $group_ids variable which can be found in line: 415.
foreach( $user_groups as $user_group ) {
$group_ids[] = Groups_Utility::id( $user_group->group_id );
}
// added this line
$group_ids = apply_filters('filter_user_group_ids', $group_ids);`
I then created a new plugin, which hooks into this filter.
add_filter('filter_user_group_ids', 'dnmc_filter_groups', 10, 1);
function dnmc_filter_groups($user_group_ids) {
$forum_id = dnmc_get_forum_id();
if(!$forum_id) return $user_group_ids;
$forum_group_ids = Groups_Post_Access::get_read_group_ids( $forum_id);
$user_restricted_forum_group_ids = array_intersect($user_group_ids, $forum_group_ids);
return $user_restricted_forum_group_ids;
}
function dnmc_get_forum_id() {
$args_topic = array(
'name' => basename( untrailingslashit( rtrim($_SERVER['REQUEST_URI'], '/') ) ),
'post_type' => 'topic',
'post_status' => 'publish',
'numberposts' => 1
);
if($topic = get_posts($args_topic)) {
return $topic[0]->post_parent;
}
$args_forum = array(
'name' => basename( untrailingslashit( rtrim($_SERVER['REQUEST_URI'], '/') ) ),
'post_type' => 'forum',
'post_status' => 'publish',
'numberposts' => 1
);
if($forum = get_posts($args_forum)) {
return $forum[0]->ID;
}
return false;
}

Php error Notice:Undefined offset:255715

hello I'm working on a symfony2 project , and when I'm trying to create associative array this issue happend => Notice: Undefined offset :25715
My code editor alert me that the error come from when I'am create my assoiative array $Tableau_comptes_dependants
here is code
foreach ($tableau_compte_fictifs as $tableau_compte_fictif) {
$Tableau_id_compte_fictifs[] = $tableau_compte_fictif["id"];
}// this array content two value 25715 and 31170
foreach ($Tableau_id_compte_fictifs as $Tableau_id_compte_fictif) {
$Mes_comptes_reels_dependants = $mes_comptesRepo-
>all_client_compte_dependant($Tableau_id_compte_fictif);
if (count($Mes_comptes_reels_dependants) > 0) {
foreach ($Mes_comptes_reels_dependants as
$Mes_comptes_reels_dependant)
{
if (!in_array($Mes_comptes_reels_dependant,
$Tableau_comptes_dependants[$Tableau_id_compte_fictif]))
{
$Tableau_comptes_dependants[$Tableau_id_compte_fictif[] =
$Mes_comptes_reels_dependant;
}
}
}
}
return new JsonResponse(
array(
'code' => 200,
'result' => true,
'comptes' => $Tableau_id_compte_fictifs,
)
);
please let me know what I'am doing wrong
The notice is generated from your call to in_array:
if (!in_array($Mes_comptes_reels_dependant,
$Tableau_comptes_dependants[$Tableau_id_compte_fictif]))
You try to access your array on an index that does not exist, 25715, since this is the value of your variable which you pass between the square brackets.
You should check first, if an index exists with isset, before accessing it.
That said, I think your code has a design flaw if you run into problems like this. You should try to refactor it or talk with your co workers about how to simplify it. I must admit, that I, although I could, won't try solving this problem for you, since your variable names are basically unreadable.

How does one have to call the function in this example for JMSPaymentCoreBundle?

How would detailsAction() have to be called in the example provided in the documentation for JMSPaymentCoreBundle?
It makes use of an order object, which in the function definition is passed as an argument.
public function detailsAction(Order $order)
{
$form = $this->getFormFactory()->create('jms_choose_payment_method', null, array(
'amount' => $order->getAmount(),
[...]
Am I right in assuming that the function can only work when passing an order object at the time it is being called? Are there any other ways of achieving this other than doing something like
return $this->forward('MyBundle:Payment:details', array(
'order' => $order,
));
Yes, you are right. The detailsAction requires an Order object in order to work. So, you will have to have one created by the time you get to this action, otherwise it will throw a 404 error (because the route without an order does not exists).
You should create your own Order entity, which then you can persist to the database (once it's started, that is, the submitted form is correct).
Good luck.

Illegal offset type in isset or empty in EntityChoiceList.php line 273

Within my Symfony2 project I've attempted to dynamically generate the entities used within my form type, by-passing the use of query builder etc.
To he entity choices property I am supplying an array of entities to be used. On page load everything seems fine and the correct content is displayed. However on form submission I get
Illegal offset type in isset or empty in EntityChoiceList.php line 273
at ErrorHandler ->handle ('2', 'Illegal offset type in isset or empty',
'..../Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php', '273', array('key' => object(myEntity))) in ..../Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php at line 273
.....
return isset($entities[$key]) ? $entities[$key] : null;
.....
What has me stumped is if I add var_dump(isset($this->entities[$key]));exit; above this line I am returned 'bool(true)' which to me means the key does exist.
As background I have attempted to extend the EntityType, for ease within my project and added:
public function getDefaultOptions(array $options)
{
$defaultOptions = array(
'em' => null,
'class' => 'Acme\TestBundle\Entity\myEntity',
'property' => null,
'query_builder' => null,
'choices' => $this->myEntityArray,
);
$options = array_replace($defaultOptions, $options);
$defaults = parent::getDefaultOptions($options);
return $defaults;
}
Has any one any ideas why I getting this error, or am I going about my issue all wrong anyway, with trying to pass an array of entities to choices?
If you're getting this while trying to remove an element from an ArrayCollection it's probably because you've typed:
$list->remove($item) instead of $list->removeElement($item)
I'm guessing you already solved this some other way, and this isn't a real answer either.
But I'm guessing either $entities isn't an array on that point, or $key isn't a scalar value.
For debugging you should use:
<?php
if (!is_array($entities) || !is_scalar($key)) {
var_dump($key, $entities));exit;
}
How you now tested this, it would stop on the first pass in that function. Symfony Forms use quit a lot of recursion, so an exit in any function usually doesn't help you.

Resources