tim
tim

Reputation: 27

Filter & Convert an Array into String using PHP

How can I turn this array:

print_r($arr)

Array (
    [131] => stdClass Object
        (
            [tid] => 131
            [vid] => 9
            [name] => apple
            [description] => 
            [weight] => 0
        )

    [112] => stdClass Object
        (
            [tid] => 112
            [vid] => 9
            [name] => cool
            [description] => 
            [weight] => 0
        )

    [113] => stdClass Object
        (
            [tid] => 113
            [vid] => 9
            [name] => wonderful
            [description] => 
            [weight] => 0
        )

)

into this:

apple cool wonderful

Basically take out the "name" variable of the array and implode it in order. I tried implode, but I don't know how to only reference to the "name" variables.

Upvotes: 0

Views: 1124

Answers (2)

runfalk
runfalk

Reputation: 1996

echo join(" ", array_map(create_function('$x', 'return $x->name;'), $arr));

Is one way of doing it

Upvotes: 2

ceejayoz
ceejayoz

Reputation: 180095

$terms = array();
foreach($arr as $term) { $terms[] = $term->name; }
print implode($terms, ', ');

Upvotes: 0

Related Questions