[英]save output of a foreach ($array as $key => $value) as a readable variable php
I have the following function.我有以下 function。
printArray($_POST);
function printArray($array){
foreach ($array as $key => $value){
echo "$key: $value <br>";
}
}
I modified it from here... Print $_POST variable name along with value我从这里修改它... 打印 $_POST 变量名和值
It gets all my html values along with there variable names and echos them.它获取我所有的 html 值以及变量名称并回显它们。 I need to save the output to variable.
我需要将 output 保存到变量中。 I've tried using ob..
我试过用ob ..
printArray($_POST);
ob_start();
function printArray($array){
foreach ($array as $key => $value){
echo "$key: $value <br>";
}
}
$out = ob_get_contents();
ob_end_clean();
print $out;
and that just gives me nothing.那只是什么都没有给我。 Ive tried just saving my echo to a variable and that gets me closer...
我试过将我的回声保存到一个变量中,这让我更接近......
function printArray($array){
foreach ($array as $key => $value){
echo "$key: $value <br>";
}
}
but it only gives me the last value in my output.但它只给了我 output 中的最后一个值。 Help me out here what am i doing wrong.
帮我看看我做错了什么。
see...看...
Postman call and result Postman 调用和结果
<?php
printArray($_POST);
function printArray($array){
$result = [];
foreach ($array as $key => $value){
$result[] = (object)[
$key => $value
];
}
header('Content-Type: application/json; charset=utf-8');
echo json_encode($result);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.