PHP - Output Array Value That Contains A Specific Text Marker

- 1 answer

I'm using a function to output a collection of products in Shopify, onto a WordPress page.

I've got most of the data displaying correctly, except a custom value that is entered as a tag in Shopify. Using the api, I'm then trying to get this specific tag for the product subtitle which is formatted with att:Subtitle: before each product custom value/text.

This is the code I have got to (I've commented other unsuccessful attempts in the middle) - this is inside the overall code to show the 4 products in the Shopify collection:

 // Using tags to output custom data from products
$tags = $current_product['tags'];

// $tags is a string, this turns the values into an array
$product_tags = explode(',', $tags);

// Evaluate if there is a string with att:Subtitle in the product tags
$subtitle_attribute_key = "att:Subtitle:";

if (strpos($tags, 'att:Subtitle:') !== false) {  
    // Returns a numbered value corresponding to my subtitle attribute
    // $key = array_search($subtitle_attribute_key, $product_tags);
    $sub = strpos($tags, $subtitle_attribute_key);

    // Turns the numbered value into a text value
    // $numArray = explode(" ", $sub);
    // var_dump($numArray);
    $value = print_r($sub, true);
    // $value = array_search("att:Subtitle:",$product_tabs);
    // $value = array_search("att:Subtitle:", $tabs); // Warning: array_search() expects parameter 2 to be array, null given
    // $result = $product_tags['$value']; // my attempt to return the text

    // Remove att:Subtittle: in front of the subtitle value to output the clean final value
    $subtitle = ltrim($value, 'att:Subtitle:');

So far it comes out as a number value where I'm displaying $subtitle... but I can't figure out how to display the custom text value instead.

Any ideas?


EDIT: I am working with products that have multiple tags and I do not control these. Out of the tags I'm trying to find the one that starts with att:Subtitle: but only show the custom value after that marker.

When I echo $tags, the ist comes out something like this:

att:Benefit:balance, att:Perfect:Combination Skin, att:Size: 1.8 oz, att:Subtitle: Multi-Tasking Product, Key Ingredient 1, Key Ingredient 2, Essentials, meta-related-product-xyz, meta-related-product-brandname

They will all have a different list of tags



Of course it comes out as a number. You're getting a number from the strpos() and ultimate trying to run ltrim() on it.

If you're expecting the desired text to be in $tags immediately following 'att:Subtitle:' and have nothing else in that string, $subtitle = substr($tags, strpos('att:Subtitle:') + strlen('att:Subtitle:')); should give it to you. If you're expecting it to possibly be an element in the $product_tags array, need to loop (I'm assuming it shows up once, at most):

foreach ($product_tags as $product_tag) {
    if (strpos($product_tag, 'att:Subtitle:') !== false) { 
        $subtitle = substr($product_tag, strpos($product_tag, 'att:Subtitle:') + strlen('att:Subtitle:'));