Array manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lukemack

    #1

    Array manipulation

    Hi,

    I have an array in the following format :

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

    [0] =Array
    (
    [GROUP_CONCAT(DI STINCT destination_ids )] =9
    )

    )

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

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

    )

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

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

    )

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

    [0] =Array
    (
    [GROUP_CONCAT(DI STINCT 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(DI STINCT 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.

  • fel

    #2
    Re: Array manipulation

    On May 31, 10:37 am, lukemack <lukemst...@gma il.comwrote:
    Hi,
    >
    I have an array in the following format :
    >
    Array
    (
    [0] =Array
    (
    [itineries] =Array
    (
    [ship_id] =1
    )
    >
    [0] =Array
    (
    [GROUP_CONCAT(DI STINCT destination_ids )] =9
    )
    >
    )
    >
    [1] =Array
    (
    [itineries] =Array
    (
    [ship_id] =6
    )
    >
    [0] =Array
    (
    [GROUP_CONCAT(DI STINCT destination_ids )] =9,2,21
    )
    >
    )
    >
    [2] =Array
    (
    [itineries] =Array
    (
    [ship_id] =7
    )
    >
    [0] =Array
    (
    [GROUP_CONCAT(DI STINCT destination_ids )] =9,12,4
    )
    >
    )
    >
    [3] =Array
    (
    [itineries] =Array
    (
    [ship_id] =8
    )
    >
    [0] =Array
    (
    [GROUP_CONCAT(DI STINCT 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(DI STINCT 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=ar ray();
    foreach($main_a rray as $item){
    $stuff = explode(',',$it em['GROUP_CONCAT(D ISTINCT destination_ids )']);
    foreach($stuff as $thing){
    $other_array[$item[$thing]][]=$item['ship_id'];
    }
    print_r($other_ array);





    which answer do you preffer?



    Comment

    Working...