Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am new to multidimensional arrays and ran into a problem and cannot wrap my head around it. I have been trying to turn this:

Array
(
    [1] => Array
        (
            [cat_id] => 1
            [cat_name] => Schilderijen
            [cat_description] => Omschrijving bij schilderijen
            [artists] => Array
                (
                    [artist_id] => 1
                    [lastName] => ..some value
                )

        )

)
Array
(
    [1] => Array
        (
            [cat_id] => 1
            [cat_name] => Schilderijen
            [cat_description] => Omschrijving bij schilderijen
            [artists] => Array
                (
                    [artist_id] => 4
                    [lastName] => ..some value
                )

        )

)

into something like this, so I can call a category and list the related artists underneath:

Array
(
    [1] => Array
        (
            [cat_id] => 1
            [cat_name] => Schilderijen
            [cat_description] => Omschrijving bij schilderijen
            [artists] => Array
                (
                    [artist_id] => 1
                    [lastName] => ..some value
                    [artist_id] => 4
                    [lastName] => ..some value
                )

        )

)

I am using the following code:

$cat_id = 1;

$query = "SELECT * FROM `categorie_has_artists` ";
$query .= " JOIN `categories` ON categories.cat_id = categorie_has_artists.cat_id AND categorie_has_artists.cat_id = :cat_id"; 
$query .= " JOIN `artists` ON artists.artist_id = categorie_has_artists.artist_id";

$stmt = $dbh->prepare($query);
$stmt->bindParam(':cat_id', $cat_id, PDO::PARAM_INT);
$stmt->execute();

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  $array[$row['cat_id']] = array('cat_id' => $row['cat_id'], 'cat_name' => $row['cat_name'], 'cat_description' => $row['cat_description'], 'artist' => array());
  $array[$row['cat_id']]['artist'][] = array('artist_id'=>$row['artist_id'], 'lastName' => $row['lastName']);
}
share|improve this question
    
first array should be outside loop, second one should be in loop. make $isSet = false; outside loop then inside loop add if(!$isSet) { firstArray...; $isSet = TRUE; } else { secondArray...; } –  SSpoke Jan 5 '14 at 12:44
    
You should not have same array key (artist_id,lastName) with different values in same array. –  Awlad Liton Jan 5 '14 at 12:51

1 Answer 1

up vote 1 down vote accepted

I haven't used PHP in a while this might not work.

Better way would be to check if atleast one artist subarray exists instead of $isSet flag

$cat_id = 1;

$query = "SELECT * FROM `categorie_has_artists` ";
$query .= " JOIN `categories` ON categories.cat_id = categorie_has_artists.cat_id AND categorie_has_artists.cat_id = :cat_id"; 
$query .= " JOIN `artists` ON artists.artist_id = categorie_has_artists.artist_id";

$stmt = $dbh->prepare($query);
$stmt->bindParam(':cat_id', $cat_id, PDO::PARAM_INT);
$stmt->execute();

$isSet = FALSE;

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  if(!$isSet) {
    $array[$row['cat_id']] = array('cat_id' => $row['cat_id'], 'cat_name' => $row['cat_name'], 'cat_description' => $row['cat_description'], 'artist' => array());
    $isSet = TRUE;
  } else {
    $array[$row['cat_id']]['artist'][] = array('artist_id'=>$row['artist_id'], 'lastName' => $row['lastName']);
  }
}

This is the other way to do it (also might not work)

$cat_id = 1;

$query = "SELECT * FROM `categorie_has_artists` ";
$query .= " JOIN `categories` ON categories.cat_id = categorie_has_artists.cat_id AND categorie_has_artists.cat_id = :cat_id"; 
$query .= " JOIN `artists` ON artists.artist_id = categorie_has_artists.artist_id";

$stmt = $dbh->prepare($query);
$stmt->bindParam(':cat_id', $cat_id, PDO::PARAM_INT);
$stmt->execute();

while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  if(!isset($array[$row['cat_id']][$row['cat_name']][$row['cat_description']])) {
    $array[$row['cat_id']] = array('cat_id' => $row['cat_id'], 'cat_name' => $row['cat_name'], 'cat_description' => $row['cat_description'], 'artist' => array());
  } else {
    $array[$row['cat_id']]['artist'][] = array('artist_id'=>$row['artist_id'], 'lastName' => $row['lastName']);
  }
}
share|improve this answer
1  
Thanks for the tips on the isset. This works for me: foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) { $artist[] = array ( 'artist_id' => $r['artist_id'], 'lastName' => $r['lastName'] ); } $stmt->execute(); foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $r) { if(!isset($category)) { $category[] = array ( 'cat_id' => $r['cat_id'], 'cat_description' => $r['cat_description'], 'artists' => $artist ); } } –  user3140559 Jan 5 '14 at 13:04

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.