Help Creating Multidimensional Array On The Fly?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djlex1928
Forum Commoner
Posts: 31
Joined: Sun Sep 19, 2010 3:23 pm

Help Creating Multidimensional Array On The Fly?

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help Creating Multidimensional Array On The Fly?

Post by AbraCadaver »

Much easier:

Code: Select all

while($row = mysql_fetch_assoc($result) {
    $arr[] = $row;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
mikecampbell
Forum Commoner
Posts: 38
Joined: Tue Oct 12, 2010 7:26 pm

Re: Help Creating Multidimensional Array On The Fly?

Post by mikecampbell »

You almost had it...

Code: Select all

while($row = mysql_fetch_array($result) {
    $arr[] = array (
        'id' => $row['id'],
        'user' => $row['user']
    )
}
djlex1928
Forum Commoner
Posts: 31
Joined: Sun Sep 19, 2010 3:23 pm

Re: Help Creating Multidimensional Array On The Fly?

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Help Creating Multidimensional Array On The Fly?

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply