Array_Unique and Then Renumbering Keys

array_unique and then renumbering keys

Easiest way would be to put them into a new array either via a loop, or better yet array_values function.

$new_array = array_values($original_array)

More information

Array after array_unique function is returned as an object in JSON response

array_unique removes values from the array that are duplicates, but the array keys are preserved.

So an array like this:

array(1,2,2,3)

will get filtered to be this

array(1,2,3)

but the value "3" will keep his key of "3", so the resulting array really is

array(0 => 1, 1 => 2, 3 => 3)

And json_encode is not able to encode these values into a JSON array because the keys are not starting from zero without holes. The only generic way to be able to restore that array is to use a JSON object for it.

If you want to always emit a JSON array, you have to renumber the array keys. One way would be to merge the array with an empty one:

$nonunique = array(1,2,2,3);
$unique = array_unique($nonunique);
$renumbered = array_merge($unique, array());

json_encode($renumbered);

Another way to accomplish that would be letting array_values create a new consecutively-indexed array for you:

$nonunique = array(1,2,2,3);
$renumbered = array_values(array_unique($nonunique));

json_encode($renumbered);

array_merge & array_unique

The most elegant, simple, and efficient solution is the one mentioned in the original question...

$ab = array_unique(array_merge($a, $b));

This answer was also previously mentioned in comments by Ben Lee and doublejosh, but I'm posting it here as an actual answer for the benefit of other people who find this question and want to know what the best solution is without reading all the comments on this page.

Splatpacking versus array_values() to re-index an array with numeric keys

When re-indexing a 450,000 element array which has its first element unset...

array_values() is consistently twice as fast as splatpacking.

$array = range(0, 450000);
unset($array[0]);

Benchmark script

Sample output:

Duration of array_values: 15.328378677368
Duration of splat-pack: 29.623107910156

In terms of performance, you should always use array_values(). This is one case when a function-calling technique is more efficient than a non-function-calling technique.


I suppose the only scenario where the splatpacking technique wins is if you are a CodeGolfer -- 13 characters versus 5.

How to array_push unique values inside another array

You could foreach() through $DocumentID and check for the current value in $UniqueDocumentID with in_array() and if not present add it. Or use the proper tool:

$UniqueDocumentID = array_unique($DocumentID);

To your comment about wanting sequential keys:

$UniqueDocumentID = array_values(array_unique($DocumentID));

The long way around:

$UniqueDocumentID = array();

foreach($DocumentID as $value) {
if(!in_array($value, $UniqueDocumentID)) {
$UniqueDocumentID[] = $value;
}
}

Re-index numeric array keys

$your_new_array = array_values($your_old_array);

How to Remove Array Element and Then Re-Index Array?

unset($foo[0]); // remove item at index 0
$foo2 = array_values($foo); // 'reindex' array

Php Json Array, how to order the output without 0 1 and so on if more than 3

Your use of array_unique removes the array values that are the same (leaving only the first instance), however it also kills the numeric keys in-between.

Your options are to remove the array_unique call and include the duplicate entries in the array or reset the array at the end:

<?php 
require_once('include/db_connect.php');

$sql = "SELECT * FROM `pesanan` ORDER BY id_pesanan DESC";

$response = mysqli_query($con, $sql);

if (mysqli_num_rows($response) > 0 ) {

$result = array();
$result ["pesanan"] = array();

while ($data = mysqli_fetch_assoc($response)) {

$h['id_pesanan'] = $data['id_pesanan'];
$h['id_user'] = $data['id_alamat'];
$h['id_beli'] = $data['id_beli'];
$h['id_bank'] = $data['id_bank'];
$h['kode_pesanan'] = $data['kode_pesanan'];
$h['tgl_pesanan'] = $data['tanggal_pesanan'];
$h['harga_pesanan'] = $data['harga_pesanan'];
$h['harga_ongkir'] = $data['harga_ongkir'];
$h['kurir_pengiriman'] = $data['kurir_pengiriman'];
$h['status_pesanan'] = $data['status_pesanan'];
$id_beli = $data['id_beli'];

$sql2= mysqli_query($con, "SELECT * FROM `keranjang`
INNER JOIN `produk`
ON keranjang.`id_produk` = produk.`id_produk`
INNER JOIN `pesanan_file`
ON keranjang.`id_pesanan` = pesanan_file.`id_pesanan`
WHERE keranjang.`id_beli`='".$id_beli."' ");

/*$h["produk"] = array();*/

$produk = array();

while ($data2 = mysqli_fetch_array($sql2)) {

$produk[] = array('nama_produk' => $data2['nama_produk'] , 'id_pesanan' => $data2['id_pesanan'],);

$h["produk"] = array_map("unserialize", array_unique(array_map("serialize", $produk)));
}

// Reset array keys to fix items removed by array_unique
$h['produk'] = array_values($h['produk']);

array_push($result["pesanan"], $h);

}

$result["success"] = "1";
echo json_encode($result);

}else {

$result["success"] = "0";
echo json_encode($result);
}

?>`

Removing non-unique PHP array values from unassociated array

Add one more line
array_values($ids)



Related Topics



Leave a reply



Submit