How to get an array value after explode?

前端 未结 6 1197
感动是毒
感动是毒 2021-01-24 17:39

I have passed a value from one page to another in array. I can extract first two vars by explode but I can\'t get the third value which is formed in an array out.

This i

相关标签:
6条回答
  • 2021-01-24 17:48

    You could serialize your array parameters before you pass it. Then unserialize them on the 2nd page.

    $rate = serialize($rate);
    

    On your 2nd page before you run it through the loop:

    $rate = unserialize($rate);
    
    0 讨论(0)
  • 2021-01-24 17:52

    you can encode the values on the page as

    implode("|", array($total, $promo, implode(",",$rate));
    

    Then decode it with

    list($total, $promo, $rates) = explode("|", $_POST["user_rate"]);
    $rates = explode(",", $rates);
    
    0 讨论(0)
  • 2021-01-24 17:54

    Using serialize is definitely the key to getting this to work, but base64_encode() is the ideal way to make it less obfuscated. When you only use serialize, then you get semicolons, commas, double quotes, single quotes, and several other characters that could cause problems when you pass them through to your script that reassembles it.

    For instance, after serializing an array, you will get this, like Sidux pointed out

    a:3:{i:0;s:4:"toto";i:1;s:4:"titi";i:2;a:2:{i:0;s:4:"coco";i:1;s:4:"cici";}}
    

    When you add base64_encode to the mix you will get an easier value to work with.

    YTozOntpOjA7czo0OiJ0b3RvIjtpOjE7czo0OiJ0aXRpIjtpOjI7YToyOntpOjA7czo0OiJjb2NvIjtpOjE7czo0OiJjaWNpIjt9fQ==
    

    With this, you can easily send/receive your saved array without any trimming, adding of slashes, etcetera.

    Assuming you send your data this way, your new code will look like this

    $user_rate=$_POST['user_rate'];//15000|ss|Array
    list($total,$promo,$rate)=explode("|",$user_rate);
    $rate = unserialize( base64_decode($rate) );
    

    Now, $rate is a functional array

    0 讨论(0)
  • 2021-01-24 17:57

    Hi i think that i understand you probleme, when you passe and array from as string try to "serialize" it and "unserialize" it in the other page, for exemple :

    $array = serialize(array('toto', 'titi', array('coco', 'cici')));
    

    will give you somthing like this

    a:3:{i:0;s:4:"toto";i:1;s:4:"titi";i:2;a:2:{i:0;s:4:"coco";i:1;s:4:"cici";}}

    and if you useunserialize($array); it will give you your array back

    0 讨论(0)
  • 2021-01-24 17:58

    ok, many answers and no luck... i'll try it again:

    you have an array $rate before you submit your form:

    foreach($rate as $val){
       echo "$val<br>";
    }
    

    and you want to add $ss_total and the string "ss" and submit it so you need to:

    $newarray = array('rate'=>$rate, 'type'=>'ss', 'ss_total'=>$ss_total); 
    // now you have a 2-dimensional array ($newarray)
    
    // the next step is to prepare it for form-submitting (serialize and base64_encode):
    $stringvalue = base64_encode(serialize($newarray));    
    
    // ok, now you are able to submit it as radio-field-value:
    echo '<input type="radio" name="user_rate" id="promo_3" value="'.$stringvalue.'" />';
    

    when the form is submitted you get a serialized and encoded string in $_POST['user_rate'], you can do an echo if you'd like to see how it looks.

    Now you need to base64_decode and unserialize:

    $arr = unserialize(base64_decode($_POST['user_rate']));
    

    and you have now full access to your array:

    echo $arr['type']."<br />";
    
    echo $arr['ss_total']."<br />";
    echo $arr['rate']['index_of_rate']; // i dont know the keys of rate array...
    

    access $arr in javascript:

    echo "<script>";
    echo "var jsarr = ".json_encode($arr).";\n";
    echo "alert(jsarr.ss_total);\n";
    echo "</script>";
    

    I hope you get it now.

    0 讨论(0)
  • 2021-01-24 18:03

    this probably be a wrong $user_rate=$_POST['user_rate'];//15000|ss|Array

    you can use serialize / unserialize for array

    0 讨论(0)
提交回复
热议问题