0

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.

0

2 Answers 2

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

Is one way of doing it

Sign up to request clarification or add additional context in comments.

5 Comments

for some reason I can't get this to work... I'm trying to figure it out
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"]
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.
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...)
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
0
$terms = array();
foreach($arr as $term) { $terms[] = $term->name; }
print implode($terms, ', ');

8 Comments

I got this error Fatal error: [] operator not supported for strings. I'm using drupal.
Sounds like there's an existing $terms variable. Change $terms to a different variable name, or do $terms = array(); before this code.
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']
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.
print_r ing $term gave me the same results as $arr. print_r ing $terms gave me Array ( [0] => )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.