1

I have 'N' single dimensional arrays(where N represents number of records retrieved by a sql command) How do I convert it to multi dimensional array with keys as index? At first I thought of creating a separate key array and then combining it with those N arrays

My output

Array ( [0] => PONDICHERRY [1] => 31-Jan-2018 [2] => [3] => distance and       height are not proportional. ) 
Array ( [0] => PONDICHERRY [1] => 03-Feb-2020 [2] => [3] => helloooooooooooooo )   

My expected output

Array ( [0] => Array ( [0] => PONDICHERRY [1] => 31-Jan-2018 [2] => [3] => distance and height are not proportional. ) [1] =>Array ( [0] => PONDICHERRY [1] => 03-Feb-2020 [2] => [3] => helloooooooooooooo )  )

It's difficult for me to combine the arrays, but how do i create like this?

Thank you

1
  • Two seperate arrays than use $arr[] =array_merge Commented Jan 24, 2016 at 14:02

1 Answer 1

1

Rather than creating N separate arrays and then combining them later, you can combine them already while reading the rows.

$results = array();
while($row = $query->fetch_row()){
  $results[] = $row;
}
// here you have $results in the expected output format
Sign up to request clarification or add additional context in comments.

1 Comment

except the indices are strings instead of integer, your output would give something like this for me: Array ( [0] => Array ( [StationName] => PONDICHERRY [DOI] => 31-Jan-2018 [Remarks] => distance and height are not proportional. flask a [RainGauge] => ) [1] => Array ( [StationName] => PONDICHERRY [DOI] => 03-Feb-2020 [Remarks] => helloooooooooooooo [RainGauge] => ))

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.