Hi,

is it possible to have a large string, and then explode it into an array but have the first bit of the explode as the key and the second as the value?

i.e.

$exploded = explode(':', $string);

$thisarray();
foreach ($exploded as $key => $val){
    $thisarray[$key] = $val;

}


Something like that. But it doesnt work.

Any help apreciated.

    Could you show us a hypothetical example of a string and what you'd like the final result to be?

      Sure, ok here is my first shot at it.

      $string = "q🅰b:f:t:y";
      explode(':', $string);

      i would like use the array as follows:

      $array[q];
      which would ouput "a";

      $array;
      which would ouput "f";

      and so on.

        <?php
        $string = "q:a:b:f:t:y";
        $parts = explode(':', $string);
        $array = array();
        while(count($parts))
        {
           $key = array_shift($parts);
           if(count($parts))
           {
              $array[$key] = array_shift($parts);
           }
           else
           {
              user_error("Odd number of elements in string", E_USER_WARNING);
           }
        }
        echo "<pre>";
        print_r($array);
        echo "</pre>\n";
        ?>
        

          Hi heres an example of my code, it gives me the user error, and im not sure why. Cold you point it out for me?
          Thanks very much for your reply.

          
          if ($_SESSION['section'] == "anytime41def39030a65"){ $exploded = explode('|*|', $bsel_f['SEC01']); }
          
             $large = array();
              while(count($exploded)){
                      $key = array_shift($exploded);
                      if (count($exploded)){
                              $large[$key] = array_shift($exploded);
                      }else{
                              user_error("Wrong", E_USER_WARNING);
                      }
              }
              print_r($exploded);
          

            My guess is that it is not finding the characters "|*|" within the string in question, so the result of the explode is a 1-element array with that element's value being the entire string.

              Ok well i sorted it now, heres the code i did. Pretty much the same thing.
              And i wanted a quick solution 😉

              $ii = count($exploded);
                      while($i < $ii){
                              $key = array_shift($exploded);
                                       $large[$key] = array_shift($exploded);
              
                                  user_error("Wrong", E_USER_WARNING);
              
                          $i++;
                  }
              
              
              
              

              Thanks very much for your help.

                🙂

                $egstr="q:a:b:f:t:y"; 
                $egarr=explode(':', $egstr);
                
                $keyarr=array();
                $valuearr=array();
                for($i=0;$i<count($egarr);$i++){
                		if($i%2==0){
                			$keyarr[]= $egarr[$i];
                		}
                		else{
                			$valuearr[]=$egarr[$i];
                		}
                	}
                
                $f=array_combine($keyarr, $valuearr);
                print_r($f);
                  Write a Reply...