0

I have an array which has an array within it, and I want to create a new array for each of the array values of 'power'. What is the best way to do this?

Original array:

Array
(
    [0] => Array
        (
            [date] => 2019-09-16 15:16:03
            [stid] => 64
            [beam] => 1
            [power] => {1,3,7,8,9}
            [gates] => 5
)

   [1] => Array
        (
            [date] => 2019-09-16 15:17:03
            [stid] => 64
            [beam] => 1
            [power] => {14,15,16}
            [gates] => 3
)
)

Edit: Output array should increment gate started from 1

Array
(
    [0] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 1
            [power] => 1
        )

   [1] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 2
            [power] => 3
         )

   [2] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 3
            [power] => 7
         )

   [3] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 4
            [power] => 8
         )


   [4] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 5
            [power] => 9
         )

   [5] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 1
            [power] => 14
         )

   [6] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 2
            [power] => 15
         )

   [7] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 3
            [power] => 16
         )
)

Continued for all 8 (5 and 3) power values, such that the resultant array is an array of 8 arrays.

2 Answers 2

1
# Original Data Structure
$arr = 
[
    [
        'date' => '2019-09-16 15:16:03',
        'stid' => 64,
        'beam' => 1,
        'power' => '{1,3,7,8,9}',
        'gates' => 5
    ],
    [
        'date' => '2019-09-16 15:17:03',
        'stid' => 64,
        'beam' => 1,
        'power' => '{14,15,16}',
        'gates' => 3
    ]
];

$output = [];
foreach ($arr as $a)
{
    $gateCount = 0;
    foreach (explode(',', trim($a['power'], '{}')) as $p) 
    {
        $output[] = [
            'date' => $a['date'],
            'gate' => $gateCount++ % $a['gates'] + 1,
            'power' => $p
        ];
    }
}
print_r($output);

Output:

Array
(
    [0] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 1
            [power] => 1
        )

    [1] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 2
            [power] => 3
        )

    [2] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 3
            [power] => 7
        )

    [3] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 4
            [power] => 8
        )

    [4] => Array
        (
            [date] => 2019-09-16 15:16:03
            [gate] => 5
            [power] => 9
        )

    [5] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 1
            [power] => 14
        )

    [6] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 2
            [power] => 15
        )

    [7] => Array
        (
            [date] => 2019-09-16 15:17:03
            [gate] => 3
            [power] => 16
        )

)

Note:

['beam'] and ['stid'] have been left off as per your sample. Can easily be added if you need them.

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

16 Comments

This is true. In my sample recreation of the data structure, I simply made it an array,
I modified my answer to have the structure use the {x,y} format and the foreach accordingly.
Also, gate should equal 0,1,2,3,4,5,0,1,2 in this example. How would I make that addition?
@mcook16, edit your original question to specify how the gate index should be populated.
@mcook16 How do you want your 'gate' defined?
|
0

You can do this:

$newArray = [];
foreach ($oldArray as $element) {
   foreach ($element['power'] as $power) {
      $element['power'] = $power;
      $newArray[] = $element;
   }
}

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.