If I have an array in php:
$items = array(
"nombtre" => "botella";
"precio" => "1";
"descripccion" = "Botella de cristal"
);
How can I send it to jquery? to use all the index of it?
If I have an array in php:
$items = array(
"nombtre" => "botella";
"precio" => "1";
"descripccion" = "Botella de cristal"
);
How can I send it to jquery? to use all the index of it?
To declare a multi-dimensional array in JavaScript from data of a PHP variable it is best to generate the necessary code (in the HTML document) to define its value using the function json_encode()
:
<?php
$prueba = [
'valor1',
[
'valor2.1',
'valor2.2',
],
[
'indice3.1' => 'valor3.1',
'indice3.2' => 'valor3.2',
],
];
?><script type="text/javascript">
var prueba = <?= json_encode($prueba,
JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS
) ?>;
console.log(prueba);
</script>
Note that if a matrix is associative, it will become an object instead of an array. In the third value of my example matrix you can see that effect.
Although the security of the function json_encode()
is usually enough, a bad implementation in the browser or changing the default mode could be a problem, so it is advisable to use the options JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS
. Without these options </script>
would be converted to <\/script>
and with them it would become \u003C\/script\u003E
.
Edit: Well, in the end I saw that you are not talking about a multi-dimensional matrix, but an associative matrix (just what I was saying at the end of my original response).
Here is the exact code for the example that you propose with syntax, but not spelling, corrected:
<?php
$prueba = [
'nombtre' => 'botella',
'precio' => '1',
'descripccion' => 'Botella de cristal',
];
?><script type="text/javascript">
var prueba = <?= json_encode($prueba,
JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS
) ?>;
console.log(prueba);
</script>
If what you want is to use only the indexes (it is somewhat confusing the meaning of your question "to use all the indexes of the same?") would have to use array_keys()
:
<?php
$prueba = [
'nombtre' => 'botella',
'precio' => '1',
'descripccion' => 'Botella de cristal',
];
?><script type="text/javascript">
var prueba = <?= json_encode($prueba,
JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS
) ?>;
console.log(prueba);
</script>
I hope that these last two examples will satisfy all the questions you ask.
Since PHP runs on the server and javascript on the client, you can not "send" it in the strictest use of the word.
But there are ways to use it:
<script>
<?php
$items = array(
"nombtre" => "botella",
"precio" => "1",
"descripccion" => "Botella de cristal"
);
echo "var items " . json_encode($items);
?>
console.log(items);
</script>
This is one of the many ways you have to do it, simply "draw" the information in the place and in the appropriate format.
This code is for illustrative purposes only, and it is very improvable.
Be careful also in how you define your arrays, the element separator is the comma ",", not the semi-colon ";".