0

I have a PHP array dd($iperfProfiles);

array:1 [▼
  0 => "ipv6-udp-upload"
]

Trying to access it in JS

I've tried

var iperfProfiles = "{{ json_encode($iperfProfiles) }}";
console.log(iperfProfiles);
return false;

I get

IPERF:1699 ["ipv6-udp-upload"]

How do I basically turn a PHP array into a JS array ?

2
  • 2
    You have to print it without escaping the output, by using var iperfProfiles = {!! json_encode($iperfProfiles) !!}; - There's also a JSON blade directive var iperfProfiles = @json($iperfProfiles); which is probably your best bet. Commented Oct 15, 2020 at 18:16
  • 1
    var iperfProfiles = {!! json_encode($iperfProfiles) !!}; no quotes. Commented Oct 15, 2020 at 18:21

3 Answers 3

1
//in PHP 
$iperfProfiles = json_encode($iperfProfiles);

//and then in JS
var iperfProfiles = JSON.parse({!!$iperfProfiles!!});
Sign up to request clarification or add additional context in comments.

2 Comments

Call to undefined function App\Http\Controllers\json_string()
That was just a typo, wo sorry for that.
1

just use @json blade directive and pass raw php data to it without encoding it JSON.

in blade template:

const profiles = "@json($profiles)"

Comments

1

try with just removing quotes

var iperfProfiles = {{ json_encode($iperfProfiles) }};
console.log(iperfProfiles);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.