public ksoftss
public ksoftss

Reputation: 1

delete an element of given key of array in 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","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

Upvotes: 0

Views: 89

Answers (2)

Remko Janse
Remko Janse

Reputation: 36

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

Upvotes: 0

B. Desai
B. Desai

Reputation: 16436

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);

Upvotes: 1

Related Questions