Skip to content
Advertisement

I want new array from existing array has one of old array key and value [closed]

$main_array = array( [ “XS”, “S”, “M”, “L”, “XL” ], [ “12”, “34”, “56” ], [ “Series 6/SE-44 MM”, “Series 7-45 MM” ]

);

Want result as below:

$result = array( array( “pa_clothes”=> “xs”, “pa_apple_watch”=> “44-mm”,
“test”=> “12” ), array( “pa_clothes”=> “xs”, “pa_apple_watch”=> “44-mm”,
“test”=> “34” ), array( “pa_clothes”=> “xs”, “pa_apple_watch”=> “44-mm”,
“test”=> “56” ), array( “pa_clothes”=> “xs”, “pa_apple_watch”=> “45-mm”,
“test”=> “12” ), array( “pa_clothes”=> “xs”, “pa_apple_watch”=> “45-mm”,
“test”=> “34” ), array( “pa_clothes”=> “xs”, “pa_apple_watch”=> “45-mm”,
“test”=> “56” ), array( “pa_clothes”=> “s”, “pa_apple_watch”=> “44-mm”,
“test”=> “12” ), array( “pa_clothes”=> “s”, “pa_apple_watch”=> “44-mm”,
“test”=> “34” ), array( “pa_clothes”=> “s”, “pa_apple_watch”=> “44-mm”,
“test”=> “56” ), array( “pa_clothes”=> “s”, “pa_apple_watch”=> “45-mm”,
“test”=> “12” ), array( “pa_clothes”=> “s”, “pa_apple_watch”=> “45-mm”,
“test”=> “34” ), array( “pa_clothes”=> “s”, “pa_apple_watch”=> “45-mm”,
“test”=> “56” ), array( “pa_clothes”=> “m”, “pa_apple_watch”=> “44-mm”,
“test”=> “12” ), array( “pa_clothes”=> “m”, “pa_apple_watch”=> “44-mm”,
“test”=> “34” ), array( “pa_clothes”=> “m”, “pa_apple_watch”=> “44-mm”,
“test”=> “56” ), array( “pa_clothes”=> “m”, “pa_apple_watch”=> “45-mm”,
“test”=> “12” ), array( “pa_clothes”=> “m”, “pa_apple_watch”=> “45-mm”,
“test”=> “34” ), array( “pa_clothes”=> “m”, “pa_apple_watch”=> “45-mm”,
“test”=> “56” ), array( “pa_clothes”=> “l”, “pa_apple_watch”=> “44-mm”,
“test”=> “12” ), array( “pa_clothes”=> “l”, “pa_apple_watch”=> “44-mm”,
“test”=> “34” ), array( “pa_clothes”=> “l”, “pa_apple_watch”=> “44-mm”,
“test”=> “56” ), array( “pa_clothes”=> “l”, “pa_apple_watch”=> “45-mm”,
“test”=> “12” ), array( “pa_clothes”=> “l”, “pa_apple_watch”=> “45-mm”,
“test”=> “34” ), array( “pa_clothes”=> “l”, “pa_apple_watch”=> “45-mm”,
“test”=> “56” ), array( “pa_clothes”=> “xl”, “pa_apple_watch”=> “44-mm”,
“test”=> “12” ), array( “pa_clothes”=> “xl”, “pa_apple_watch”=> “44-mm”,
“test”=> “34” ), array( “pa_clothes”=> “xl”, “pa_apple_watch”=> “44-mm”,
“test”=> “56” ), array( “pa_clothes”=> “xl”, “pa_apple_watch”=> “45-mm”,
“test”=> “12” ), array( “pa_clothes”=> “xl”, “pa_apple_watch”=> “45-mm”,
“test”=> “34” ), array( “pa_clothes”=> “xl”, “pa_apple_watch”=> “45-mm”,
“test”=> “56” ) );

Advertisement

Answer

As per my understanding you required

$array1 = [
    "XS",
    "S",
    "M",
    "L",
    "XL"
];
$array2 = [
    "12",
    "34",
    "56"
];
$array3 = [
    "Series 6/SE-44 MM",
    "Series 7-45 MM"
];

$myArray = [];
for ($i = 0; $i < count($array1); $i++) {
    for ($j = 0; $j < count($array2); $j++) {
        for ($k = 0; $k < count($array3); $k++) {
            $myArray[] = [
                'pa_clothes' => $array1[$i],
                'test' => $array2[$j],
                'pa_apple_watch' => $array3[$k]
            ];
        }
    }
}

echo '<pre>';
print_r($myArray);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement