코드와 매우 몇 가지 문제가 그래서 우리는 정말 다시 시작할 필요가 있습니다. 당신이 제공 한 것에 기초하여 ACF 리피터가 어떻게 설정되는지 정확히 알지 못하면 다음과 같은 것이 당신을 위해 일해야한다고 생각합니다. 당신이 원하는 것으로 보인다 무엇
은 다음과 같습니다
- 루프 중계기의 모든 제품을 통해
- 모든 제품에 걸쳐 독특한 값을 얻기 당신의
product_attributes
- 에서 모든 값을 가져옵니다
- 같은 줄에 공백으로 구분 된 값을 표시하십시오.
주요 문제는 고유 배열을 얻는 것입니다. 이 작업을 수행하는 방법은 여러 가지가있다 : 당신을 가장 복잡한에게 이전 값
이것은 당신이 순간에 그것을 시도하는 방법입니다을 확인
1. 사용와 in_array를 선택했지만 당신은 논리에 문제가 있습니다. 그러므로 나는 옵션 2를 제안 싶지만, 완전성이 당신이 그것을해야하는 방법이다 : 이에 대한 코드는 이전 옵션보다 훨씬 간단
$filter_attributes = array();
if (have_rows('product')): while (have_rows('product')) : the_row();
$attributes = get_sub_field_object('product_attributes');
$values = $attributes['value'];
if($values) {
foreach ($values as $value)
if (!in_array($value, $filter_attributes)) {
$filter_attributes[] = $value;
}
}
} endwhile; endif;
/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes);
2.
array_unique. 모두 값을 배열에 저장 한 다음 끝에 array_unique
(PHP.net array_unique)을 사용합니다. 따라서 매번 기존 값을 확인할 필요가 없습니다. 예 :
$all_attributes = array();
$filter_attributes = array();
if (have_rows('product')): while (have_rows('product')) : the_row();
$attributes = get_sub_field_object('product_attributes');
$values = $attributes['value'];
if($values) {
foreach ($values as $value)
$all_attributes[] = $value; /* Save ALL values */
}
} endwhile; endif;
/* use array_unique to remove duplicates from the array */
$filter_attributes = array_unique ($all_attributes);
/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes);
3. 중복 키가 배열에서 허용되지 않기 때문에 각 값이 고유해질 수있게됩니다 그 다음, 당신은 배열 키 값을 사용하는 경우
키 배열입니다.그것은 약간의 방법-Y를 해킹,하지만 것의 빠른 & 쉽게 :`$의 values`에서 오는
입니다
$filter_attributes = array();
if (have_rows('product')): while (have_rows('product')) : the_row();
$attributes = get_sub_field_object('product_attributes');
$values = $attributes['value'];
if($values) {
foreach ($values as $value)
/* if $all_attributes[$value] already exists, this will overwrite it
ensuring the array only has unique values */
$all_attributes[$value] = $value;
}
} endwhile; endif;
/* display the values on the same line, separated by spaces */
echo implode(" ", $filter_attributes);
? 나는 그것이'$ 속성들 '이어야한다고 생각하니? – FluffyKitten
$ 속성으로 업데이트했습니다. 루프가 계속 중복을 생성합니다. – SBWT
질문이 매우 모호했기 때문에 아무런 결과도 얻지 못했다고 생각했습니다 ("지금까지는 주사위를 잘못 쓰지 않아도 무엇이 잘못되었는지 알 수 없습니다"). 그래서 문제는 사실'$ filter_attributes' 배열에 중복 된 것이 있습니까? 'foreach' 루프에서 여전히'$ values '를 사용하고 있기 때문에 어떻게 동작합니까? '$ values '는 무엇이고 올바른 값을 가지는 방법은 무엇입니까? 귀하의 코드가 무엇을 하려는지 확실하지 않습니다. 'if (in_array ...) '에 의해 달성하고자하는 것은 무엇입니까? – FluffyKitten