0

I have a string:

01;Tommy;32;Coder&&02;Annie;20;Seller

I want it like this:

array (size=2)
0 => 
array (size=4)

  0 => string '01' (length=2)

  1 => string 'Tommy' (length=5)

  2 => int 42

  3 => string 'Coder' (length=5)  

1 => 
array (size=4)

  0 => string '02' (length=2)

  1 => string 'Annie' (length=5)

  2 => int 20

  3 => string 'Seller' (length=6)

4 Answers 4

5

Not sure if the datatypes will be matching (as I believe it's all in a string) but here's the code

 $myarray = array();
 foreach(explode("&&",$mystring) as $key=>$val)
 {
 $myarray[] = explode(";",$val);
 }

The explode command takes a string and turns it into an array based on a certain 'split key' which is && in your case
but since this is a dual array, I had to pass it through a foreach and another explode to solve.

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

2 Comments

what u calling explode() in foreach u need to put out of loop and just use variable in foreach
You don't need to explode out of the loop unless you are doing a large set. It's slower sure, but 0.0003 vs 0.0004 seconds (outside vs inline) on a 2 element array isn't really doing any harm. stackoverflow.com/questions/5861826/…
3

It's very simple. First you need to explode the string by && and then traverse through array exploded by &&. And explode each element of an array by ;.

Like this,

<?php

$str="01;Tommy;32;Coder&&02;Annie;20;Seller";

$array=explode("&&",$str);

foreach($array as $key=>$val){ 
    $array[$key]=explode(";",$val);
}
print_r($array);

Demo: https://eval.in/629507

Comments

3

you should just have to split on '&&', then split the results by ';' to create your new two dimensional array:

// $string = '01;Tommy;32;Coder&&02;Annie;20;Seller';

// declare output
$output = [];

// create array of item strings
$itemarray = explode('&&',$string);
// loop through item strings
foreach($itemarray as $itemstring) {
  // create array of item values
  $subarray = explode(';',$itemstring);
  // cast age to int
  $subarray[2] = (int) $subarray[2]; // only useful for validation
  // push subarray onto output array
  $output[] = $subarray;
}

// $output = [['01','Tommy',32,'Coder'],['02','Annie',20,'Seller']];

keep in mind that since php variables are not typed, casting of strings to ints or keeping ints as strings will only last depending on how the values are used, however variable type casting can help validate data and keep the wrong kind of values out of your objects.

good luck!

Comments

0

There is another appropach of solving this problem. Here I used array_map() with anonymous function:

$string = '01;Tommy;32;Coder&&02;Annie;20;Seller';

$result = array_map(function($value){return explode(';',$value);}, explode('&&', $string));

Comments

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.