469,927 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Array manipulation

Hi,

I have an array in the following format :

Array
(
[0] =Array
(
[itineries] =Array
(
[ship_id] =1
)

[0] =Array
(
[GROUP_CONCAT(DISTINCT destination_ids)] =9
)

)

[1] =Array
(
[itineries] =Array
(
[ship_id] =6
)

[0] =Array
(
[GROUP_CONCAT(DISTINCT destination_ids)] =9,2,21
)

)

[2] =Array
(
[itineries] =Array
(
[ship_id] =7
)

[0] =Array
(
[GROUP_CONCAT(DISTINCT destination_ids)] =9,12,4
)

)

[3] =Array
(
[itineries] =Array
(
[ship_id] =8
)

[0] =Array
(
[GROUP_CONCAT(DISTINCT destination_ids)] =>
15,23,26,15,19,26
)

)

Yes, I'm querying a badly designed database in which destination_ids
are stored as comma separated fields in a single field. I didnt design
it and changing it is unfortunately not an option, at least for now.
The itineraries table (the source of the array) is the only point in
the database where the destination and ship_id are linked so I need to
figure out a way of converting the above array so that i have a list
of ships per destination id, instead of the other way round.

the query i am using is $query = 'SELECT ship_id,
GROUP_CONCAT(DISTINCT destination_ids) FROM itineries GROUP BY
ship_id;';

if i change this query to group by distinct destination ids, i still
end up with some destination id entries with more that one value
(comma separated).

can anyone suggest how i can manipulate the above array using array
functions to get the resultset i need?

many thanks,

lukemack.

May 31 '07 #1
1 2131
fel
On May 31, 10:37 am, lukemack <lukemst...@gmail.comwrote:
Hi,

I have an array in the following format :

Array
(
[0] =Array
(
[itineries] =Array
(
[ship_id] =1
)

[0] =Array
(
[GROUP_CONCAT(DISTINCT destination_ids)] =9
)

)

[1] =Array
(
[itineries] =Array
(
[ship_id] =6
)

[0] =Array
(
[GROUP_CONCAT(DISTINCT destination_ids)] =9,2,21
)

)

[2] =Array
(
[itineries] =Array
(
[ship_id] =7
)

[0] =Array
(
[GROUP_CONCAT(DISTINCT destination_ids)] =9,12,4
)

)

[3] =Array
(
[itineries] =Array
(
[ship_id] =8
)

[0] =Array
(
[GROUP_CONCAT(DISTINCT destination_ids)] =>
15,23,26,15,19,26
)

)

Yes, I'm querying a badly designed database in which destination_ids
are stored as comma separated fields in a single field. I didnt design
it and changing it is unfortunately not an option, at least for now.
The itineraries table (the source of the array) is the only point in
the database where the destination and ship_id are linked so I need to
figure out a way of converting the above array so that i have a list
of ships per destination id, instead of the other way round.

the query i am using is $query = 'SELECT ship_id,
GROUP_CONCAT(DISTINCT destination_ids) FROM itineries GROUP BY
ship_id;';

if i change this query to group by distinct destination ids, i still
end up with some destination id entries with more that one value
(comma separated).

can anyone suggest how i can manipulate the above array using array
functions to get the resultset i need?

many thanks,

lukemack.

there are 2 answers for your question.
first:
------

php.net/explode

second:
-------

$other_array=array();
foreach($main_array as $item){
$stuff = explode(',',$item['GROUP_CONCAT(DISTINCT destination_ids)']);
foreach($stuff as $thing){
$other_array[$item[$thing]][]=$item['ship_id'];
}
print_r($other_array);

which answer do you preffer?

Jun 1 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Chris Lambacher | last post: by
5 posts views Thread by Johnny Meredith | last post: by
7 posts views Thread by Jamil Anwar Zaman | last post: by
20 posts views Thread by K.M. Jr. | last post: by
12 posts views Thread by Sheldon | last post: by
9 posts views Thread by Nathan Sokalski | last post: by
15 posts views Thread by DL | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.