Zoker
Zoker

Reputation: 2059

Get a value of an class in an array

Im a php beginner and want to do the following: When I var_dump a function called get_values(), this is the output:

array(2) { 
    [0]=> object(stdClass)#1033 (9) { 
        ["sub_id"]=> string(1) "1" 
        ["sub_email"]=> string(18) "[email protected]" 
        ["sub_name"]=> string(7) "John" 
        ["sub_last_name"]=> string(0) "" 
        ["sub_key"]=> string(34) "..." 
        ["created"]=> string(19) "2015-02-17 11:37:49" 
        ["status"]=> string(1) "1" 
        ["sola_nl_mail_sent"]=> string(1) "0" 
        ["sola_nl_mail_sending_time"]=> string(19) "0000-00-00 00:00:00" 
    } 
    [1]=> object(stdClass)#858 (9) { 
        ["sub_id"]=> string(2) "10" 
        ["sub_email"]=> string(19) "[email protected]" 
        ["sub_name"]=> string(8) "Doe" 
        ["sub_last_name"]=> string(0) "" 
        ["sub_key"]=> string(34) "..." 
        ["created"]=> string(19) "2015-02-24 03:04:58" 
        ["status"]=> string(1) "1" 
        ["sola_nl_mail_sent"]=> string(1) "1" 
        ["sola_nl_mail_sending_time"]=> string(19) "0000-00-00 00:00:00" 
    }
}

Now I want to output an array, that contains only the emails:

$emails = array('[email protected]','[email protected]');

Can somebody help me, archiving this?

Upvotes: 0

Views: 40

Answers (3)

vladislav
vladislav

Reputation: 71

Use array_map

$emails = array_map(function ($value) {
    return $value->sub_email;
}, get_values());

Upvotes: 1

Rizier123
Rizier123

Reputation: 59681

This should work for you:

<?php

    $data = get_values();
    $emails = array();

    foreach($data as $v)
        $emails[] = $v->sub_email;

    print_r($emails);

?>

Upvotes: 2

Vivek Vaghela
Vivek Vaghela

Reputation: 1075

User foreach :

$arrData = get_values();
$emails = array();
foreach($arrData as $val) {
    $emails[] = $val->sub_email;
}

Upvotes: 1

Related Questions