0
 $nesAry=array();
$nesAry["name"]="abc";
$nesAry["email"]="[email protected]";
$nesAry1=array();
$nesAry1["name"]="abc1";
$nesAry1["email"]="[email protected]";
$nesAry2=array();
$nesAry2["name"]="abc2";
$nesAry2["email"]="[email protected]";
$responseAry = array();
$responseAry[0]=$nesAry;
$responseAry[1]=$nesAry1;
$responseAry[2]=$nesAry2;
echo json_encode($responseAry); // here output like this => [{"name":"abc","email":"[email protected]"},{"name":"abc1","email":"[email protected]"},{"name":"abc2","email":"[email protected]"}]
unset($responseAry[1]);
echo "------------removed 1--------";
echo json_encode($responseAry); // but here output like this => {"0":{"name":"abc","email":"[email protected]"},"2":{"name":"abc2","email":"[email protected]"}}

I want Out put Like this after removing an element \n [{"name":"abc","email":"[email protected]"},{"name":"abc2","email":"[email protected]"}]

Please Help me

asked Jun 15, 2017 at 9:07
1

2 Answers 2

1

Try to regenerate your array after unset an item:

$nesAry=array();
$nesAry["name"]="abc";
$nesAry["email"]="[email protected]";
$nesAry1=array();
$nesAry1["name"]="abc1";
$nesAry1["email"]="[email protected]";
$nesAry2=array();
$nesAry2["name"]="abc2";
$nesAry2["email"]="[email protected]";
$responseAry = array();
$responseAry[0]=$nesAry;
$responseAry[1]=$nesAry1;
$responseAry[2]=$nesAry2;
echo json_encode($responseAry); // __here output like this => [{"name":"abc","email":"[email protected]"},{"name":"abc1","email":"[email protected]"},{"name":"abc2"}]__
unset($responseAry[1]);
$responseAry = array_values($responseAry); //regenerate array(reindexing)
echo "------------removed 1--------";
echo json_encode($responseAry); //[{"name":"abc","email":"[email protected]"},{"name":"abc2","email":"[email protected]"}]

EDIT:

As other option you can use array_splice method http://php.net/manual/en/function.array-splice.php

$nesAry=array();
$nesAry["name"]="abc";
$nesAry["email"]="[email protected]";
$nesAry1=array();
$nesAry1["name"]="abc1";
$nesAry1["email"]="[email protected]";
$nesAry2=array();
$nesAry2["name"]="abc2";
$nesAry2["email"]="[email protected]";
$responseAry = array();
$responseAry[0]=$nesAry;
$responseAry[1]=$nesAry1;
$responseAry[2]=$nesAry2;
echo json_encode($responseAry); // __here output like this => [{"name":"abc","email":"[email protected]"},{"name":"abc1","email":"[email protected]"},{"name":"abc2"}]__
array_splice($responseAry,1,1);
echo "------------removed 1--------";
echo json_encode($responseAry);
answered Jun 15, 2017 at 9:12
Sign up to request clarification or add additional context in comments.

2 Comments

is there any other option ?
Thank you so much for this answer
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

answered Jun 15, 2017 at 9:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.