1

From the code below, the output is

<meta mytags="tags" content="Array" />

but if I echo the array_values($field->rawvalue), it displays all the populated values from the array.

Can someone point to what I'm doing wrong?

if ($field->id === '10') {
    //Makes sure the field is not empty
    if (!empty($field->rawvalue)) {
        //Inserts the field content with markup into the HTML head
        $customField = '<meta mytags="' . $field->label . '" content="' . array_values($field->rawvalue) . '" />';
        $document->addCustomTag($customField);
    }
    continue;
}
  • Use json_encode($field->rawvalue) (without array_values()) on your array, then you should see the output, then you will know how to call the element that you want. If you can't figure it out, post the json data for us and tell us your desired result. In the end, you will want something like $field->rawvalue["somekey"]. If you want all of the values as a comma separated string, then use implode(", ", $field->rawvalue). – mickmackusa Oct 1 '18 at 22:59
  • To offer the best support, we always need to know: 1. What result you are getting and 2. What result you want. – mickmackusa Oct 1 '18 at 23:06
  • echo cannot be used to display array data (this is why you see Array). Because of the way that the data must be displayed in the html, you must convert it to a string. If using implode() qorks as desired, let me know and I'll post this advice as an answer. I generally don't like to post "guesses" as "answers", because this is a symptom of an unclear/incomplete question. Please always improve your questions and provide feedback to volunteers so that all of your questions always progress to a system-recognized resolution. – mickmackusa Oct 2 '18 at 0:57
2

PHP array_values returns an array - that's why you get "array" printed out. Most likely if you deal with an array and want to make it a string, then you are after implode($separator, $array); http://php.net/manual/en/function.implode.php

For example, for a comma separated string you can do:

implode(',', $field->rawvalue);

But you have to make clear what's type is in your $field->rawvalue.

  • I guess I should have posted an answer. No points for comments. – mickmackusa Oct 2 '18 at 5:31
  • @mickmackusa Great! Using the implode(", ", $field->rawvalue) was exactly what I needed to show all the populated values in the array, as FFrewin confirmed. – seefah Oct 2 '18 at 5:45
  • Hey @mickmackusa : Comments are comments - answers are answers IMHO there is not always a need to overdoing things, or give an exact solution tailored on specific needs, or write an essay as answer. You wrote so many comments -constituting an answer anyway- while the case was simpler: a wrong usage of a php function. I am not saying you aren't doing a good job overall. You do at a highest level. Just sometimes it's better to keep things simple and quick ;) Keep up the good work mate! – FFrewin Oct 2 '18 at 8:40
  • Feel free to comment to me and just say: "hey mick, post those comments as an answer." I get notifications when comments are directed to me. I know I could have posted an answer, but I don't just want to answer questions here, I want to educate new OPs about how to ask. Furthermore, deleted answers never leave the system, so I didn't want to answer before being absolutely sure. Sometimes on SO, I post an answer, then instantly delete, then tell the OP that I will undelete my solution after the question is complete. I was hoping to avoid this tactic on this community. – mickmackusa Oct 2 '18 at 8:44
  • 1
    @mickmackusa :: that was a good catch. Thanks for reporting. – FFrewin Oct 16 '18 at 5:33
1

To construct your tag data in the format that can be echoed into the html, you must convert the data into a string.

array_values() does not convert an array to a string, it reindexes (assigns new numeric keys starting from 0) the array.

Assuming you wish to generate a string with comma-separated values, you should use implode(',', $field->rawvalue) like this:

if ($field->id === '10' && !empty($field->rawvalue)) {
    //Inserts the field content with markup into the HTML head
    $customField = '<meta mytags="' . $field->label . '" content="' . implode(`,`, $field->rawvalue) . '" />';
    $document->addCustomTag($customField);
}

If for some reason, you have double quotes in your array values, you'll need to escape them after imploding.

p.s. I am not sure if continue is necessary in your loop (it is meaningless if there are no more lines of code after the if block in your loop). For that matter, I don't even know if that loop is necessary -- I'd need to see a bit more code to properly review.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.