i got a php function in Wordpress that get serialized user meta : like this
function get_allowwed_tournees_ids(){
$tournees = get_user_meta($this->ID, 'tournees',true);
$tournees = unserialize($tournees);
$tournees_ids = array();
foreach ($tournees as $key => $value) {
array_push($tournees_ids, $key);
}
var_dump($tournees_ids);
return $tournee_ids;
}
get_allowwed_tournees_ids() is in a class that extends WP_User
and when i want to call it :
$id_tournees = $current_user->get_allowwed_tournees_ids();
var_dump($id_tournees);
the var_dump inside the function returns me the unserialised array, and the second var_dump outside the function returns null.
Any idea ?? Thanks !
Because you are returning $tournee_ids which is never defined. I think you should
return $tournees_ids;
Related
I'm working on magento site and facing strange error when array values assign inside function and retrieve outside of function.
//define array
$ctall=array();
//function for array value assign
function printtest($fg){
//global variable
global $ctall;
//just assign to array
$ctall[rand(10000,100000)]=$fg;
//this var dump shows array with vaues when function calling
// var_dump($ctall);
}
i call the function here inside an another function
$categoryTree = Mage::getResourceModel('catalog/category')->getCategories($categoryId, 0, true);
$printCategories = function($nodes) use (&$printCategories) {
foreach ($nodes as $_category):
$ctdf=$_category->getId();
$categoryn = Mage::getModel('catalog/category')->load($ctdf);
if($ctdf!='' && $categoryn->getIsActive()):
//here call to function by passing a value
printtest($ctdf);
$printCategories($_category->getChildren());
endif;
endforeach;
};
$printCategories($categoryTree);
//sleep(10);
// i try to get array results here but it shows empty
var_dump($ctall);
Anyone know how to fix this, i tried hours without luck. Thank You
remove all declaration of $ctall, and try this:
//remove define array, don't define it
// $ctall=array();
function printtest($fg){
if(!isset($GLOBALS['ctall'])){
$GLOBALS['ctall'] = array();
}
//assign to global
$GLOBALS['ctall'][rand(10000,100000)]=$fg;
}
on outside, dump like this:
var_dump($GLOBALS['ctall'])
Try to push instead of assigning.Try this:
$ctall[][rand(10000,100000)]=$fg; //notice the empty square brackets
you can try this also:
function printtest($fg){
global $ctall;
$new_array =array();
$new_array[rand(10000,100000)] = $fg;
array_merge($ctall, $new_array);
}
I have the following codes.
<?php
class Reg {
private $pros = array();
public function __set($key,$val) {
$this->pros($key)= $val;
}
public function __get($key) {
return $this->pros($key);
}
}
$reg= new Reg;
$reg->tst="tst";
echo $reg->tst;
?>
But when executing this script I got the error following.
Fatal error : can't use method return value in write context in line 5
I believe that to add an element to array is possible like the above.
$array = array();
$array('key')='value';
Please make clear that I was wrong.
Thanks
The is because of you are trying to set a functions return value. $this->pros($key) means calling pros($key) function. Not setting a value to $pros array.
The syntax is wrong. Setting values to array be -
$array['index'] = 'value';
Change
$this->pros($key)= $val; -> $this->pros[$key]= $val;
and
return $this->pros[$key];
Working code
$this->pros[$key] = $value;
OR
$keys = array($key);
$this->pros = array_fill_keys($keys,$value);
The array_fill_keys() function fills an array with values, specifying keys.
Syntax:
array_fill_keys(keys,value);
foreach( $items as $item) {
$taskid = (int) $goal['goal_id'];
$items[$i]['tasks'] = array();
$items[$i]['tasks'] = array_filter($tasks, function($task, $taskid){
return $task['task_id'] == $taskid;
});
Why is $taskid not being passed to the array_filter function, it returns null if echoed from within but if echoed just after it is set it gives the correct value e.g.
foreach( $items as $item) {
$taskid = (int) $goal['goal_id'];
echo $taskid;
Will return whatever the integer is
The return part of the function also works if I manually set a value i.e
return $task['task_id'] == 2;
Guidance appreciated
The issue is variable scope and function arguments.
First, array_filter expects a function with a single argument, that argument is the value in the position in the array. It doesn't handle keys.
You set $taskid = (int) $goal['goal_id']; outside of the anonymous function, and you have a local variable of the same name, which is null because array_filter only passes one argument.
foreach( $items as $item) {
$taskid = (int) $goal['goal_id'];
$items[$i]['tasks'] = array();
# Per the OP, you can pass the necessary variable in via 'use'
$items[$i]['tasks'] = array_filter($tasks, function($task) use($taskid){
return $task['task_id'] == $taskid;
});
}
Thanks guy once you pointed out it was vairiable scope and anonymous functions it was easy enough to fix by referencing in function closure.
$items[$i]['tasks'] = array_filter($tasks, function($task) use(&taskid){
return $task['task_id'] == $taskid;
});
The array_filter function passes in an arrays values into a callback function one-by-one. You cannot pass other parameters in with the anonymous callback function like you are attempting to do.
A valid example would be:
$array = ["Bob","Sam","Jack"];
print_r(
array_filter(
$array,
function($value) {
return ($value !== 'Jack');
}
)
);
Returns
Array ( [0] => Bob [1] => Sam )
Is it possible to do in one line calling a method that returns an array() and directly get a value of this array ?
For example, instead of :
$response = $var->getResponse()->getResponseInfo();
$http_code = $response['http_code'];
echo $http_code;
Do something like this :
echo $var->getResponse()->getResponseInfo()['http_code'];
This example does not work, I get a syntax error.
If you're using >= PHP 5.4, you can.
Otherwise, you'll need to use a new variable.
What you can do is to pass the directly to your function. Your function should be such that if a variable name is passed to it, it should the value of that variable, else an array with all variables values.
You can do it as:
<?php
// pass your variable to the function getResponseInfo, for which you want the value.
echo $var->getResponse()->getResponseInfo('http_code');
?>
Your function:
<?php
// by default, it returns an array of all variables. If a variable name is passed, it returns just that value.
function getResponseInfo( $var=null ) {
// create your array as usual. let's assume it's $result
/*
$result = array( 'http_code'=>200,'http_status'=>'ok','content_length'=>1589 );
*/
if( isset( $var ) && array_key_exists( $var, $result ) ) {
return $result[ $var ];
} else {
return $result;
}
}
?>
Hope it helps.
Language itself does not support that for an array.
In case you can change what getResponseInfo() return:
You can create simple class, which will have array as an constructor parameter. Then define magical getter which will be just pulling the keys from the instance array
function __get($key)
{
return #content[$key]
}
Then you'll be able to do
echo $var->getResponse()->getResponseInfo()->http_code;
// or
echo $var->getResponse()->getResponseInfo()->$keyWhichIWant;
What i wrote is just proposal. The real __get method should have some check if the exists and so
I have a static method 'findAll' on a model which basically gets all rows with certain criteria. This method works fine and I can call it using:
$m::findAll();
Where $m is the model name as a variable. I can output this and it returns correct results. However, when assigning this to a variable in the Zend_View object, as:
$this->view->viewvariable = $m::findAll();
I get the error:
Zend_Db_Table_Exception: Too many
columns for the primary key
Any ideas why?
Find all function:
final public static function findAll($where = false, array $options = array()) {
$object = new static();
if (!empty($options)) $options = array_merge($object->options, $options);
else $options = $object->options;
$run = $object->buildDefaultSelect($where, $options);
$rows = $run->fetchAll();
if ($options['asObject'] == true) {
$result = array();
foreach ($rows as $r) {
$class = new static();
$class->setInfo($r);
$result[] = $class;
}
return $result;
} else {
if (count($rows) > 0) return $rows;
else return array();
}
}
Note: This function works fine everywhere apart from when assigning to a view variable. If I run the below (not assigning it to a view variable), it shows the correct array data.
var_dump($m::findAll($module['where'], $module['options']));
exit;
In my view (I have replaced the actual name with viewvariable for the sake of this post):
<?php foreach($this->viewvariable as $item) { ?>
//Do some echoing of data in $item
//Close foreach
I doubt the issue is with Zend_View. It's hard to tell without seeing your code, but my guess is that findAll() is using the Zend_Table_Db find() function incorrectly.
To my knowledge, the only place that throws that exception is the find() function on Zend_Db_Table_Abstract.
Perhaps, inside the findAll() function (or in a function it calls) you're doing one of these:
$zendDbTable->find(1,2) //is looking for a compound key
$zendDbTable->find(array(1,2)) //is looking for two rows
When you really want the opposite.