1- Soit coté PHP :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?php
foreach( $tabCat as $index => $categorie )
{
$tabCat_js[$index] = [];
foreach( $categorie as $idx => $elt )
{
$tabCat_js[$index][$idx] = $elt.'='.($idx+1).';'; // de la forme 'Types=1;'
}
$tabCat_js[$index] = implode('',$tabCat_js[$index]); // on concatène
}
?>
<script>
var tabCat = <?php echo json_encode($tabCat_js); ?>;
console.log( tabCat );
</script> |
2- Soit coté JavaScript :
1 2 3 4 5 6 7 8 9 10 11 12 13
| <script>
var tabCat_json = <?php echo json_encode($tabCat); ?>;
var tabCat = [];
tabCat_json.forEach( function(categorie,index){
tabCat[index] = [];
categorie.forEach( function(elt,idx){
tabCat[index][idx] = elt+'='+(idx+1)+';'; // de la forme 'Types=1;'
});
tabCat[index] = tabCat[index].join(''); // on concatène
});
console.log( tabCat );
</script> |
Au choix.
Dans les 2 cas, on obtient :
Array [ "Types=1;Ingrédients=2;", "Côtés=1;Champs=2;Surfaces=3;" ]
Partager