5

When a object with private variables, has converted (cast) to an array in php the array element keys will be started with

*_

. How to remove the "*_"s that exists at the beginning of array keys?

For example

class Book {
    private $_name;
    private $_price;
}

the array after casting

array('*_name' => 'abc', '*_price' => '100')

I want

array('name' => 'abc', 'price' => '100')
0

4 Answers 4

12

I did it in this way

class Book {
    private $_name;
    private $_price;

    public function toArray() {
        $vars = get_object_vars ( $this );
        $array = array ();
        foreach ( $vars as $key => $value ) {
            $array [ltrim ( $key, '_' )] = $value;
        }
        return $array;
    }
}

and when I want to convert a book object to an array I call the toArray() function

$book->toArray();
Sign up to request clarification or add additional context in comments.

1 Comment

Handy, I didn't know about get_object_vars. I knew there had to be a simple way to convert a basic object to an array. Thanks!
3

to do this properly you need to implement a toArray() method in your class. That way you can keep your properties protected and still have access to the array of properties.
There are many ways to accomplish this, here is one method useful if you pass the object data to the constructor as an array.

//pass an array to constructor
public function __construct(array $options = NULL) {
        //if we pass an array to the constructor
        if (is_array($options)) {
            //call setOptions() and pass the array
            $this->setOptions($options);
        }
    }

    public function setOptions(array $options) {
        //an array of getters and setters
        $methods = get_class_methods($this);
        //loop through the options array and call setters
        foreach ($options as $key => $value) {
            //here we build an array of values as we set properties.
            $this->_data[$key] = $value;
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }

//just return the array we built in setOptions
public function toArray() {

        return $this->_data;
    }

you can also build an array using your getters and code to make the array look how you want. Also you can use __set() and __get() to make this work as well.

when all is said and done the goal would be to have something that works like:

//instantiate an object
$book = new Book(array($values);
//turn object into an array
$array = $book->toArray();

Comments

2

You're probably running into problems because you're accessing private variables outside of their allowed scope.

Try changing to:

class Book {
    public $_name;
    public $_price;
}

Or, a hack:

foreach($array as $key => $val)
{
   $new_array[str_replace('*_','',$key)] = $val;
}

4 Comments

No i just want to remove *_ in the array
Try implementing what I've recommended
It is not possible because I am accessing the variables only through the getters and setters (Encapsulation).
You don't seem to be putting the $ before your variables and using the appropriate privacy scope. I've included a hack which should reset the array keys for you.
1

Here are the steps for Convert object to array

1). Convert Object to Array

2). Convert array to json String.

3). Replace string to remove "*_"

e.g
    $strArr= str_replace('\u0000*\u0000_','',json_encode($arr));
    $arr = json_decode($strArr);

1 Comment

Thanks for the simple one liner. Flag to return an associative array must be set on json_decode, otherwise we will get an object back.

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.