Function does not remove array elements

Asked

Viewed 520 times

0

Within the PHP i add two arrays and then I add them into a variable of a print with JS.

<?php
$objp = array();    
$objs = array();

print("<SCRIPT language=javascript> 
         objp = \"$objp\";
         objs = \"$objs\";
         n_p = new Array (10);
         </SCRIPT>");   
?>

Now inside the JavaScript I use a for when I submit to add the contents of these two arrays within the n_p;

<script language="javascript">

    for(var i = 0; i < 10; i++){
        if(objp!=''){
            n_p[i] = objp;
            objp.shift();
        }
        else{
            n_p[i] = objs;
            objs.shift();
        }   
    }
</script>

The problem is, I’m using the shift() to remove the first element of array, but when he goes inside the if it does not perform. What could be the problem? The function objs.shift(); is not executed!

Obs* I used the splice() but ended up not working either!

  • 2

    language="javascript" is discontinued, must be type="text/javascript" or may carry nothing in certain types of document.

  • @Zuul edited the way you showed, but ended up not solving either. But I appreciate the help!

  • 1

    It wasn’t really a help for your current problem but rather a warning about the standards :) @bfavaretto is well on the way to solving your issue!

  • 1

    What happens is simple... If you are comparing the objp array with nothing and it has something will return false. rsrs. Obvious right. If you want to check if the first location is empty you should compare the identifier of the first space: if (objp[0] != '')

1 answer

2


Your variables objp and objs in Javascript are not arrays, are strings. PHP will simply issue "Array()" for them. Your PHP would need to be like this:

print("<SCRIPT language=javascript> 
     objp = " . json_encode($objp) . ";
     objs = " . json_encode($objs) . ";
     n_p = new Array (10);
     </SCRIPT>");

Assuming I understand what you want with the Javascript loop, it should look like this:

for(var i = 0; i < 10; i++){
    if(objp[i] != ''){
        n_p[i] = objp[i];
        objp.splice(i, 1);
    }
    else{
        n_p[i] = objs[i];
        objs.splice(i, 1);;
    }   
}
  • Thanks @bfavaretto did not know this method! But in the same way, the for in the JS does not perform after the if(); to be more exact, it does not perform the function objp.shift();

  • I made an edit including modifications to JS. See if this is it.

  • Thanks for the @bfavaretto edition, but it didn’t work anyway, apparently it is a Mozilla error. But in the case of its edition, the i which is being as accountant could catch the next element of the array, that is to say, the accountant of the first array come to 3 and contain some elemento in the first position of objs could go wrong!

  • You can explain better what you’re trying to do?

  • I’m simply adding to arrays the value of a select and adding to the n_p to move on to the next program! Obs it is necessary for me to do this to perform another search in the next, but this is optional.

  • But I still don’t understand how the new array will be built from the other two. What can each of them contain? Are the sizes always the same? It would be better to solve this directly in PHP, and pass the array already ready for Javascript (unless you will also use the two originals in JS).

  • Look what I commented on in your publication..

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.