views:

178

answers:

2

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.

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

Is one way of doing it

antennen
for some reason I can't get this to work... I'm trying to figure it out
tim
it doesn't work, because the "name" is under a number. $arr->##["name"]. And that number can be any number, like $arr->131["name"], $arr->113["name"]... you cannot refrence to it like $arr->name or $arr["name"]
tim
That's the whole point of `array_map` - it's turning each term into `$x` for easier access. Please check the documentation on these functions before dismissing them.
ceejayoz
I guess what I'm trying to say is - I'm trying to get past the "object", number 131, 112, 113, etc... and pull out the name from the object. I'm probably not making sense because of my lack of vocabulary... basically the arrays are stored under the objects, and I need to pull the "name" variable in the array, while I don't know that object name (object 131, 112, 113, etc...)
tim
I found an error on my side, after fixing it, I'm getting the following error:Fatal error: Cannot use object of type stdClass as array in filename.php
tim
Edited to work with your code. Note the -> instead of ["
antennen
A: 
$terms = array();
foreach($arr as $term) { $terms[] = $term->name; }
print implode($terms, ', ');
ceejayoz
I got this error Fatal error: [] operator not supported for strings. I'm using drupal.
tim
Sounds like there's an existing `$terms` variable. Change `$terms` to a different variable name, or do `$terms = array();` before this code.
ceejayoz
The array() think helps pass the error, but I get a blank result. I think this statement is referencing to $arr->name, when it should reference to $arr->anynumber['name']
tim
Nope. `foreach` steps through each item in `$arr` and turns it into `$term`, so `$term->name` is the correct notation. Try `print_r` ing `$term` to check.
ceejayoz
print_r ing $term gave me the same results as $arr. print_r ing $terms gave me Array ( [0] => )
tim
`print_r` ing `$term` cannot give the exact same results as `$arr`. Are you **sure** you're not seeing three separate `$term` `print_r` results, one after another? This is pretty straightforward PHP, so I'm rather baffled that you're having issues with both solutions proposed.
ceejayoz
I found a mistake on my side... so after fixing it, print_r ing $term is giving me the object 113. and the following code: foreach($arr as $term) { $terms = array($term->name); }print implode($terms, ', '); gives me "wonderful"
tim
You're doing the array() wrong. It needs to be done once, before the `foreach` loop, or you're wiping it out each time `foreach` runs. See my update to the answer.
ceejayoz
You rock! It worked. Thanks for sticking with me the whole hour:)
tim