-2

The output of the code inside index.blade.php is this:

Array
(
    [0] => <strong>Wheat</strong> Flour
    [1] => Tomato Purée
    [2] => Mozzarella Cheese (<strong>Milk</strong>) (16%), Pepperoni (10%), Water, Mini Pepperoni (3.5%), Yeast, Dextrose, Rapeseed Oil, Salt, Sugar, Dried Garlic, Dried Herbs, Spice. Pepperoni contains: Pork, Pork Fat, Salt, Dextrose, Spices, Spice Extracts, Antioxidants (Extracts of Rosemary, Sodium Ascorbate), Preservative (Sodium Nitrite). Mini Pepperoni contains: Pork, Pork Fat, Salt, Dextrose, Spices, Spice Extracts, Sugar, Antioxidants (Sodium Erythorbate, Extracts of Rosemary), Preservative (Sodium Nitrite).
)

Unlike [0] and [1], [2] has many ingredients. I need to be able to separate them all out on to their own rows (using commas and spaces etc as break points). I achieved this in JavaScript doing this: .split(/[:;,.<> /)(]+/);

The result shown here would be a good example: split array values when there is a comma however I get an array to string conversion error when attempting to emulate this.

index.blade.php

require_once 'HTTP/Request2.php';

$request = new Http_Request2('https://dev.tescolabs.com/product/');
$url = $request->getUrl();

$headers = array(
    // Request headers
    'Ocp-Apim-Subscription-Key' => 'key',
);

$request->setHeader($headers);

$parameters = array(
    // Request parameters
    'gtin' => '05054402006097',
);

$url->setQueryVariables($parameters);

$request->setMethod(HTTP_Request2::METHOD_GET);

// Request body
$request->setBody("{body}");

try
{
    $response = $request->send();
    $result = $response->getBody();

    //true decodes the json into an associative array instead of stdObject
    $decoded = json_decode($result,true);

    $ingredients = $decoded['products'][0]['ingredients'];
    print_r($ingredients);

}
catch (HttpException $ex)
{
    echo $ex;
}
Dan
  • 951
  • 1
  • 23
  • 46
  • 2
    look into [preg_split()](http://de2.php.net/manual/en/function.preg-split.php), which works similar to split() – Erik Kalkoken Feb 04 '18 at 19:34
  • have you considered change the source, rather than being an ambalance at the bottom of the cliff –  Feb 04 '18 at 19:35
  • 1
    How is [tag:json] related to the question? – axiac Feb 04 '18 at 19:35
  • 1
    Thanks again @Erik Kalkoken, that lead paid off! – Dan Feb 04 '18 at 20:07
  • 1
    @ rtfm I'm sorry I am not sure what you mean? – Dan Feb 04 '18 at 20:08
  • 1
    @axiac I am receiving json data and then trying to manipulate it afterwards, so I figured this would be an appropriate tag? – Dan Feb 04 '18 at 20:11
  • 1
    Not quite sure why this question would qualify for being marked down? – Dan Feb 04 '18 at 20:12
  • It is not related to arrays either and the posted code is completely useless for the question. The question is about splitting a string in pieces using more than one separator or a regular expression. – axiac Feb 04 '18 at 20:45
  • 1
    I'm afraid I disagree. Think about it from the position of someone learning.... At the time of posting this I had an array, of which I was trying to split the contents, hence including tag for arrays. The contents were not string at that point, so my question wasn't how do I split string, because I didn't know it needed to be string in order to be split. I wasn't aware of `preg_split()` at the time of posting. And the code I posted was all the code I had, so I don't understand how you can call it "completely useless". – Dan Feb 04 '18 at 21:09

2 Answers2

1
//true decodes the json into an associative array instead of stdObject
$decoded = json_decode($result,true);

// can now target ingredient array
$ingredients = $decoded['products'][0]['ingredients'];

//turn contents into string so can use preg_split(
 $test = implode($ingredients);

$ingredientList = preg_split("/[\s, ]+/", $test);
print_r($ingredientList);
Dan
  • 951
  • 1
  • 23
  • 46
1

I achieved this in JavaScript doing this: .split(/[:;,.<> /)(]+/);

The equivalent code in PHP uses preg_split():

 $pieces = preg_split('@[:;,.<> /)(]+@', $ingredients[2]);

Read more about PCRE patterns in PHP.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • This partly fixes the problem, but thinking of the bigger picture I am dealing with data being pulled from an API. This only deals with $ingredients[2]. $ingredients[0] and $ingredients[1] are not necessarily going to be the same on future results. There may not always even be a [2]. I didn't know that I could use the regex with the @signs like that and get the same result as I did in JS. This is very useful thank you. – Dan Feb 04 '18 at 21:16
  • PHP does not provide a separate type for `regex` (as JS does). `regex` in PHP are represented as strings. The usual delimiter is `/` but because the `regex` contains `/` inside I used a different delimiter. PHP accepts several [delimiters](http://php.net/manual/en/regexp.reference.delimiters.php) in PCRE. The `regex` can be written as `'/[:;,.<> \/)(]+/'` (uses `/` as delimiter) but in this case the internal `/` must be [escaped](http://php.net/manual/en/regexp.reference.escape.php) and this makes it less clear. – axiac Feb 04 '18 at 21:29