0

I have 3 arrays in a function which I want to return as it is seperated by comma. Tried array_merge, array_combine, array_merge_recursive as well as the + operator, but nothing worked.

Here is the function I have:

function svca_icon_fields() {

    $icon = array (
            'type' => 'dropdown',
            'heading' => __( 'Select Icon Type', 'svca-addon' ),
            'param_name' => 'icon_type',
            'description' => __( 'Select Icon Type', 'svca-addon' ),
            'value' => array(
                __( 'No Icon', 'svca-addon' ) => 'noicon',
                __( 'Font Awesome', 'svca-addon' ) => 'fontawesome',
                __( 'Open Iconic', 'svca-addon' ) => 'openiconic',
                __( 'Typicons', 'svca-addon' ) => 'typicons',
                __( 'Entypo', 'svca-addon' ) => 'entypo',
                __( 'Linecons', 'svca-addon' ) => 'linecons',
            ),
        );
    $fa = array(
            'type' => 'iconpicker',
            'value' => 'fa fa-star',
            'heading' => __( 'Icon', 'svca-addon' ),
            'param_name' => 'fa_icon',
            'description' => __( 'Pick an Icon to represent your package.', 'svca-addon' ),
            'settings' => array(
                'emptyIcon' => false,
                'type' => 'fontawesome',
                'iconsPerPage' => 200,
                ),
            'dependency' => array( 'element' => 'icon_type', 'value' => 'fontawesome' ),
        );
    $oi = array(
            'type' => 'iconpicker',
            'value' => 'vc-oi vc-oi-eye',
            'heading' => __( 'Icon', 'svca-addon' ),
            'param_name' => 'oi_icon',
            'description' => __( 'Pick an Icon to represent your package.', 'svca-addon' ),
            'settings' => array(
                'emptyIcon' => false,
                'type' => 'openiconic',
                'iconsPerPage' => 200,
            ),
            'dependency' => array( 'element' => 'icon_type', 'value' => 'openiconic' ),
        );

    $data = array_merge_recursive( $icon, $fa, $oi );
    //$data = array_merge( $icon, $fa, $oi );
    //$data = array_combine( $icon, $fa, $oi );
    //$data = $icon + $fa + $oi + $ti + $et + $li;
    //$data = $icon . $fa . $oi . $ti . $et . $li;

    //return $data;

    echo '<pre>';
    print_r( $data );
    echo '</pre>';      
}

And, here is what I want to return:

array (
        'type' => 'dropdown',
        'heading' => __( 'Select Icon Type', 'svca-addon' ),
        'param_name' => 'icon_type',
        'description' => __( 'Select Icon Type', 'svca-addon' ),
        'value' => array(
            __( 'No Icon', 'svca-addon' ) => 'noicon',
            __( 'Font Awesome', 'svca-addon' ) => 'fontawesome',
            __( 'Open Iconic', 'svca-addon' ) => 'openiconic',
            __( 'Typicons', 'svca-addon' ) => 'typicons',
            __( 'Entypo', 'svca-addon' ) => 'entypo',
            __( 'Linecons', 'svca-addon' ) => 'linecons',
        ),
), //Seperated By Comma
array(
        'type' => 'iconpicker',
        'value' => 'fa fa-star',
        'heading' => __( 'Icon', 'svca-addon' ),
        'param_name' => 'fa_icon',
        'description' => __( 'Pick an Icon to represent your package.', 'svca-addon' ),
        'settings' => array(
            'emptyIcon' => false,
            'type' => 'fontawesome',
            'iconsPerPage' => 200,
            ),
        'dependency' => array( 'element' => 'icon_type', 'value' => 'fontawesome' ),
), //Seperated By Comma
array(
        'type' => 'iconpicker',
        'value' => 'vc-oi vc-oi-eye',
        'heading' => __( 'Icon', 'svca-addon' ),
        'param_name' => 'oi_icon',
        'description' => __( 'Pick an Icon to represent your package.', 'svca-addon' ),
        'settings' => array(
            'emptyIcon' => false,
            'type' => 'openiconic',
            'iconsPerPage' => 200,
        ),
        'dependency' => array( 'element' => 'icon_type', 'value' => 'openiconic' ),
), //Seperated By Comma

Spent like last 2 hours figuring this out without success.
Thanks

Abhik
  • 664
  • 3
  • 22
  • 40
  • Possible duplicate of [Multiple returns from function](http://stackoverflow.com/questions/3451906/multiple-returns-from-function) – miken32 May 29 '16 at 04:03

5 Answers5

1

Return it like:

//your code above;
return array(
    'fa' => $fa,
    'oi' => $oi,
    'icon' => $icon
);

It's a bit easier to use indexes, so you don't need to remember the order. So, if

$a = svca_icon_fields();
$icon = $a['icon'];// instead of $a[2]; 
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
0

This should work as you want

return array($icon,$fa,$oi);
Bart
  • 1,268
  • 2
  • 12
  • 14
0

It's not possible to return three arrays. Try to return a multidimensional array with the content of the three arrays.

return array(your,arrays,here);
manniL
  • 7,157
  • 7
  • 46
  • 72
0

Functions and methods can return only one value, so you have to return:

$data = array( $icon, $fa, $oi );
$return $data;

However, if you want assign returned value to three arrays, you can do it in this way:

list( $icon, $fa, $oi ) = svca_icon_fields();

then you will have three different arrays (you can change $icon etc with your preferred array names). list() is a php language constructor that assign a list of variables in one operation. Please note that list() can be used only with non-associative arrays.


fusion3k
  • 11,568
  • 4
  • 25
  • 47
0

hy, i try to use this example in visual composer plugin, if i return

svca_icon_fields()[0],
svca_icon_fields()[1],
svca_icon_fields()[2],
etc...,

all works good

how to return all values like

svca_icon_fields(),