0

Using the following arrays as input:

  • style numbers: Array ( [0] => 1001 [1] => 1002 [2] => 1003 )

  • colors: Array ( [0] => 'red' [1] => 'red' [2] => 'red' )

  • small size: Array ( [0] => 1 [1] => 1 [2] => 1 )

  • large size: Array ( [0] => 2 [1] => 2 [2] => 2 )

I need to create an array like this:

 array(
    '1001'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    ),
    '1002'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    ),
    '1003'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    )
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136

5 Answers5

2

Do it like below (very simple):-

$final_array = array();

foreach($style as $key=>$val){
  $final_array[$val]['color'] = $colors[$key];
  $final_array[$val]['small_size'] = $small_size[$key];
  $final_array[$val]['large_size'] = $large_size[$key];
}

echo "<pre/>";print_r($final_array);

Output:- https://eval.in/776784

A Better approach to do it:- https://eval.in/776787

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • @mickmackusa what typo? in your example link everything perfectly fine. – Alive to die - Anant Apr 15 '17 at 08:31
  • @mickmackusa it's not a typo. i just take that name. it's ridiculous . I was thinking that my logic is wrong or some error is coming in my code. – Alive to die - Anant Apr 15 '17 at 09:03
  • @mickmackusa done. check now. I am also not fighting with you. :):):) – Alive to die - Anant Apr 15 '17 at 09:43
  • I edited this question a while ago (while it was "on hold") and now that it has gone past the holding time period, I would like to ask you to re-open the question. The question is clear now. I assume you have the super-powers necessary to reopen this question. If you feel it shouldn't be re-opened please leave me a comment explaining why. – mickmackusa Apr 22 '17 at 09:33
1

You can simply achieve this using a for loop:

$arrayNumbers = array(1001, 1002, 1003);
$arrayColours = array('red', 'red', 'red');
$arraySmallSizes = array(1, 1, 1);
$arrayLargeSizes = array(2, 2, 2);

$output = array();

$count = count($arrayNumbers);
for($i = 0; $i < $count; $i++) {
    $output[$arrayNumbers[$i]] = array('color' => $arrayColours[$i], 'small_size' => $arraySmallSizes[$i],'large_size' => $arrayLargeSizes[$i]);
}

print_r($output);
// Array (
    [1001] => Array ( [color] => red [small_size] => 1 [large_size] => 2 )  
    [1002] => Array ( [color] => red [small_size] => 1 [large_size] => 2 ) 
    [1003] => Array ( [color] => red [small_size] => 1 [large_size] => 2 )
)

(See https://3v4l.org/9Wpj9 for the performance)
Or using a foreach loop:

$arrayNumbers = array(1001, 1002, 1003);
$arrayColours = array('red', 'red', 'red');
$arraySmallSizes = array(1, 1, 1);
$arrayLargeSizes = array(2, 2, 2);

$output = array();
foreach($arrayNumbers as $key => $value) {
    $output[$arrayNumbers[$key]] = array('color' => $arrayColours[$key], 'small_size' => $arraySmallSizes[$key],'large_size' => $arrayLargeSizes[$key]);
}

print_r($output);

(See https://3v4l.org/ifv1o for the performance)

Tom Udding
  • 2,264
  • 3
  • 20
  • 30
0

Hello You can try this code i have tested it-

$array1 = array( 0 => 1001  , 1 => 1002 ,2 => 1003 );
$array2 = array( 0 => 'red' ,1 => 'red' ,2 => 'red' );
$array3 = array( 0 => 1 ,1 => 1 ,2 => 1 );
$array4 = array( 0 => 2, 1 => 2 ,2 => 2 );
$final_array = [];
foreach($array1 as $key =>$value){
    $final_array[$value]['color'] =   $array2[$key];
    $final_array[$value]['small_size'] =   $array3[$key];
    $final_array[$value]['large_size'] =   $array4[$key];
}
print_r($final_array);
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Gohel Dhaval
  • 820
  • 1
  • 8
  • 12
  • Using a `foreach()` method was posted by AlivetoDie 6 minutes before your answer was posted. All duplicate answers should be removed because they do not help SO readers and only cause page bloat. Please practice good SO citizenship and delete this answer. – mickmackusa Apr 17 '17 at 12:28
-1

that code should resolve your problem:

$input = array(
    '1001'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    ),
    '1002'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    ),
    '1003'=>array(
        'color'=>'red',
        'small_size'=>1,
        'large_size'=>2,
    ),
);

$keys = array_keys($input);

$color = array_column($input, 'color');
$smallSize = array_column($input, 'small_size');
$largeSize = array_column($input, 'large_size');
VirCom
  • 3,414
  • 1
  • 10
  • 10
  • You are confused by this poorly worded question. You are using output as input and providing the input as output. – mickmackusa Apr 15 '17 at 04:59
-1

You can use array_column

 $data = array(
    '1001'=>array(
        'color'=>'red',
        small_size=>1,
        large_size=>2,
    ),
    '1002'=>array(
        'color'=>'red',
        small_size=>1,
        large_size=>2,
    ),
    '1003'=>array(
        'color'=>'red',
        small_size=>1,
        large_size=>2,
    ),
);

var_dump(array_column($data, 'color'));

var_dump(array_column($data, 'small_size'));

Output:

array(3) { [0]=> string(3) "red" [1]=> string(3) "red" [2]=> string(3) "red" } 

array(3) { [0]=> int(1) [1]=> int(1) [2]=> int(1) } 
Priyank
  • 1,180
  • 1
  • 15
  • 26
  • You are confused by this poorly worded question. You are using output as input and providing the input as output. – mickmackusa Apr 15 '17 at 05:00
  • What I understood in oq is he is having multi dimension array and he needs selected keys outupt (color, small_size, large_size) in single array. – Priyank Apr 17 '17 at 05:09