0

I'm having quite hard time to figure out how can i convert this into single array based on cakephp. I'm new in cakephp and still learning. I hope you guys could help me.

'Sizes' => array(
            'girls' => ['7-8', '8', '9-10', '10', '11-12', '12', '13-14'],
            'bras' => ['32A', '32B', '32C', '32D', '34A', '34B', '34C', '34D', '34DD', '36A', '36B', '36C', '36D', '36DD', '38A', '38B', '38C', '38D', '38DD', '40A', '40B', '40C', '40D', '40DD'],
            'size' => ['32', '34', '36', '38', '40', '42', '44', '46', 'XS', 'S', 'M', 'L', 'XL', 'XXL', '2XL', 'XXXL'],
        )
2
  • 1
    what is your expected outcome? As well as what you have tried so far? Add both in your question Commented Feb 5, 2020 at 5:09
  • Show us what you expect and then we may provide you solution, otherwise not! Commented Feb 5, 2020 at 5:24

3 Answers 3

1
$new_array = [];

foreach($Sizes as $v){

    $new_array += $v;

}
echo '<pre>';
print_r($new_array);
Sign up to request clarification or add additional context in comments.

1 Comment

Please comment your code and/or offer details and an explanation of how this solves the posted question.
0

You could that using array_merge function. But since PHP version 7.4 you can do that using spread operator easily. But you may use loop to do that too. See the following examples:

<?php

$a = array(
        'Sizes' => array(
            'girls' => ['7-8', '8', '9-10', '10', '11-12', '12', '13-14'],
            'bras' => ['32A', '32B', '32C', '32D', '34A', '34B', '34C', '34D', '34DD', '36A', '36B', '36C', '36D', '36DD', '38A', '38B', '38C', '38D', '38DD', '40A', '40B', '40C', '40D', '40DD'],
            'size' => ['32', '34', '36', '38', '40', '42', '44', '46', 'XS', 'S', 'M', 'L', 'XL', 'XXL', '2XL', 'XXXL'],
        )
    );

// Method 1: (since 7.4)    
$b = [...$a['Sizes']['girls'], ...$a['Sizes']['bras'], ...$a['Sizes']['size']];

var_dump($b);

// Method 2:
$c = array_merge($a['Sizes']['girls'], $a['Sizes']['bras'], $a['Sizes']['size']);

var_dump($c);

// Method 3:
$d = $a['Sizes']['girls'] + $a['Sizes']['bras'] + $a['Sizes']['size'];

var_dump($d);

Note:

  • The + operator makes union of the two arrays.
  • The array_merge function makes union but the duplicate keys are overwritten.

Comments

0

I just did it like this and it work

'Sizes' => array(
            'size' => [
                '7-8', '8', '9-10', '10', '11-12', '12', '13-14',
                '32A', '32B', '32C', '32D', '34A', '34B', '34C', '34D', '34DD', '36A', '36B', '36C', '36D', '36DD', '38A', '38B', '38C', '38D', '38DD', '40A', '40B', '40C', '40D', '40DD', 
                '32', '34', '36', '38', '40', '42', '44', '46', 'XS', 'S', 'M', 'L', 'XL', 'XXL', '2XL', 'XXXL'
            ],
        )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.