Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here i have an array

 Array
(
   [0] => stdClass Object
       (
           [gallery_id] => 25
           [title] => Our Job is Your Vacation
           [image_path] => 1.png
       )

   [1] => stdClass Object
       (
           [gallery_id] => 25
           [title] => Our Job is Your Vacation
           [image_path] => 2.png
       )

   [2] => stdClass Object
       (
           [gallery_id] => 25
           [title] => Our Job is Your Vacation
           [image_path] => 3.jpg
       )

   [3] => stdClass Object
       (
           [gallery_id] => 26
           [title] => enjoy vacation
           [image_path] => 1.jpg
       )
   [4] => stdClass Object
       (
           [gallery_id] => 26
           [title] => enjoy vacation
           [image_path] => 2.jpg
       )
)

Expected array to be

   Array
    (
       [0] => stdClass Object
           (
               [gallery_id] => 25
               [title] => Our Job is Your Vacation
               [image_path] => array{
                                      '1.png',
                                      '2.png',
                                      '3.png'
                                    }
           )
       [1] => stdClass Object
           (
               [gallery_id] => 26
               [title] => enjoy vacation
               [image_path] => array{
                                      '1.jpg',
                                      '2.jpg'
                                     }
           )
    }

How can i get this, could anyone help me out.

EDIT Below are the table structures

table1 - gallery

**gallery_id**        **title**
'25'              'Our Job is Your Vacation'
'26'              'enjoy vacation'

table2 - gallery_images

**id**     **gallery_id**        **image_path**
1            '25'              '1.png'
2            '25'              '2.png'
3            '25'              '3.png'
4            '26'              '1.jpg'
5            '26'              '2.jpg'
share|improve this question
    
how had yo saved it in database i means that what is the format of your table?? –  Drudge Nov 21 '13 at 12:15
    
@Drudge i have edited the question check it once –  user123456789 Nov 21 '13 at 12:26
    
I.m sure You can do this by changing your SQL query. Try with GROUP CONCAT etc. –  Adam Nov 21 '13 at 12:29
    
@Adam i want query in codeigniter –  user123456789 Nov 21 '13 at 12:30
    
can u post your query that u had used?? –  Drudge Nov 21 '13 at 12:41
show 1 more comment

2 Answers

up vote 2 down vote accepted

I think this is a terrible way, it would be best getting the data correct formated from the database. but try with this.

Where $youArray is your old array.

<?php 

$newArray = array();
//put here your array instead of $yourArray
foreach ($youArray as $key => $value) { 
  if(empty($newArray)) {
    array_push($newArray,$value);
    continue;
  }
  foreach ($newArray as $k => $v) {

    if($v->gallery_id == $value->gallery_id) {

      if($v->image_path == $value->image_path) {
        continue;
      } else {
        if(is_array($newArray[$k]->image_path)) {
          array_push(
            $newArray[$k]->image_path,
            $value->image_path
          );
        } else {
          $newArray[$k]->image_path = array(
            $newArray[$k]->image_path,
            $value->image_path
          );
        }
      }
    } else {
      $repeated = 0;
      foreach ($newArray as $ky => $val) {
        if($newArray[$ky]->gallery_id == $value->gallery_id) {
          $repeated ++ ;
        }
      }
      if(!$repeated) {
        array_push($newArray,$value);
      }
    }
  }
}

var_dump($newArray);
share|improve this answer
add comment

I'm not sure but query like this could group your image_path :

SELECT title,
GROUP_CONCAT(DISTINCT image_path SEPARATOR ',') as image_path
FROM gallery_images JOIN gallery ON gallery_images.gallery_id = gallery.id
GROUP BY student_name;

Then in PHP You have to explode(",",$image_path);

It isn't final solution but I hope this would help You

share|improve this answer
    
Note that GROUP_CONCAT has a max length set by the group_concat_max_len system variable (i think it defaults to 1024 chars). –  Vlad Preda Nov 21 '13 at 12:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.