Home > Software design >  I want new array from existing array has one of old array key and value
I want new array from existing array has one of old array key and value

Time:11-27

$array1 = [ "XS", "S", "M", "L", "XL" ]; $array2 = [ "12", "34", "56" ] $array3 = [ "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" ) );

CodePudding user response:

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);

CodePudding user response:

assume that each $array1 $array2 $array3 count are same.

$newArray = [];
for($i=0;$i<count($array1);$i  ){
    $newArray[] = [
         'pa_clothes' => $array1[$i],
         'test' => $array2[$i],
         'pa_apple_watch' => $array3[$i],
    ];
}
echo '<pre>';
print_r($newArray);
echo '</pre>';
  • Related