0

I have an multidimensional array made from database query and is like that:

Array
(
    [0] => Array
        (
            [0] => -8.63296022565696
            [x] => -8.63296022565696
            [1] => 41.1584289069069
            [y] => 41.1584289069069
            [2] => 0
            [seq] => 0
            [3] => 2
            [seq2] => 2
            [4] => -8.63306031211831
            [next_x] => -8.63306031211831
            [5] => 41.1584543235506
            [next_y] => 41.1584543235506
            [6] => -8.64195115878864
            [alert_x] => -8.64195115878864
            [7] => 41.1599295066425
            [alert_y] => 41.1599295066425
            [8] => 54e728edafac1
            [route] => 54e728edafac1
            [9] => 54e728edafac1
            [routeid] => 54e728edafac1
            [10] => 2
            [counttargetinter] => 2
            [11] => passeio
            [type] => passeio
            [12] => 1355
            [arcid] => 1355
        )

All the values are repeated because have a key number and a key name. Example: The value '-8.63296022565696' are in key "0" and "X".

How I can remove the duplicated?

This is how i made the array:

$query = "SELECT * FROM foo;";
$startRows =  pg_query($connection, $query);
$startInfo = array();   
while($list = pg_fetch_array($startRows)) {
    $startInfo[] = $list;
}
5
  • 1
    Have you tried anything? Commented Feb 20, 2015 at 12:21
  • I am searching for a way of deleting all values with integer keys. Commented Feb 20, 2015 at 12:22
  • According to me its JSON Commented Feb 20, 2015 at 12:27
  • The array_unique is not want i want because its possible that my array have correct not unique values . Example two points with same latitude. Commented Feb 20, 2015 at 12:28
  • yes, its JSON because i made json_encode. I will correct. Commented Feb 20, 2015 at 12:29

3 Answers 3

1

Of course you can't mess with the generate JSON string to deal with the dups. You solve it during the creation of the array itself before encoding. Looking at the structure, this seems to be the problem of fetching both numeric and column indices.

Since you haven't posted any codes related to actually creating this JSON string, just use this basic idea on how to get rid of them.

If you intent do remove those numeric indices, you'll probably need to use fetch_assoc() flavours of your database API, so that in turn, you'll only get the column name indices instead of having them both.

Here's the idea:

$data = array(); // initialization of the container
while($row = your_fetch_assoc($result)) { // use assoc() instead to exclude those numeric indices
    $data[] = $row;
}
echo json_encode($data);

Depending on what API you're using, if its PDO, either use -->fetch(PDO::FETCH_ASSOC) or just ->fetchAll(PDO::FETCH_ASSOC) without the need of a loop. If its MySQLi, then just use ->fetch_assoc()

EDIT: At last your codes, as I have suspected you're using _array() function which results associative and numeric indexed rows.

$query = "SELECT * FROM foo;";
$startRows =  pg_query($connection, $query);
$startInfo = array();   
while($list = pg_fetch_assoc($startRows)) {
    $startInfo[] = $list;
}

Use pg_fetch_assoc() instead of _array() so that you'll get the associative indices only.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I have edited my question. You can see how i do the array
@pdcc yeah, my suspicion is correct, you're using _array(), use _assoc() instead, check my revised answer above.
@Ghost You almost guessed right with *_assoc() only the pg_* you didn't guessed :D
@Rizier123 yeah, i was blind with regards to that, i was guessing either PDO or MySQLi, and both failed. lol :D
@pdcc sure, glad this shed some light
0

Like Ghost:

You can use pg_fetch_row() to get numeric indices or pg_fetch_assoc() to get field name.

$query = "SELECT * FROM foo;";
$startRows =  pg_query($connection, $query);
$startInfo = array();   
while($list = pg_fetch_row($startRows)) {
    $startInfo[] = $list;
}

Comments

0

Try this:

foreach($walkroute as $routepoints){
    foreach($routepoints as $key => $value){
       if (is_int($key)){
         unset($routepoints[$key]);
       }
    }
}

You have to modify it due to the fact that i don't know, what the structure and names of your array actually are.

Comments

Your Answer

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.