0

I'm trying to query database A and store results as an array, which I will then insert into database B. I can move simple data directly, but because of the complexity of this particular query (30+ left joins), I have to store it as an array and insert it in a separate query. The examples below contain just two fields, to keep things simple and focus on the core issue.

I have working code to insert an array stored as follows:

$data = array(
    array(  // record 1, or row 1
        "1",
        "Value for field 1.2"
    ),
    array(  // record 2, or row 2
        "2",
        "Value for field 2.2"
    ),
    array(  // record 3, or row 3
        "3",
        "Value for field 3.2"
    ),   

     // etc...

);

Unfortunately, I can't get my first query to store the data like this. I have used PHP for years, but I never messed with arrays before. I'm not sure what I'm doing wrong. Using "print_r($data), this is what the results look like for how I need the array to be (This is what the $data variable looks like with print_r() ):

Array ( [0] => Array ( [0] => 1 [1] => Value for field 1.2 ) [1] => Array ( [0] => 2 [1] => Value for field 2.2 ) [2] => Array ( [0] => 3 [1] => Value for field 3.2 ) ) 

My query to create the array doesn't match this pattern. A "print_r($new_array)" command yields this:

Array ( [0] => Array ( [id] => 37 [post_title] => test1 ) [1] => Array ( [id] => 38 [post_title] => test2 ) [2] => Array ( [id] => 35 [post_title] => test3 ) [3] => Array ( [id] => 42 [post_title] => test4 ) [4] => Array ( [id] => 44 [post_title] => test5 ) [5] => Array ( [id] => 46 [post_title] => test6 ) ) 

This is my MySQL query to put the data into an array:

$test_sql = "SELECT id, post_title FROM wp_posts where post_type LIKE 'test'";
$resultTest = mysqli_query($con, $test_sql);
//$new_array[] = $row;
while ($row = mysqli_fetch_assoc($resultTest)) {
    $rows[] = $row;
}

If I understand what is going on, the array I'm creating is making key value pairs for a multidimensional array, but the format I need is not key value, just an array of values. Since I already have the insert query working, I would prefer to use a select query that stores the data in an array without the key value pairs, if that is possible, but I am open to all suggestions.

Thank you in advance for your kindness and help!

Jason
  • 871
  • 1
  • 8
  • 18
  • next time try to provide a *meaningful* question title. As soon as you'll be able to describe the desired format instead of just saying "this", you'll be able to use that Google thingy to help you – Your Common Sense Mar 19 '18 at 07:04
  • @Your Common Sense - That's really good advice. Would "How to store MYSQLI query as multidimensional array NOT as key/value pair" have been more meaningful? I to find out for myself if the new title was more meaningful, so I use that Google thingy, in an incognito window across a VPN to mask my IP (so as to not bias my results), and you know what the first answer was? – Jason Mar 19 '18 at 15:01

1 Answers1

3

All you need to do is change fetch function to fetch_row:

while ($row = mysqli_fetch_row($resultTest)) {
    $rows[] = $row;
}

Or to fetch_array with MYSQLI_NUM as second argument:

while ($row = mysqli_fetch_array($resultTest, MYSQLI_NUM)) {
    $rows[] = $row;
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64