-1

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.

innov8
  • 2,093
  • 2
  • 24
  • 31
  • 1
    We really need to see some sample data to understand what you're referring to. Give us a `var_export($args);`, and also the output of your `echo` statement to compare with. Maybe even more than one example would really help. – ADyson Jul 12 '22 at 10:35
  • 1
    Thanks for the update. `$dm = $args[0]['date_modified'];` ought to solve your problem, in that case. – ADyson Jul 12 '22 at 13:28
  • 1
    Apologies it should be `$dm = $args[0]->date_modified'` (since it's an object within the outer array, rather than an associative array). Working demo: https://3v4l.org/hdYvc – ADyson Jul 12 '22 at 14:01

1 Answers1

1

This answer really came from ADyson.

If picking up PHP either for the first time, or when rusty, and having trouble with arrays or object, use var_export or var_dump to check the structure is as expected, and see what is going on.

innov8
  • 2,093
  • 2
  • 24
  • 31