skip to Main Content

I have an array like this

on;tracking34567;1;ABC;2019-08-13;on;tracking123;1;BCD;2019-08-13;on;123456test;1;USA;2019-08-13;

How to split it like this

Array
(
    [0] => on
    [1] => tracking34567
    [2] => 1
    [3] => ABC
    [4] => 2019-08-13
)
Array
(
    [0] => on
    [1] => tracking123
    [2] => 1
    [3] => BCD
    [4] => 2019-08-13
)
Array
(
    [0] => on
    [1] => 123456test
    [2] => 1
    [3] => USA
    [4] => 2019-08-13
)

Please help me

UPDATE

I tryed but it show

public function indexAction()
      {
        $data = $this->getRequest()->getParam("values");
        $temp = explode(";",$string);
        $i = 0;
        $result = array();
        foreach($temp as $v){
            if($v == "on"){
                $i++;
            }
            $result[$i][] = $v;
        }
        print_r($result);exit;
          // var_dump($data);exit;
      }

enter image description here

UPDATE

It work fine, but if i need array like this, how to change it?

Array
(
    [a] => on
    [b] => tracking34567
    [c] => 1
    [d] => ABC
    [e] => 2019-08-13
)
Array
(
    [a] => on
    [b] => tracking123
    [c] => 1
    [d] => BCD
    [e] => 2019-08-13
)
Array
(
    [a] => on
    [b] => 123456test
    [c] => 1
    [d] => USA
    [e] => 2019-08-13
)

6

Answers


  1. You can use as below:

    $arrayStr = "on;tracking34567;1;ABC;2019-08-13;on;tracking123;1;BCD;2019-08-13;on;123456test;1;USA;2019-08-13";
    print_r (explode(";",$arrayStr));
    
    Login or Signup to reply.
  2. You first need to convert whole string in array

    <?php
    $string = 'on;tracking34567;1;ABC;2019-08-13;on;tracking123;1;BCD;2019-08-13;on;123456test;1;USA;2019-08-13;';
    $temp = explode(";",$string);
    

    Now use this temp array to create your new array

    $i = -1;
    $k = 0;
    $result = array();
    $alpha = range('a','z');
    foreach($temp as $v){
        if($v == "on"){
            $i++;
            $k=0;
        }
        $result[$i][$alpha[$k]] = $v;
        $k++;
    }
    

    You will have your result in $result

    Login or Signup to reply.
  3. Can you please try this it will help you.

        <?php
        $string = 'on;tracking34567;1;ABC;2019-08-13;on;tracking123;1;BCD;2019-08-13;on;123456test;1;USA;2019-08-13;';
    
        $arr = explode(';',$string);
    
        $result = array_chunk($arr, 5);
    
        echo "<pre>";
    
        print_r($result);
    
        ?>
    
    Login or Signup to reply.
  4. You can try this

    $string = 'on;tracking34567;1;ABC;2019-08-13;on;tracking123;1;BCD;2019-08-13;on;123456test;1;USA;2019-08-13;';
    $temp = explode(";",$string);
    $i = 0;
    $result = array();
    $j = 'a';
    foreach($temp as $v){
    
        if($v == "on"){
            $i++;
            $j = 'a';
        }else
        {
            $j++;
        }
        $result[$i][$j] = $v;
    }
    
    Login or Signup to reply.
  5. Try this

    <?php
    
    $string = "on;tracking34567;1;ABC;2019-08-13;on;tracking123;1;BCD;2019-08-13;on;123456test;1;USA;2019-08-13";
    $arr = explode("on",$string);
    
    for($i = 1; $i < count($arr); $i++ ){
        $arr[$i] = "on".$arr[$i];
        $arr[$i] = explode(";",$arr[$i]);
    }
    
    
    $arr = array_filter($arr)
    
    Login or Signup to reply.
  6. If values in your string always comes in the same format and there are always five element in the string, then you can do it in a much better and simpler way, rather then using loops.

    You can use php in-built functions and achieve it.
    Here have a look at the code.

    <?php
    $text = "on;tracking34567;1;ABC;2019-08-13;on;tracking123;1;BCD;2019-08-13;on;123456test;1;USA;2019-08-13;";
    $exploded_string = explode(';', $text);
    $final_array = array_chunk($exploded_string, 5);
    print_r($final_array);
    ?>
    

    Hope this helps.Thanks

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search