0

I have the following code :

$results = $Q->get_posts($args);

foreach ($results as $r) {
    print $r['trackArtist'];
}

This is the output :

["SOUL MINORITY"]
["INLAND KNIGHTS"]
["DUKY","LOQUACE"]

My question is, if trackArtist is an array, why can't I run the implode function like this :

$artistString = implode(" , ", $r['trackArtist']);

Thanks

UPDATE :

Yes, it is a string indeed, but from the other side it leaves as an array so I assumed it arrives as an array here also. There must be some processing done in the back.

Any idea how I can extract the information, for example from : ["DUKY","LOQUACE"]

to get :

DUKY, LOQUACE

Thanks for your time

6
  • 1
    This seems not to be an array but maybe an object? Commented Apr 4, 2013 at 14:28
  • Why do you think that trackArtist is array? Commented Apr 4, 2013 at 14:28
  • Try var_dump($r['trackArtist']); instead of print(); to check if it is actually an array. Commented Apr 4, 2013 at 14:28
  • What makes you think trackArtist is an array? Commented Apr 4, 2013 at 14:28
  • echo "<pre>"; print_r($results); echo "</pre>"; whats the Output ? Commented Apr 4, 2013 at 14:29

3 Answers 3

1

It's probably a JSON string. You can do this to get the desired result:

$a = json_decode($r['trackArtist']); // turns your string into an array
$artistString = implode(', ', $a); // now you can use implode
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like it's not actually an array; it's the string '["DUKY","LOQUACE"]' An array would be printed as Array. You can confirm this with:

var_dump($r['trackArtist']);

Comments

1

To me content of $r['trackArtist'] is NOT an array. Just regular string or object. Instead of print use print_r() or var_dump() to figure this out and then adjust your code to work correctly with the type of object it really is.

2 Comments

I guess he meant $r['trackArtist'] is an array, not 'trackArtist'.
yep. I refered to that. Edited

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.