0

I have a String in this format

{value=very big +$5},{value=super big +$10},{value=extra big +$15}

How to convert them into 2 Arrays in such format?

For example:

$name=["very big","super big","extra big"];
$price=["5","10","15"];   // OR  $price=[5,10,15];

I can do that by using explode() if the string format is in a simpler format. However, this format is too complicated. Anyone knows how to do that ?

3
  • 1
    Explode by ,, then explode the exploded items. Commented Oct 15, 2016 at 11:09
  • @u_mulder ok. after Explode by , and remove {} . How about the remaining "value=very big +$5" ? Commented Oct 15, 2016 at 11:11
  • 1
    Explode by =? Explode by $? Explode by +$? Commented Oct 15, 2016 at 11:12

3 Answers 3

1

use explode with ',' ,'=' ,'+$'

$string = "{value=very big +$5},{value=super big +$10},{value=extra big +$15}";
$temp_array = (explode(",",$string));
foreach($temp_array as $val)    
{
    $temp_array = (explode("=",$val));  
    $temp_string = $temp_array[1]; 
    $temp_string = str_replace("}","",$temp_string);
    $temp_array = (explode("+$",$temp_string));
    $name[] = $temp_array[0];
    $price[] = $temp_array[1];
}

DEMO

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

Comments

1

You may try it like the Snippet below suggests. Quick-Test: Here.

<?php

    $string     = '{value=very big +$5},{value=super big +$10},{value=extra big +$15}';
    $arrParts   = explode(',', $string);
    $name       = [];
    $price      = [];
    $result     = [];

    foreach($arrParts as $iKey=>$part){
        $block      = preg_replace(["#^\{.*?=#", "#\}$#"], "", $part);
        $segments   = preg_split("#\s#", $block);   //<== CREATE AN ARRAY WITH 3 COLUMNS

        list($val1, $val2, $val3)   = $segments;
        $namePart                   = $val1 . " {$val2}";
        $pricePart                  = preg_replace("#[\+\-\$]#", "",$val3);
        $name[]                     = $namePart;
        $price[]                    = $pricePart;

        // BONUS: JUST CREATE A 3RD ARRAY WITH NAMED-KEY
        $result[$namePart]          = $pricePart;
    }
    var_dump($name);
    //YIELDS::
    array (size=3)
      0 => string 'very big' (length=8)
      1 => string 'super big' (length=9)
      2 => string 'extra big' (length=9)

    var_dump($price);
    //YIELDS::
    array (size=3)
      0 => string '5' (length=1)
      1 => string '10' (length=2)
      2 => string '15' (length=2)

    var_dump($result);
    //YIELDS::
    array (size=3)
      'very big' => string '5' (length=1)
      'super big' => string '10' (length=2)
      'extra big' => string '15' (length=2)

Comments

0

You can use regex, for example this should work:

preg_match_all('/value=([\w\s]+)\s\+/', $string, $matches)
preg_match_all('/\$([\d]+)\}/', $string, $matches)

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.