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

Array manipulation

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.