简体   繁体   中英

Retrieve single data from an array

Hi Im a novice in php and Im trying to display a single data from a column of records. I cant find any id/key on the table that could help me to pull out a data using the get_row method, instead Ive used get_results to display all records of that column then ill display it base on thier index number. The problem is it only displaying the word "Array" I been trying to display the index number 4 but I only get error.

$meta_value = $wpdb->get_results( $wpdb->prepare("SELECT meta_value FROM  wp_gf_entry_meta WHERE entry_id=%d", $entry_id));         
foreach($meta_value as $data){
    $i++;
    if($data[$i]==4){
        echo $data['metavalue'];
    }
 }

Heres the architecture of the array.

169 

Array (

[0] => stdClass Object
    (
        [meta_value] => Patrick
    )

[1] => stdClass Object
    (
        [meta_value] => Mantin
    )

[2] => stdClass Object
    (
        [meta_value] => 069434
    )

[3] => stdClass Object
    (
        [meta_value] => Teacher
    )

[4] => stdClass Object
    (
        [meta_value] => CW Jefferys CI
    )

[5] => stdClass Object
    (
        [meta_value] => 9
    )

[6] => stdClass Object
    (
        [meta_value] => a:1:{s:28:"gravityformsuserregistration";a:1:{i:0;s:1:"1";}}
    )

)

Thank you so much in advance, God Bless!

You are getting value as OBJECT and you are accessing as ARRAY . You can use ARRAY_A to get results as ARRAY . Try the below code.

Also key is meta_value and you accessing as metavalue .

$meta_value = $wpdb->get_results( $wpdb->prepare("SELECT meta_value FROM  wp_gf_entry_meta WHERE entry_id=%d", $entry_id),ARRAY_A);         
foreach( $meta_value as $key => $data ){
    if( $key == 4 ){
        echo $data['meta_value'];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM