0

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

Array
(
[0] => Array
    (
        [pmkSuppliers] => 1
        [0] => 1
        [Name] => Supplier2
        [1] => Supplier2
        [Email] => [email protected]
        [2] => [email protected]
        [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] => [email protected]
        [2] => [email protected]
        [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] => [email protected]
        [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] => [email protected]
        [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;
        }
    }
}
12
  • 1
    Are you fetching this using PDO? Commented Jun 18, 2016 at 0:43
  • you can try to use is_numeric+array_flip Commented Jun 18, 2016 at 0:44
  • @Thamilan Yes, in a roundabout way. Commented Jun 18, 2016 at 0:49
  • Then simply, change the fetch style to PDO::FETCH_NUM. Can I see your fetching code? Commented Jun 18, 2016 at 0:49
  • @SML I think your way would work, but I'm not sure it would be any more elegant/efficient than the way I have been doing it. (See my edit). Commented Jun 18, 2016 at 0:51

2 Answers 2

1

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.

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

Comments

0
foreach ($array as &$arr){
    foreach ($arr as $key => $value) {
    if (!is_int($key)) {
        unset($arr[$key]);
    }
  }
}

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.