简体   繁体   中英

Convert string to associative array split on multiple characters

I have a string in the following possible format:

"John|Smith,Bob,Bill|Smith " etc.

Therefore these are comma separated pairs which are themselves split by a |

Each section (deliminated by comma) can have either 1 or 2 parts which are split by a |.

I would like the best way to convert this to an associative array such as:

array(
   array(
       'firstname' => "Bill"
       'lastname' => "Smith"
   ),
   array(
       'firstname' => "Bob"
   ),
   array(
       'firstname' => "Bill"
       'lastname' => "Smith"
   ),
)

Try this :

$test = "John|Smith,Bob,Bill|Smith";

$i=0;
$res = array();
foreach(explode(",",$test) as $val){
    $vl   = explode("|",$val);
    $res[$i]['firstname']  = $vl[0];
    if(isset($vl[1])){
       $res[$i]['lastname']  = $vl[0];
    }
    $i++;
}

echo "<pre>";
print_r($res);

Output :

Array
(
    [0] => Array
        (
            [firstname] => John
            [lastname] => John
        )

    [1] => Array
        (
            [firstname] => Bob
        )

    [2] => Array
        (
            [firstname] => Bill
            [lastname] => Bill
        )

)

See here: http://codepad.org/bQMTkWP9

<?php

$sInput = "John|Smith,Bob,Bill|Smith";

$aFirstExpl = explode(",",$sInput);
$aOutcome = array();
$i = 0;
foreach($aFirstExpl as $sPair){
    $aSecExplode = explode("|",$sPair);
    $aOutcome[$i]['first'] = $aSecExplode[0];
    if(isset( $aSecExplode[1])){$aOutcome[$i]['last'] =  $aSecExplode[1];}
$i++;
}
print_r($aOutcome);

?>

Note that this assumes there always is at least a first name.

$result = array();
$input = "John|Smith,Bob,Bill|Smith";

foreach (explode(',', $input) as $data) {
    if (false !== strpos($data, '|')) {
        list($firstname, $lastname) = explode('|', $data, 2);
        $result[] = array(
            'firstname' => $firstname,
            'lastname' => $lastname,
        );
    } else {
        $result[] = array(
            'firstname' => $data,
        );
    }
}

print_r($result);

Output

Array
(
    [0] => Array
        (
            [firstname] => John
            [lastname] => Smith
        )

    [1] => Array
        (
            [firstname] => Bob
        )

    [2] => Array
        (
            [firstname] => Bill
            [lastname] => Smith
        )

)

You could do something like this:

$values = "John|Smith,Bob,Bill|Smith";

$result = array_map(function($elem) {
    $parts = explode('|', $elem);

    $output = array('firstname' => $parts[0]);

    if (isset($parts[1]))
    {
        $output['lastname'] = $parts[1];
    }

    return $output;

}, explode(',', $values));

var_dump($result);

With the output being:

array
  0 => 
    array
      'firstname' => string 'John' (length=4)
      'lastname' => string 'Smith' (length=5)
  1 => 
    array
      'firstname' => string 'Bob' (length=3)
  2 => 
    array
      'firstname' => string 'Bill' (length=4)
      'lastname' => string 'Smith' (length=5)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM