By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,747 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,747 IT Pros & Developers. It's quick & easy.

bidimensional array from query

P: n/a
Hi, I have the following MySQL table:

inner_id data1 data2 data3

->0 g sd ds

1 a n y
2 b o w

->3 c p aa
->3 d q bb
->3 e r cc

4 h u ff

5 i v gg
5 l z hh

6 m x i

After a query, I need to put all data in a bidimensional array this way:
every index of the array is an array, so that:

my_array[0] is array("data1"=>g,"data2"=>sd,"data3"=>ds)

BUT

my_array[3] is
array(array("data1"=>c,"data2"=>p,"data3"=>aa),arr ay("data1"=>d,"data2"=>q,"data3"=>bb),array("data1 "=>e,"data2"=>r,"data3"=>cc))

as for data1, data2, data3 labels, it is NOT important they match the
name of the field ("data1", "data2" can be ok too).

I simply can't do it: would you gently tell me how to do? Thanks!

Feb 2 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Anna wrote:
After a query, I need to put all data in a bidimensional array this way:
every index of the array is an array, so that:

my_array[0] is array("data1"=>g,"data2"=>sd,"data3"=>ds)
I don't think you can make the INDEX (key) an array.
The value - yes, but the key - no.

What you've got there is an "associative" array.
Feb 2 '07 #2

P: n/a
Sanders Kaufman ha scritto:
Anna wrote:
>After a query, I need to put all data in a bidimensional array this way:
every index of the array is an array, so that:

my_array[0] is array("data1"=>g,"data2"=>sd,"data3"=>ds)

I don't think you can make the INDEX (key) an array.
The value - yes, but the key - no.

What you've got there is an "associative" array.


maybe I did not explain well the problem:

<?php
mysql_connect("localhost", "root", "") or
die("Connessione non riuscita: " . mysql_error());
mysql_select_db("db");

$result = mysql_query("SELECT inner_id,data1,data2,data3 FROM test");

$ext = array();
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

// here I want to transform my result in a bidimensional array
// where $row[0] is an index of an indexed numeric array of arrays
// --->when inner_id is a unique value, it must be a single array,
// --->while, if inner_id is multiple (that is, many records share that
inner_id) the result must be pushed in an array of arrays

// I'm sure it can be done but I don't know how

$ext[$row[0]] = ...

}
?>
Feb 2 '07 #3

P: n/a
Hi, I was about to reach the solution but...
By using this in that while loop:

if(!$ext[$row[0]]){
array_push($ext,array("a"=>$row[1],"b"=>$row[2],"c"=>$row[3]));
}
else
{
array_push($ext[$row[0]],array("a"=>$row[1],"b"=>$row[2],"c"=>$row[3]));
}
I get:

Array ( [0] =Array ( [a] =aa [b] =ab [c] =ac )
[1] =Array ( [a] =ba [b] =bb [c] =bc )
[2] =Array ( [a] =ca [b] =cb [c] =cc )
[3] =Array ( [a] =da [b] =db [c] =dc
[0] =Array ( [a] =ea [b] =eb [c] =ec )
[1] =Array ( [a] =fa [b] =fb [c] =fc ) )
[4] =Array ( [a] =ga [b] =gb [c] =gc )
[5] =Array ( [a] =ha [b] =hb [c] =hc
[0] =Array ( [a] =ia [b] =ib [c] =ic ) )
[6] =Array ( [a] =la [b] =lb [c] =lc ) )
This would be good, but there's a problem:

the first element at index [3] must be [0] (then [1] and [2]) while at
the moment it is "anonymous", AND

the first element at index [5] must be [0] too.
Could anyone help me?


Feb 3 '07 #4

P: n/a
THIS:
Array ( [0] =Array ( [a] =aa [b] =ab [c] =ac )
[1] =Array ( [a] =ba [b] =bb [c] =bc )
[2] =Array ( [a] =ca [b] =cb [c] =cc )
[3] =Array ( [a] =da [b] =db [c] =dc
[0] =Array ( [a] =ea [b] =eb [c] =ec )
[1] =Array ( [a] =fa [b] =fb [c] =fc ) )
[4] =Array ( [a] =ga [b] =gb [c] =gc )
[5] =Array ( [a] =ha [b] =hb [c] =hc
[0] =Array ( [a] =ia [b] =ib [c] =ic ) )
[6] =Array ( [a] =la [b] =lb [c] =lc ) )

SHOULD BE:

Array ( [0] =Array ( [a] =aa [b] =ab [c] =ac )
[1] =Array ( [a] =ba [b] =bb [c] =bc )
[2] =Array ( [a] =ca [b] =cb [c] =cc )
[3] =Array ( [0] =Array([a] =da [b] =db [c] =dc)
[1] =Array ( [a] =ea [b] =eb [c] =ec )
[2] =Array ( [a] =fa [b] =fb [c] =fc ) )
[4] =Array ( [a] =ga [b] =gb [c] =gc )
[5] =Array ( [0] =Array([a] =ha [b] =hb [c] =hc)
[1] =Array ( [a] =ia [b] =ib [c] =ic ) )
[6] =Array ( [a] =la [b] =lb [c] =lc ) )
Feb 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.