0

I have an array after dd() in php it show like this array:

1 [▼0 => "1,18,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,19,20,21,22,23,24"]

now I want to loop the array and pass only those values which are more than 200. Here is the code

 $('#btnSave').on('click', function () {
   var checkedIds = mytree.getCheckedNodes();
   $('#userData').val(checkedIds);
 });        

where #userData is a hidden field and checkedIds has the above values.

How to loop and make a condition to assign only the desired values to #userData

5
  • 5
    Your array has only one element and it is a comma delimited string. There are plenty of resources that explain how to turn a string like that into an actual array. Commented Mar 25, 2021 at 12:39
  • 1
    Please do not tag questions with improper tags just to get them more available. php tag is redundant here. Commented Mar 25, 2021 at 12:43
  • yes, if i selected two nodes in treeview it shows the following array:1 [▼ 0 => "1,18,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,19,20,21,22,23,24,9,90,298,299,91,300,301,92,302,303,93,304,94,305,306,95,307,96,308,97,309 ◀" ] in one element . i will try to search on string to array. if u have possible soultion please Commented Mar 25, 2021 at 12:43
  • @biesior I'd beg to differ. The source of the data is an array coming from PHP. Reformatting data into a desired shape is best done at the source whenever possible, to relieve the client of any extra work. Commented Mar 25, 2021 at 12:47
  • Ismail, I can advise you to have a look at explode and array_filter. Commented Mar 25, 2021 at 12:49

1 Answer 1

1

In PHP, instead of using dd() encode the array into JSON and put it into an element's data attribute (with proper escaping): <a data-array="<?php escape(json_encode($array))">...</a>. Then in js, you can decode the JSON again: const array = JSON.parse(element.getAttribute('data-array')).

Sign up to request clarification or add additional context in comments.

1 Comment

Putting a value into an otherwise irrelevant DOM element does not seem a good way to go about this. You can put it directly in a js variable const array = JSON.parse("<?php escale(json_encode($array,....

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.