Answer the question
In order to leave comments, you need to log in
How to parse a php array into pairs - key -> array element, value -> array / subarray element?
There is this array:
[ID] => 7204
[IBLOCK_SECTION_ID] => 142
[PROPERTY_VIRTUAL_SECTIONS_VALUE] => Array
(
[0] => 296
[1] => 433
[2] => 434
)
[7204] => Array
(
[0] => 142
[1] => 296
[2] => 433
[3] => 434
)
[0] => 142
[1] => 296
[2] => 433
[3] => 434
Answer the question
In order to leave comments, you need to log in
Seriously? Question about how to work with arrays in PHP? Have you tried to solve it yourself?
<?php
// Test data
$in = [
'ID' => 7204,
'IBLOCK_SECTION_ID' => 142,
'PROPERTY_VIRTUAL_SECTIONS_VALUE' => [
296,
433,
434,
],
];
$test = [
7204 => [
142,
296,
433,
434,
],
];
// Working function
//
function flattenConcreteArray (array $arr = []) : array {
$id = $arr['ID'];
$result = [$arr['IBLOCK_SECTION_ID']];
$result = array_merge($result, $arr['PROPERTY_VIRTUAL_SECTIONS_VALUE']);
return [$id => $result];
};
// Testing
$out = flattenConcreteArray($in);
assert($out === $test);
echo 'It Works!' . PHP_EOL;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question