array_keys error while running artisan basset - php

I am receiving the proceeding error exception when I run php artisan basset --tidy-up from the command line.
[ErrorException]
array_keys() expects parameter 1 to be array, object given
basset [--delete-manifest] [--tidy-up]
I was unfortunate in finding any details online.

Although I've not used Basset, the reason for this error is that you are passing in an object, rather than an array, for the first argument of array_keys.
For example, were I to have an array as follows:
$myGreatArray = array('first' => 'foo', 'bar' => 'sup');
And then pass it in to array keys:
print_r(array_keys($myGreatArray));
I'd get this as the output:
Array
(
[0] => first
[1] => bar
)
However, I can't do the same thing with an object, even if it's structured the same way.
In other words, we can't do something like this (and not just because you need to var_dump objects, but also because you need to pass in arrays to array_keys()):
$myGreatArray = new stdClass;
$myGreatArray->first = 'foo';
$myGreatArray->bar = 'sup';
print_r(array_keys($myGreatArray));
You'll get an error similar to what you're seeing, although what you're seeing appears to be in a format of an exception handled by Basset. You can also see it when running a script from other than the command line, depending on your level of error reporting.
Edit:
I just pulled up the source for Basset's command line script, and it looks like you're getting the error on line 120, which reads as follows:
$collections = array_keys($this->environment->all()) + array_keys($this->manifest->all());
You might want to check the way your manifest and environment are structured.

Related

Array to String conversion on phpredis zScan

When using a sorted set and doing a zScan call on it, I get a Notice for Array to String conversion and I do wonder where that comes from. Does anyone have an idea?
This is the code:
$redis = new Redis();
$redis->connect('127.0.0.1');
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
$redis->zAdd('check', 0, array('a'));
$it = NULL;
$redis->zScan('check', $it);
Tried that with Redis::SERIALIZER_IGBINARY too but got the same results. It must be the value set because if I do a $redis->zAdd('check', 0,'a'); everything is fine.
I am using PHP 7.1.12 with php-redis 3.1.4
PHP doesn't allow array keys to be arrays. The following script will cause an illegal offset type error.
<?php
$array = [
['one'] => 0,
['two'] => 1
];
?>
A few phpredis commands "zip" the responses which means the actual Redis reply of [key, value, key, value] is turned into the PHP like associative array [key => value, key => value].
You're running into this problem with zScan as internally when it is attempting to zip the values, PHP is forced to use the value "Array" as an array key cannot be an array.
You would see the same problem if you tried to do this:
$redis->zRange('check', 0, -1, true);
I didn't step through the code but my guess is that the problem happens precisely here.
I don't know anything about what you're trying to build, so it's difficult to say what a proper workaround might be. You can always disable serialization before attempting to execute ZSCAN or ZRANGE WITHSCORES but then you'd need to deal with the serialized data manually.

Warning: array_keys() expects parameter 1 to be array, null given, although array appears properly formed?

I'm working with user data in WordPress, but I think this error is more of a general PHP issue on my end.
I use a function wp_update_user() which takes an array of keys and values corresponding to certain user fields, like display_name.
The code responsible looks as follows (note that I'm hardcoding the value for display_name for debugging purposes):
$returnValue = wp_update_user(array( 'ID' => $user->ID, 'display_name' => 'Test 123' ));
if (is_wp_error($returnValue)) {
print_r($returnValue);
} else {
echo "User update was a success, ID returned is " . $returnValue;
}
The if statement would output an error object if there is an error. However, the value is updating in the database as expected, and no error is returned from WP. The server, however, is giving me a series of warnings, which begin with Warning: array_keys() expects parameter 1 to be array, null given in /srv/www/mysite.com/current/web/wp/wp-includes/user.php on line 1993. I looked up the function array_keys() but as far as I can tell, I'm passing a correctly-formed array as required. This warning does stem from the wp_update_user() function.
As stated above, the actual code seems to work and do what I want it to do, so this warning isn't actually impeding any functionality. I suppose I can hide this warning output but in the interest of best practices I would like to get to the bottom of it. How can I solve this?
Pretty sure its not the parameter you pass that is wrong , but an internal WP process.Try calling that with a plain integer as id of an already existing user

end() expects parameter 1 to be array - php

I'm retrieving the email list from a MySQL database along side with the IDs of the users it gets something like this
Array (
[0] => Array ( [ID] => 1 [Email] => email1 )
[1] => Array ( [ID] => 2 [Email] => email2 )
)
and while trying to test for the value of the last email "email2" I used
end(end($array_sample));
this used to work on my old server running PHP 5.0 and stopped at the new one running PHP 5.6
Was there something I did wrong or is it a php version?
I basically changed the whole approach to get the site to do what it was meant to do any how, but I still would like to learn about the end(end(array)) issue.
end() function needs to get array by reference, so it can't be a result of other function, because you get following error:
Only variables should be passed by reference
To avoid it assign result of inner end() to variable and then use end() on this variable:
$tmp = end($array);
$result = end($tmp);
And you probably don't get any error in previous version of PHP due to error_reporting set to quiet them.
According to documentation:
Prior to PHP 5.4.0 E_STRICT was not included within E_ALL, so you
would have to explicitly enable this kind of error level in PHP <
5.4.0.
As far as I know, your code should had never worked:
Only variables should be passed by reference
As documentation explains:
This array is passed by reference because it is modified by the
function. This means you must pass it a real variable and not a
function returning an array because only actual variables may be
passed by reference.
What has changed is the severity of the error. It has been a fatal error, a strict standards notice and a regular notice. Between 4.3.0 and 5.0.4 is just failed silently.
Most likely the error went unnoticed until you upgraded and an actual error message was triggered.
You have an end within another end, the inner one returns last element of array, the outer one is expecting an array not a single value

best way append named array to array in PHP 5.2

I got used to this notation for creating empty arrays and add named elements to them when needed;
$array = [];
// in case there is an error
$array["error"][] = "new error message as element 0 of $array['error']";
Now I learned that the [] notation for arrays does not work in older versions of PHP, like PHP 5.2.
Instead I have to do;
$array = array(
"error" => array()
);
array_push($array["error"], "new error message as element 0 of $array['error']");
This way is a little bit inconvenient in my case because the great thing about the first code snippet is that the "error" entry in $array is only created when there is an actual error, whereas in the latter case the entry (although empty) exists either way.
Is there a way to get similar 'functionality' (i.e. specifying/adding named elements when needed, not at initialisation) in a way that is also easily readable in PHP 5.2?
EDIT:
The first code snippet in the original post was reading $array = array[];. The author corrected it after I posted this answer.
The first code snipped is incorrect. There is no such thing as array[]. The correct syntax is array().
$array = array();
// in case there is an error
$array["error"][] = "new error message as element 0 of $array['error']";
You don't have to worry about PHP versions. This syntax always worked on PHP since its dawn and it will probably work forever. Keep using it.
The first way of creating array in PHP is incorrect. This syntax works in PHP5.2 below too, so you dont need to worry about it. You don't need to use array_push and simply do following.
The correct syntax is:
$array = array(); // notice it doesn't to array[]
// add error when there is one
$array["error"][] = "new error message as element 0 of $array['error']";

PHP - Function name from variable

Okay, I've found a possible solution for this, but for some reason, I can't make it work in my application. Apparently, if I have a variable which contains a name function, I could use
<?php echo $variable(); ?>
to output the function with the same name.
I'm using Codeigniter. It has a function in its Form helper to output a text field, which is
<?php form_input(); ?>
I have a variable
<?php $instance['taxon_field'] = 'form_input'; ?>
If I echo out this variable, I do get the needed value, 'form_input'. However, as soon as I try to echo
$instance['taxon_field']()
I get a warning:
Message: Illegal string offset 'taxon_field'
and a fatal error:
Call to undefined function p()
I am really clueless here, because echoing only the variable gives 'form_input', but echoing $variable() only gives 'p'.
Where am I doing wrong?
The actual problem here is that $instance is not an array, but a string. Judging from the error message, it's a string whose value starts with p.
The syntax $var[$key] is used not only to access array elements but also to index into strings, where $var[0] would be the first character (actually, byte) of $var etc. If $instance is a string and you write $instance['taxon_field'] then PHP will try to convert 'taxon_field' to an integer in order to index into the string. This results in 0 as per the usual conversion rules, so the whole expression gets you the first letter of the string.
Assuming that the string starts with p it's then pretty obvious why it tries to call a function with that name.
Use call_user_func()
call_user_func($instance['taxon_field']);
The confusion created is actually my own fault because I failed to provide some aditional information which I thought was not important, but turned out to be crutial. My $instance[] array is actually a result of a foreach loop (two of them, to be precise) and is a part of a bigger multidimensional array. The actual code is more complicated, but I'll try to represent it right:
<?php
$bigger_array = array(
0 => array(
'field_one' => 'value_one',
'field_two' => 'value_two',
'field_three' => 'new_function'
),
1 => array(
'field_one' => 'new_value_one',
'field_two' => 'new_value_two',
'field_three' => 'echo'
)
);
function new_function()
{
echo 'New function called.';
}
foreach($bigger_array as $instance)
{
$name = $instance['field_three'];
$name('Hello World!');
}
?>
This will output the following:
New function called.
Fatal error: Call to undefined function echo() in /opt/lampp/htdocs/bla.php on line 69
In other words, the newly defined function works fine, but the built-in 'echo' doesn't.
This is actually not my original problem, this is something that I've encountered while trying to debug the initial issue. And the original problem is that creating a function from a single-dimensional array works okay. whereas creating a function from a multi-dimensional array within a foreach loop transforms the array into a string with the value of its last member.
Now, I'm still not really able to fully answer my question, but I think information I'm giving could lead to a solution. In the simplified example that I gave here, why am I getting the message that echo() function is not defined, while the new function works fine?

Categories