app/config/routing.yml
我想在删除元素之后输出这样的内容\ n [{“name”:“abc”,“email”:“abc@email.com”},{“name”:“abc2”, “电子邮件”: “abc2@email.com”}]
请帮帮我
答案 0 :(得分:1)
尝试在取消设置项目后重新生成数组:
$nesAry=array();
$nesAry["name"]="abc";
$nesAry["email"]="abc@email.com";
$nesAry1=array();
$nesAry1["name"]="abc1";
$nesAry1["email"]="abc1@email.com";
$nesAry2=array();
$nesAry2["name"]="abc2";
$nesAry2["email"]="abc2@email.com";
$responseAry = array();
$responseAry[0]=$nesAry;
$responseAry[1]=$nesAry1;
$responseAry[2]=$nesAry2;
echo json_encode($responseAry); // __here output like this => [{"name":"abc","email":"abc2@email.com"},{"name":"abc1","email":"abc1@email.com"},{"name":"abc2"}]__
unset($responseAry[1]);
$responseAry = array_values($responseAry); //regenerate array(reindexing)
echo "------------removed 1--------";
echo json_encode($responseAry); //[{"name":"abc","email":"abc@email.com"},{"name":"abc2","email":"abc2@email.com"}]
修改强>
作为其他选项,您可以使用array_splice
方法http://php.net/manual/en/function.array-splice.php
$nesAry=array();
$nesAry["name"]="abc";
$nesAry["email"]="abc@email.com";
$nesAry1=array();
$nesAry1["name"]="abc1";
$nesAry1["email"]="abc1@email.com";
$nesAry2=array();
$nesAry2["name"]="abc2";
$nesAry2["email"]="abc2@email.com";
$responseAry = array();
$responseAry[0]=$nesAry;
$responseAry[1]=$nesAry1;
$responseAry[2]=$nesAry2;
echo json_encode($responseAry); // __here output like this => [{"name":"abc","email":"abc2@email.com"},{"name":"abc1","email":"abc1@email.com"},{"name":"abc2"}]__
array_splice($responseAry,1,1);
echo "------------removed 1--------";
echo json_encode($responseAry);
答案 1 :(得分:0)
Your array when first converted to json is a so called "associative" array, and json_encode then exports it to the object you see in the first echo.
After your unset, the array is changed to a "numeric" array and json_encode will export the array with array keys.
Php it self does not care about how the array is used, but json_encode does.
You can use
echo json_encode(array_values($responseAry));
Or not change the final array you want to export