Have not coded PHP for many years, but trying a wordpress plugin.
I am having a strange problem with some array values returned by a function.
This specific problem does not matter, the problem really is if dealing with values returned by functions, how do you check the structure returned?
I have an array built with the statement:
$args = array(shortcode_atts(OFP_Posts::get_default_args($dev_mode), $atts));
and when I pass that array into a static function, I get this strange behaviour:
static function get_recent_posts($args = array()) {
$dm = $args['date_modified'];
$jargs = json_encode($args);
echo "<p>at top, mod date:$dm:</p><p> $jargs</p>";
I get a different value for 'date_modified' from the '$dm' variable, to that in the json. The one in the json is what I would expect.
output from echo:
at top, mod date::
[{"dev_mode":"simpl","limit":5,"offset":0,"order":"DESC","orderby":"date","cat":[],"tag":[],"taxonomy":"","post_type":["post"],"post_status":"publish","ignore_sticky":1,"exclude_current":1,"excerpt":false,"length":10,"date":" ","date_relative":"","date_modified":"yes","css":"","cssID":"","css_class":"","before":"","after":""}]
Note: $dm seems to be an empty string, and yet "date_modified" is set to "yes".
I must be doing something fundamentally wrong. Any suggestions appreciated.
@ADyson suggested using var_export($args)
... which I think has provided the answer. The data returned from short_code_atts
does not make a simple array.
Thank you @ADyson
array ( 0 => (object) array( 'dev_mode' => 'simpl', 'limit' => 5, 'offset' => 0, 'order' => 'DESC', 'orderby' => 'date', 'cat' => array ( ), 'tag' => array ( ), 'taxonomy' => '', 'post_type' => array ( 0 => 'post', ), 'post_status' => 'publish', 'ignore_sticky' => 1, 'exclude_current' => 1, 'excerpt' => false, 'length' => 10, 'date' => ' ', 'date_relative' => '', 'date_modified' => 'yes', 'css' => '', 'cssID' => '', 'css_class' => '', 'before' => '', 'after' => '', ), 'date_modified' => 'maybe: ', )
Note: Solved. Following the suggestion to add var_export, I had all that was needed to fix the errors. Lesson is data may not be what you expect, and var_dump
or var_export
are the ways to check.