다차원 배열에 익숙하지 않아 문제가 발생하여 머리를 감쌀 수 없습니다. 나는이 설정하는 것을 시도하고있다 : 이런 식으로 뭔가에PHP/MySQL 다차원 배열
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 ) ) )
을, 그래서 나는 범주를 호출하고 아래 관련 아티스트를 나열 할 수 있습니다
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 ) ) )
을 나는 다음과 같은 코드를 사용하고 있습니다 :
을$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']);
}
첫 번째 배열 그것을 할 수있는 다른 방법의 존재 여부를 확인하는 것입니다해야 루프 외부에 있으니 두 번째 루프에 있어야합니다. '$ isSet = false;'외부 루프 다음에 루프를 추가'if (! $ isSet) {firstArray ...; $ isSet = TRUE; } else {secondArray ...; }' – SSpoke
동일한 배열에 다른 값을 가진 동일한 배열 키 (artist_id, lastName)가 없어야합니다. –