... PS 문서는 가난보다 더 나쁜 것입니다
{addJsDef combinations[]=$combinations} but it of course won't work.
때문에, 나는 그냥 하루 추측 같아요,하지만 어쩌면 누군가가 비슷한 문제가 발생했습니다 :
내가 좋아하는 뭔가를 시도하고있다 물론, PHP에서이 같은 스마티 요소에 할당하는 경우 :
$smarty->assign('combinations', array (1,2,3,20));
당신이 멋지에서 사용할 수 있습니다
,451,515,
<script>
var combinations = [];
{foreach $combinations as $item}
combinations.push({$item})
{/foreach}
console.log(combinations);
</script>
I는 JS 콘솔 로그온 console.log(combinations);
추가로, 콘솔 그래서 모든 요소가 자바 스크립트 배열에 삽입
Array [ 1, 2, 3, 20 ]
있다. 경우
당신은 더 복잡한 PHP 배열이 있습니다
$smarty->assign('combinations', array(
'a' => 'aval',
'b' => 'bval',
'c' => array('c1' => 'c1val', 'c2' => 'c2val'),
'd' => array(
'd1' => 'd1val',
'd2' => array(
'd21' => 'd21val',
'd22' => 'd22val',
'd23' => array('d231', 'd232')
)
)
));
을 당신은 당신이 사용할 수 평면 자바 스크립트 배열을 생성합니다 :
{function jsadd}
{foreach $data as $item}
{if not $item|@is_array}
combinations.push('{$item}')
{else}
{jsadd data = $item}
{/if}
{/foreach}
{/function}
<script>
var combinations = [];
{jsadd data=$combinations}
console.log(combinations);
</script>
당신은 얻을 것이다 :
Array [ "aval", "bval", "c1val", "c2val", "d1val", "d21val", "d22val", "d231", "d232" ]
EDIT2
그리고 당신은 당신이 멋지 템플릿을 사용할 수 있습니다 주석에 설명 된대로 PHP의 데이터를 사용하여 자바 스크립트에서 다차원 배열을 생성해야하는 경우 :
{function jsadd keypart=''}
{foreach $data as $key => $item}
{if not $item|@is_array}
{if $keypart eq ''}
combinations['{$key}'] = '{$item}'
{else}
combinations{$keypart}['{$key}'] = '{$item}'
{/if}
{else}
combinations{$keypart}['{$key}'] = [];
{jsadd data = $item keypart = "`$keypart`['`$key`']" }
{/if}
{/foreach}
{/function}
<script>
var combinations = [];
{jsadd data=$combinations}
console.log(combinations['a']);
</script>
"나는 배열의 항목, 배열, 배열을 사용할 수 $ 조합 배열이를 기타." 그래서 PHP에서 다음과 같이 말하면됩니다 : $ smarty-> assign ('조합', array ('a'=> 'aval', 'b'=> 'bval', 'c'=> array 'd1'=> 'd1val', 'd2'=> 배열 ('d21'=> 'd21value'> 'c1'=> 'c1val', 'c2'=> 'c2val' , 'd22'=> 'd22val', 'd23'=> array ('d231', 'd232'))))) –
@KubaStachu 그리고 JavaScript 배열에 해당 PHP 값이 들어 있어야합니까? 샘플 데이터와 원하는 JS 배열 출력으로 질문을 편집하십시오. –
불행히도 배열을 평평하게 할 필요가 없습니다. 자바 스크립트 구조를 복제해야합니다. 실제로 prestashop 모듈에서 정적 변수와 싸우고 있지만 그들은 그냥 PrestaShop에서 작동하지 않는 것 ... –