0

I want to create a multicolumn array from a string where the spearator for the first level array is ";" and for the second level array is ","

This string is given:

$string = ("de,text1,text2,text3;en,text1,text2,text3;pl,text1,text2,text3")

This is the structure of the array that i want as result:

Array
(
    array([de] => text1, text2, text3),
    array([en] => text1, text2, text3),
    array([pl] => text1, text2, text3),
)
1
  • 2
    So what have you tried so far? What are you specific issues with your code? Please understand that we are here to help, we are not a free coding service. Commented Jun 2, 2016 at 11:24

4 Answers 4

1

The solution using array_map, substr, strpos and explode functions:

$string = ("de,text1,text2,text3;en,text1,text2,text3;pl,text1,text2,text3");

$items = array_map(function($v){
    $sep_pos = strpos($v, ",");  // position of key/values separator
    return [substr($v, 0, $sep_pos) => substr($v, $sep_pos + 1)];
}, explode(";", $string));

print_r($items);

The output:

Array
(
    [0] => Array
        (
            [de] => text1,text2,text3
        )

    [1] => Array
        (
            [en] => text1,text2,text3
        )

    [2] => Array
        (
            [pl] => text1,text2,text3
        )
)
Sign up to request clarification or add additional context in comments.

Comments

1

You can use array_map and pass array by using explode, and inside callback you can explode again with , and store it in a variable.

there after you can grab the value using array_shift, which needed to be used as a key, and return a new array.

$string = "de,text1,text2,text3;en,text1,text2,text3;pl,text1,text2,text3";
$result = array_map(function($value) {
    $temp = explode(',', $value);
    return [array_shift($temp) => implode(',', $temp)];
}, explode(';', $string));
print_r($result);

Example: https://eval.in/581893

1 Comment

plus 1 for fixed solution )
0

Try:

$string = "de,text1,text2,text3;en,text1,text2,text3;pl,text1,text2,text3";
$mainArr = explode(";",$string);
$finalArr = array();
foreach($mainArr as $arr) {
   $tempArr = explode(",",$arr);
   reset($tempArr);
   $mainK = $tempArr[0];
   unset($tempArr[key($tempArr)]);
   $finalArr[][$mainK] = implode(",", $tempArr);
}

print '<pre>';print_r($finalArr);print '</pre>';

Output:

Array
(
    [0] => Array
        (
            [de] => text1,text2,text3
        )

    [1] => Array
        (
            [en] => text1,text2,text3
        )

    [2] => Array
        (
            [pl] => text1,text2,text3
        )

)

Comments

0

Try This Code

$string = "de,text1,text2,text3;en,text1,text2,text3;pl,text1,text2,text3";
$array1 = explode(';', $string);
foreach($array1 as $value){
$temp=explode(',', $value);
$key=$temp[0];
unset($temp[0]);
$new_array[][$key]=$temp;
}

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.