1

Im trying to work out how to get the value from one setting in an array.

When i do this

<?php 
$data = array($settings);
echo "<pre>";
echo print_r($data);
echo "</pre>";
?>

I get this ( but i dont want to just spit the array out like below )

Array
(
    [0] => Array
        (
            [layout] => pizza:simple
            [style] => pizza:red
        )

)
1

I tried this but no luck ( What i need is just the value of layout or style )

<?php
$data = array($settings);
$layout = $data->layout;
?>

So basically i can now use the $layout value for things i want to do on the page.

How do i do this please?

Thanks in advance :) jonny

1
  • Why do you array-ify $settings anyway? It's already an array. Access like so, $settings['layout']. Commented Mar 28, 2012 at 1:56

2 Answers 2

1

$data[0]['layout'] or $data[0]['style']

This is an associative array so its basically an array inside of an array. So if you had another value 1 it could have a different layout and pizza and it would be accessed by $data[1]['layout'] etc

Sign up to request clarification or add additional context in comments.

3 Comments

Ok thats cool! thank you, it worked. Can you explain why as well. Im doing everything backwards. Learning without manuals or school. I just pick it up as i go along.
This is an associative array so its basically an array inside of an array. So if you had another value 1 it could have a different layout and pizza and it would be accessed by $data[1]['layout'] etc
Please edit your answer and move your comments up into there to provide more details for posterity. Thanks.
1

if you intend to use

$data->layout

You will have to define class and layout as its property

class Settings {
    public $layout;
    public $style;

    public function __construct($data = null) {
         $this->layout = $data['layout'];
         $this->style = $data['style'];
    }
}

// you can now use it

$data = new Settings($settings);
echo $data->layout; // this will work

Comments

Your Answer

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.