How to print a string array on the screen?

Asked

Viewed 286 times

1

How do I print this SQL query on the screen in string form?

$current_user = wp_get_current_user();

$resultado = $wpdb->get_results( "SELECT meta_value FROM $wpdb->usermeta WHERE user_id = '$current_user->ID' AND meta_key = '_jm_candidate_field_clocknow_user_btn'" );

This query is returning the following result:

Array ( [0] => stdClass Object ( [meta_value] => value_1 ) ) 

I’d like you to just show the value value_1.

  • 2

    echo $resultado[0]->meta_value

  • Thanks rray! Funfo :)

  • What if I want to check the $result on an IF. Example: if ( $result == value 1) { do one thing } Else { do another thing }. How do I do it? I’m a beginner in PHP.

  • It’s the same thing.

1 answer

4


As the message itself says:

Array ( [0] => stdClass Object ( [meta_value] => value_1 ) ) 

Its variable $resultado is the type Array, containing a zero value of the type stdClass, which has an attribute meta_value. To access it, we must first recover the respective object:

$resultado[0]

With the object, we can trace its attribute through the operator ->, as below:

$resultado[0]->meta_value;

If you still want to perform some operation with this value, it is more didactic to store it in a variable:

$value = $resultado[0]->meta_value;

Here, your variable will have the value value_1, following the example given in the question.

  • Thanks Anderson, that’s what I needed! (Just missed having stored inside a variable).

  • 1

    It is not a necessary process. Having something like if ($resultado[0]->meta_value == 1) { ... } is perfectly valid. Storing the value in another variable just leaves more didactic and facilitates the understanding of beginners.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.