6

I have an array:

Array
(
    [12] => USD
    [13] => 10150.00
    [14] => 9850.00
    [15] => SGD
    [16] => 8015.40
    [17] => 7915.40
    [18] => HKD
    [19] => 1304.60
    [20] => 1288.60
    ...
)

What I want to do is arrange it to be like this:

Array
(
    [USD] => Array
             (
                 [Buy] => 10150.00
                 [Sell] => 9850.00
             )
    [SGD] => Array
             (
                 [Buy] => 8015.40
                 [Sell] => 7915.40
             )
    [HKD] => Array
             (
                 [Buy] => 1304.60
                 [Sell] => 1288.60
             )
    ...
)

I've done a lot of array functions but still stuck with this.

2
  • 1
    You need to iterate over it and create desired structure, there is no ready function for that. Commented Jul 13, 2013 at 16:48
  • @frozenade, your question being answered, may you please mark one answer as accepted? Commented Aug 12, 2014 at 14:08

3 Answers 3

5

If the suite of fields remains the same as:

  1. Currency
  2. Buy value
  3. Sell value

then, you can do:

$old_array = array('USD', 123.00, 432.34, 'SGD', 421.41, 111.11);
$new_array = array();

for ($i = 0; $i < count($old_array); $i = $i + 3) {
    $new_array[$old_array[$i]] = array
    (
        'Buy' => $old_array[$i + 1],
        'Sell' => $old_array[$i + 2]
    );
}
Sign up to request clarification or add additional context in comments.

1 Comment

You probably need to do $i_currency = $old_array[$i] tho, and then just use $i on the buy/sell indexes.
2

Demo


$data = array
(
    'USD',
    '10150.00',
    '9850.00',
    'SGD',
    '8015.40',
    '7915.40',
    'HKD',
    '1304.60',
    '1288.60',
);

$result = array();

while (is_null($value = array_shift($data)) !== true)
{
    if (preg_match('~^[A-Z]{3}$~', $value) > 0)
    {
        $result[$value] = array
        (
            'Buy' => array_shift($data),
            'Sell' => array_shift($data),
        );
    }
}

print_r($result); // transformed array

1 Comment

That's amazing. +1 for all. Thank you very much for Stéphane Bruckert and Alix Axel. Now, I just convert it to xml service.
0

Since the structural pattern of your input array is reliable, I can provide a functional approach (no for/while loops, no new variables in the global space).

Code: (Demo)

var_export(
    array_reduce(
        array_chunk($input, 3),
        function($carry, $item) {
            $carry[$item[0]] = ["Buy" => $item[1], "Sell" => $item[2]];
            return $carry;
        },
        []
    )
);

Output:

array (
  'USD' => 
  array (
    'Buy' => '10150.00',
    'Sell' => '9850.00',
  ),
  'SGD' => 
  array (
    'Buy' => '8015.40',
    'Sell' => '7915.40',
  ),
  'HKD' => 
  array (
    'Buy' => '1304.60',
    'Sell' => '1288.60',
  ),
)

array_chunk() creates an array of 3 consecutive elements from your input array.
array_reduce() is used to form a new, associative output array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.