Page 1 of 1
Help Creating Multidimensional Array On The Fly?
Posted: Mon Dec 06, 2010 11:31 am
by djlex1928
Hi There I need to create the following multidimensional array on the fly from a database loop.
array (
'id' => '1',
'user' => 'alex'
),
array (
'id => '2',
'user' = 'tom',
);
I need to somehow be able to create this on the fly in a loop beforehand.
eg.
Code: Select all
while($row = mysql_fetch_array($result) {
$arr = $arr.array (
'id' => $row['id'],
'user' => $row['user']
),
}
I know the code above doesn't work, but that is how I would like it to work. I just do not have a clue how to acheive this, can anyone help?
Re: Help Creating Multidimensional Array On The Fly?
Posted: Mon Dec 06, 2010 11:48 am
by AbraCadaver
Much easier:
Code: Select all
while($row = mysql_fetch_assoc($result) {
$arr[] = $row;
}
Re: Help Creating Multidimensional Array On The Fly?
Posted: Mon Dec 06, 2010 1:24 pm
by mikecampbell
You almost had it...
Code: Select all
while($row = mysql_fetch_array($result) {
$arr[] = array (
'id' => $row['id'],
'user' => $row['user']
)
}
Re: Help Creating Multidimensional Array On The Fly?
Posted: Mon Dec 06, 2010 3:25 pm
by djlex1928
mikecampbell wrote:You almost had it...
Code: Select all
while($row = mysql_fetch_array($result) {
$arr[] = array (
'id' => $row['id'],
'user' => $row['user']
)
}
Thanks buddy, that's nearly what I needed.
The only problem is I am uploading the data to a soap server and using the code above adds indexes to the array and the soap server returns "invalid array supplied".
Instead of:
array (
'id' => '1',
'user' => 'alex'
),
array (
'id => '2',
'user' = 'tom',
);
It creates
array (
'0' => array (
'id' => '1',
'user' => 'alex'
),
'1' => array (
'id => '2',
'user' = 'tom',
)
);
Is there any way to acheive the first example array?
Re: Help Creating Multidimensional Array On The Fly?
Posted: Mon Dec 06, 2010 3:54 pm
by AbraCadaver
Then you want to submit individual arrays. What is the code you are using to submit? In a loop? Using mysql_fetch_assoc(), just submit each $row.