Skip to content
Advertisement

Parse diverse array of numbers for value and number

I am using PHP 7.3.5 and I have the following set of array values:

$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];

Basically I want for each of these values the number and the “type”, so if it is a percentage then I would like to get -4.2 and percentage.

I tried to create a minimum example (however the below code is no real good example ;( ), but I am stuck at the data structure level as some array keys have two inputs, such as '0.74|2.6':

<?php


$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];

$resArr = array();
$structureArr = array(
    'value1' => "",
    'number1' => "",
    'value2' => "",
    'number2' => ""
);
foreach ($valueArr as $key => $v) {
    if (1 === preg_match('/%/', $valueArr[$key])) {
        preg_match('!d+.*d*!', $valueArr[$key], $structureArr['number1']);
        $structureArr['value1'] = 'percentage';
    }
    /*
    if (1 === preg_match('|', $valueArr[$key])) {
        $str = explode("|", $valueArr[$key]);
        $value1 = 'number';
        $number1 = $str[0];
        $value2 = 'number';
        $number2 = $str[1];
    }
    if (1 === preg_match('', $valueArr[$key])) {

    }
    */
    array_push($resArr, $structureArr);
}

print_r($resArr);
/*
Wanted Result
Array
(
    [0] => Array
        (
            [0] => -4.2
            [1] => 'percentage'
        )

    [1] => Array
        (
            [0] => 51.0
            [1] => 'number'
        )

    [2] => Array
        (
            [0] => 90000
            [1] => number
        )

    [3] => Array
        (
            [0] => 0.5
            [1] => percentage
        )

    [4] => Array
        (
            [0] => 0.74
            [1] => number
            [2] => 2.6
            [3] => number
        )

   ...
*/

I would highly appreciate your input on how to structure this array input.

Appreciate your replies!

Advertisement

Answer

This expands on my comment. Not sure if it’s the most optimal solution or not.

Rough outline…

Create array mapping suffix to multiplier. Loop through source array. explode on |. Loop through result. If last character is %, strip it, value=value and type=percentage, else, strip last char, use it as array index (if it is an available index), value=value*multiplier and type=number.

$resArr = array();

$multipliers = array("K" => 1000, "M" => 1000000, "B" => 1000000000, "T" => 1000000000000);

$valueArr = ['-4.2%', '51.0', '90K', '0.5%', '0.74|2.6', '-1.2B', '779B', '215K', '92.2%', '42.8B', '1.49T', '1690B', '-10.8B', '0.38|3.9', '102.4', '1.00%', '0.07|1.3'];

foreach($valueArr as $index => $value)
{
    $parts = explode("|", $value);

    $resArr[$index] = array();

    foreach($parts as $part)
    {
        $lastChar = substr($part, -1);

        if($lastChar == "%")
        {
            $resArr[$index][] = substr($part, 0, -1);
            $resArr[$index][] = "percentage";
        }
        else if(in_array($lastChar, array_keys($multipliers)))
        {
            $multiple = $multipliers[$lastChar];
            $resArr[$index][] = (substr($part, 0, -1))*$multiple;
            $resArr[$index][] = "number";
        }
        else
        {
            $resArr[$index][] = $part;
            $resArr[$index][] = "number";
        }
    }
}

var_dump($resArr);

DEMO

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement