How can I get this array to display with line breaks?

Here's my code:

$arr = explode(PHP_EOL, trim($_POST['recipe']));

// PROCESS EACH INGREDIENT
$out = [];
foreach ($arr as $itm)
{
    // NORMALIZE TO SINGLE BLANKS
    $itm = preg_replace('/\s\s+/', ' ', $itm);
    $itm = trim($itm);

    // BREAK ON BLANKS AND ISOLATE EACH ELEMENT
	//with "explode," you're now taking the normalized entry and breaking it up into an array that consists of three separate entities separated by a space
    $itm = explode(' ', $itm);
	/*here's where I wanted to really pop the hood and understand what you were doing. Based on http://php.net/manual/en/function.implode.php, it looks like implode returns the $itm array as a string. The elements within that string being $qty, $mes and $ing. You're calling a function that is referenced on the post you made out at http://php.net/manual/en/function.array-slice.php. It seems like you're sequentially "chopping" the array down into bits that are then returned as the part of the $itm string that corresponds to either $qty, $mes and $ing. It works becasue of the way implode feeds each of those elements in their appropriate order
	*/
	
    $qty = implode(NULL, array_chop($itm));
    $mes = implode(NULL, array_chop($itm));
    $ing = implode(' ', $itm);
	//now you're taking those three strings and assembling them into an array that will be neatly packaged for however many lines the user entered
    $out[] = array
    ( 'quantity'    => $qty
    , 'measurement' => $mes
    , 'ingredient'  => $ing
    )
    ;
}
echo json_encode($out, JSON_PRETTY_PRINT);

function array_chop(&$arr, $num=1)
{
    $ret = array_slice($arr, 0, $num);
    $arr = array_slice($arr, $num);
    return $ret;
}

Open in new window


It works wonderfully, but I've got to get it display like this:

[
   {
   "quantity": "1",
    "measurement": "small",
    "ingredient": "onion"
    },
    { "quantity": "1",
    "measurement": "clove",
     "ingredient": "garlic"
     },
     { "quantity": "1",
     "measurement": "tsp",
     "ingredient": "basil"
      },
     { "quantity": "1\/4",
     "measurement": "c",
     "ingredient": "olive oil"
     }
 ]

Right now, it displays like this:

[ { "quantity": "1", "measurement": "small", "ingredient": "onion" }, { "quantity": "1", "measurement": "clove", "ingredient": "garlic" }, { "quantity": "1", "measurement": "tsp", "ingredient": "basil" }, { "quantity": "1\/4", "measurement": "c", "ingredient": "olive oil" } ]

What can I do to make it display with the appropriate line breaks etc.?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
add a <pre> before you start outputing the data followed by </pre> after the data is complete.
<pre> (pre-formatted)

without it, the data is treated as HTML and is output as such given you are not adding any formatting instructions.
Ray PaseurCommented:
Here is the original script from the other question.  
http://iconoun.com/demo/temp_brucegust.php
See line 7:
<?php // demo/temp_brucegust.php
/**
 * http://www.experts-exchange.com/questions/28704361/How-would-I-parse-this-into-an-associate-array.html
 * http://php.net/manual/en/function.array-slice.php#112359
 */
error_reporting(E_ALL);
echo '<pre>';

// SAMPLE DATA FROM THE POST AT E-E
$txt = <<<EOD
1 small onion
1 clove garlic
1 tsp basil
1/4 c olive oil
EOD;

// BREAK ON LINE ENDINGS
$arr = explode(PHP_EOL, $txt);

// PROCESS EACH INGREDIENT
$out = [];
foreach ($arr as $itm)
{
    // NORMALIZE TO SINGLE BLANKS
    $itm = preg_replace('/\s\s+/', ' ', $itm);
    $itm = trim($itm);

    // BREAK ON BLANKS AND ISOLATE EACH ELEMENT
    $itm = explode(' ', $itm);
    $qty = implode(NULL, array_chop($itm));
    $mes = implode(NULL, array_chop($itm));
    $ing = implode(' ', $itm);
    $out[] = array
    ( 'quantity'    => $qty
    , 'measurement' => $mes
    , 'ingredient'  => $ing
    )
    ;
}
echo json_encode($out, JSON_PRETTY_PRINT);

function array_chop(&$arr, $num=1)
{
    $ret = array_slice($arr, 0, $num);
    $arr = array_slice($arr, $num);
    return $ret;
}

Open in new window

Here is the output from that script.
[
    {
        "quantity": "1",
        "measurement": "small",
        "ingredient": "onion"
    },
    {
        "quantity": "1",
        "measurement": "clove",
        "ingredient": "garlic"
    },
    {
        "quantity": "1",
        "measurement": "tsp",
        "ingredient": "basil"
    },
    {
        "quantity": "1\/4",
        "measurement": "c",
        "ingredient": "olive oil"
    }
]

Open in new window

Here is the reference on JSON: http://json.org/

Note the difference in the square brackets and the curly brackets.  The JSON reference will explain the background information you need to understand this difference, which appears subtle but is actually very important.

JSON treats objects and arrays in a special manner because PHP and JavaScript do not understand associative arrays the same way.  JSON is JavaScript Object Notation, and there is no concept of an associative array in javaScript.  That is why the encoding process turns the PHP associative array into the JavaScript object.

HTML collapses whitespace into single blanks when you display things in the browser.   If you want to see the "real" display, use view source.  JSON does not care about whitespace outside of the contents of the keys and the data, so you can format it for your preferences, so long as you do not disrupt anything inside the double quotes.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.