-2

There are any way to convert a PDO fetch (only 1 column) in a PHP array? Instead of a foreach loop. Example:

"data": [
    {
        "A": "1"
    },
    {
        "A": "2"
    },
    {
        "A": "3"
    }
]

expected:

"data": [
    "1",
    "2",
    "3"
]
Alvaro_SE
  • 125
  • 2
  • 13
  • We are always glad to help and support new coders but ***you need to help yourself first. :-)*** After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Oct 10 '17 at 12:55
  • 1
    `$string['data'] = array_column($string->data,'A');` OR `$string['data'] = array_column($string['data'],'A');` – Alive to die - Anant Oct 10 '17 at 12:56
  • @AlivetoDie thank! It's work! – Alvaro_SE Oct 10 '17 at 12:58
  • @AlivetoDie I've tried the second one – Alvaro_SE Oct 10 '17 at 13:01
  • Possible duplicate of [How to get an array of specific "key" in multidimensional array without looping](https://stackoverflow.com/questions/7994497/how-to-get-an-array-of-specific-key-in-multidimensional-array-without-looping) – mickmackusa Oct 10 '17 at 23:31
  • You can and should get it using fetchAll() with [PDO::FETCH_COLUMN fetch mode](https://phpdelusions.net/pdo/fetch_modes#FETCH_COLUMN) – Your Common Sense Oct 11 '17 at 09:58

1 Answers1

0

You need to use array_column() like eblow:-

$string['data'] = array_column($string['data'],'A');
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98