You are here: Re: Array manipulation « PHP Programming Language « IT news, forums, messages
Re: Array manipulation

Posted by fel on 05/31/07 23:59

On May 31, 10:37 am, lukemack <lukemst...@gmail.com> wrote:
> 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?

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация