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

Aasim Goriya
5,4622 gold badges30 silver badges54 bronze badges
asked Jun 10, 2019 at 5:57
8
  • I think both are same. I dont get any difference between what you get and what you want .. Commented Jun 10, 2019 at 6:12
  • @YashShah check updated question Commented Jun 10, 2019 at 6:19
  • You can code like this. $options[2]['value'] = $cms->toArray(); Commented Jun 10, 2019 at 6:25
  • on toArray() getting error Fatal error: Uncaught Error: Call to a member function toArray() on array Commented Jun 10, 2019 at 6:29
  • 1
    In that case, $options[2]['value'] = $cms; should work. 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
 ]
 ];
answered Jun 10, 2019 at 7:11
0
3

Try Foreach loop will help.

Syntex :

$data = $cms->toArray();
foreach ($variable as $key => $value) 
{
}

let me know if anything you need.

Thanks

Ronak Rathod
6,58020 silver badges46 bronze badges
answered Jun 10, 2019 at 6:07
8
  • strill getting same result 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 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 Commented Jun 10, 2019 at 6:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.