Home>

We have built a database handling system with Wordpress.
Receive a list of tables from the database at the following sources:

var_dump ($a_course);

According to

array (1) {["Tables_in_hogehoge-hogehoge"] =>string (14)"consumercourse"}

And apparently it was an array, so to retrieve it from the array
array_column ($a_course,'Tables_in_hogehoge-hogehoge');and T
tried to pull from ables_in_hogehoge-hogehoge
It seems that Array () is displayed and it has not been extracted.
It would be helpful to tell me how I can extract it.

try {
    $pdo = new PDO ("mysql: dbname = $db_datebase_6;host = $db_address_3;charset = utf8", $db_user, $db_password);
    $stmt = $pdo->query ('SHOW TABLES');
    print ('<table border = "1">');
    while ($a_course = $stmt->fetch (PDO :: FETCH_ASSOC)) {
        print ('<tr>');
        print ('<td>');
        var_dump ($a_course);
        print '<br>';
        $courseid = array_column ($a_course, 'Tables_in_hogehoge-hogehoge');
        print_r ($courseid);
        print ('</td>');
        print ('<td>');
        echo 'Edit course';
        print ('</td>');
        print ('<td>');
        echo 'Clear course';
        print ('</td>');
        print ('</tr>');
        echo '<br>';
    }
    print ('</table>');
} catch (PDOException $e) {
    var_dump ($e);
    echo 'An error has occurred. ';
}

I referred to ⇓ around print_r.
http://php.net/manual/en/function.array-column.php

I'm sure you don't understand/understood it fundamentally, but please forgive me.

Reminder

I used array_column to extract from an array, but is it different?

  • Answer # 1

    <? php
    ini_set ('display_errors', true);
    error_reporting (E_ALL);
    $dsn = 'mysql: host = localhost;dbname = sample;charset = utf8mb4';
    $pdo = new PDO ($dsn
        , 'root'
        , 'password'
        , [
        PDO :: ATTR_ERRMODE =>PDO :: ERRMODE_EXCEPTION
        , PDO :: ATTR_DEFAULT_FETCH_MODE =>PDO :: FETCH_COLUMN
        ]
    );
    $stmt = $pdo->query ('SHOW TABLES');
    $rows = $stmt->fetchAll ();
    ?>
    <! DOCTYPE HTML>
    <html lang = "en">
        <head>
            <meta charset = "UTF-8">
            <title>Sample</title>
        </head>
        <body>
            <div>
                <table>
                    <thead>
                        <tr>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <? php foreach ($rows as $row):?>
                            <tr>
                                <td><? php echo $row;?></td>
                            </tr>
                        <? php endforeach;?>
                    </tbody>
                </table>
            </div>
        </body>
    </html>

  • Answer # 2

    The array of the argument is described as a multidimensional array (array of arrays) in the array_column manual, but the array passed to the argument is 1 Can't get it because of the array of dimensions!
    http://php.net/manual/en/function.array-column.php

      

    Multidimensional array (or array of objects) from which you want to retrieve values.

    $a_course = ["Tables_in_hogehoge-hogehoge" =>"consumercourse"];
    $id = array_column ($a_course, "Tables_in_hogehoge-hogehoge");
    var_dump ($id);
    / *
    array (0) {
    }
    */

    If there is an array in the array as shown below, you can get it with array_column.

    $a_course = [
        [
            "Tables_in_hogehoge-hogehoge" =>"consumercourse1"
        ],
        [
            "Tables_in_hogehoge-hogehoge" =>"consumercourse2"
        ],
    ];
    var_dump ($a_course2);
    / *
    array (2) {
      [0] =>
      array (1) {
        ["Tables_in_hogehoge-hogehoge"] =>
        string (15) "consumercourse1"
      }
      [1] =>
      array (1) {
        ["Tables_in_hogehoge-hogehoge"] =>
        string (15) "consumercourse2"
      }
    }
    * /
    $ids = array_column ($a_course2, "Tables_in_hogehoge-hogehoge");
    var_dump ($ids);
    / *
    array (2) {
      [0] =>
      string (15) "consumercourse1"
      [1] =>
      string (15) "consumercourse2"
    }
    */

    As you may know, if you have a one-dimensional array, you can get it by specifying the key.

    var_dump ($a_course ["Tables_in_hogehoge-hogehoge"]);
    / *
    string (14) "consumercourse"
    */