4

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

8
  • I think both are same. I dont get any difference between what you get and what you want ..
    – Yash Shah
    Commented Jun 10, 2019 at 6:12
  • @YashShah check updated question
    – Waqar Ali
    Commented Jun 10, 2019 at 6:19
  • You can code like this. $options[2]['value'] = $cms->toArray();
    – Yash Shah
    Commented Jun 10, 2019 at 6:25
  • on toArray() getting error Fatal error: Uncaught Error: Call to a member function toArray() on array
    – Waqar Ali
    Commented Jun 10, 2019 at 6:29
  • 1
    In that case, $options[2]['value'] = $cms; should work.
    – Yash Shah
    Commented Jun 10, 2019 at 6:39

2 Answers 2

4

Why not?

$cms = $this->collectionFactory->create()->toOptionArray();
            $options = [
                // your code
                [
                    'label' => __('CMS pages'),
                    'value' => $cms
                ]
            ];
1
  • that worked for me thank you
    – Waqar Ali
    Commented Jun 10, 2019 at 7:17
3

Try Foreach loop will help.

Syntex :

$data = $cms->toArray();

foreach ($variable as $key => $value) 
{

}

let me know if anything you need.

Thanks

8
  • strill getting same result
    – Waqar Ali
    Commented Jun 10, 2019 at 6:09
  • try once $collection->toArray(); Commented Jun 10, 2019 at 6:13
  • i tried this getting Fatal error: Uncaught Error: Call to a member function toArray() on array
    – Waqar Ali
    Commented Jun 10, 2019 at 6:21
  • have you eddited question? Commented Jun 10, 2019 at 6:22
  • yes i just added code where i want to add these options
    – Waqar Ali
    Commented Jun 10, 2019 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.