Question

I have this unidimensional array:

$array1:

Array
(
    [coupon_code] => GTY777R
    [coupon_description] => Credito $5 USD
)

$array2: (2D Array)

Array
(
    [0] => Array
        (
            [coupon_code] => 0000000
            [coupon_description] => Intenta de nuevo
        )

    [1] => Array
        (
            [coupon_code] => 0000000
            [coupon_description] => Intenta de nuevo
        )

)

I need to check if $array1 is a Unidimentional Array and convert it before join, for example:

if (is_1D($array1) = TRUE) {
  $array1 = convert_2D($array1);
}
$array3 = join_arrays($array1, $array2);

Final Result $array1 converted to 2D and joined: $array3

Array
(
    [0] => Array
        (
            [coupon_code] => 0000000
            [coupon_description] => Intenta de nuevo
        )

    [1] => Array
        (
            [coupon_code] => 0000000
            [coupon_description] => Intenta de nuevo
        )

    [2] => Array
        (
            [coupon_code] => GTY777R
            [coupon_description] => Credito $5 USD
        )

)
Was it helpful?

Solution

Try this

$array1 = array(
    'coupon_code' => 'GTY777R',
    'coupon_description' => 'Credito $5 USD',
);

$array2 = array(
    array(
        'coupon_code' => '0000000',
        'coupon_description' => 'Intenta de nuevo',
    ),
    array(
        'coupon_code' => '0000000',
        'coupon_description' => 'Intenta de nuevo',
    ),
);

$result = array();

# is 1D array
if (count($array1) == count($array1, COUNT_RECURSIVE))  {
    $result[] = $array1;    
}

# join it
$result = array_merge($result, $array2);

Results

Array
(
    [0] => Array
        (
            [coupon_code] => GTY777R
            [coupon_description] => Credito $5 USD
        )

    [1] => Array
        (
            [coupon_code] => 0000000
            [coupon_description] => Intenta de nuevo
        )

    [2] => Array
        (
            [coupon_code] => 0000000
            [coupon_description] => Intenta de nuevo
        )

)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top