Magento2 – How to Convert Object into Array

magento2PHP

i Need All cms pages in an array I got All in an object but the format i got is not that I expected

$cms = $this->collectionFactory->create()->toOptionIdArray();
echo "<pre>";
print_r($cms->toArray());
exit();

these lines gave me result as

Array
(
    [0] => Array
        (
            [value] => no-route
            [label] => 404 Not Found
        )

    [1] => Array
        (
            [value] => home
            [label] => Home Page
        )

    [2] => Array
        (
            [value] => enable-cookies
            [label] => Enable Cookies
        )

    [3] => Array
        (
            [value] => privacy-policy-cookie-restriction-mode
            [label] => Privacy Policy
        )

)

but i need this something like

Array
(
     [
            "value" => 'no-route',
            "label" => '404 Not Found'
     ],
     [
            "value" => 'home',
            "label" => 'Home Page'
     ],
     [
            "value" => 'enable-cookies',
            "label" => 'Enable Cookies'
     ],
     [
            "value" => 'privacy-policy-cookie-restriction-mode',
            "label" => 'Privacy Policy'
     ],

]

i want to add CMS pages in a drop down

$options = [
            [
                'label' => __('Catagories'),
                'value' => [
                    [
                        'label' => __('Anchor Catagories'),
                        'value' => self::CATAGORIES_ANCHORED
                    ],
                    [
                        'label' => __('Non-Anchor Catagories'),
                        'value' => self::NON_CATAGORIES_ANCHORED
                    ]
                ]
            ],
            [
                'label' => __('All Products'),
                'value' => [
                    [
                        'label' => __('All Product Types'),
                        'value' => self::PRODUCT_ALL
                    ],
                    [
                        'label' => __('Simple Product'),
                        'value' => self::PRODUCT_SIMPLE
                    ],
                    [
                        'label' => __('Virtual Products'),
                        'value' => self::PRODUCT_VIRUAL
                    ],
                    [
                        'label' => __('Downloadable Products'),
                        'value' => self::PRODUCT_DOWNLOADABLE
                    ],
                    [
                        'label' => __('Configureable Products'),
                        'value' => self::PRODUCT_CONFIGUREABLE
                    ],
                    [
                        'label' => __('Grouped Products'),
                        'value' => self::PRODUCT_GROUPED
                    ],
                    [
                        'label' => __('Bundle Products'),
                        'value' => self::PRODUCT_BUNDLED
                    ]
                ]
            ],
            [
                'label' => __('CMS pages'),
                'value' => [


                ]
            ]
    ];

in the CMS pages options

Best Answer

Why not?

$cms = $this->collectionFactory->create()->toOptionArray();
            $options = [
                // your code
                [
                    'label' => __('CMS pages'),
                    'value' => $cms
                ]
            ];
Related Topic