Fatpanda Fatpanda - 5 months ago 16
PHP Question

PHP Array Remove Duplicates - not Array_Unique

I have a multidimensional array (fake data) in PHP:

Array
(
[0] => Array
(
[pmkSuppliers] => 1
[0] => 1
[Name] => Supplier2
[1] => Supplier2
[Email] => Me@me.com2
[2] => Me@me.com2
[Address] => 624 st
[3] => 624 st
[Phone] => 900-111-1111
[4] => 900-111-1111
[Fax] => 900-111-1112
[5] => 900-111-1112
[TechSupport] => 900-111-1112
[6] => 900-111-1112
[Contact] => Greg
[7] => Greg
[Modified] => 2016-06-07
[8] => 2016-06-07
)

[1] => Array
(
[pmkSuppliers] => 2
[0] => 2
[Name] => Nike
[1] => Nike
[Email] => me@none.com
[2] => me@none.com
[Address] => 4566 way
[3] => 4566 way
[Phone] => 901-206-5555
[4] => 901-206-5555
[Fax] => 901-206-5555
[5] => 901-206-5555
[TechSupport] => 901-206-5445
[6] => 901-206-5445
[Contact] => Brad
[7] => Brad
[Modified] => 2016-06-08
[8] => 2016-06-08
)

)


I want to remove all of the elements with a non int key. So it would look something like this:

Array
(
[0] => Array
(
[0] => 1
[1] => Supplier2
[2] => Me@me.com2
[3] => 624 st
[4] => 900-111-1111
[5] => 900-111-1112
[6] => 900-111-1112
[7] => Greg
[8] => 2016-06-07
)

[1] => Array
(
[0] => 2
[1] => Nike
[2] => me@none.com
[3] => 4566 way
[4] => 901-206-5555
[5] => 901-206-5555
[6] => 901-206-5555
[7] => Brad
[8] => 2016-06-08
)

)


I figure there has to be an easy way to do this, but I'm stuck. I can not use the PHP method array_unique because sometimes the values in my array are the same (like in the second set).

If anyone could point me in the right direction, that would be awesome.

Thanks,

FP

EDIT



This is how I have been doing it, but it seems crude.

foreach ($array as $insideArray) {
foreach ($insideArray as $key => $value) {
if (!is_int($key)) {
echo $value;
}
}
}

Answer

In case you are using PDOs, just set the fetch style:

$statement->setFetchMode(PDO::FETCH_NUM);

You can see the difference fetchStyle in manual.


In addition, you can also use

$statement->fetchAll(PDO::FETCH_COLUMN);

Here is an external article 1 and article 2 that discusses, different fetch methods.

Comments