Bruce Gust
asked on
How can I get this array to display with line breaks?
Here's my code:
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.?
$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;
}
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.?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.