0

I'm looking for an elegant approach to convert $array to $object.

If $array contains Array ( [0] => en=english [1] => fr=french ) the resulting $object should contain stdClass Object ( [en] => english [fr] => french )

The most elegant approach would be the one that uses the least variable names.

4 Answers 4

4

Should do it:

$object = new stdClass();

$array = array('en=english','fr=french', 'foo=bar=baz');
foreach($array as $item){
    list($name, $value) = explode('=', $item, 2);
    $object->$name = $value;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Won't work if you have an element "foo=bar=test". Although, the example in the question doesn't have such a case.
That's my bad for mis-copying the example! I've corrected that.
I know, but by the time I edited it you'd already posted yours and I would have basically given the same example, which didn't seem fair. This way there's some variety :)
could add the third argument to explode to limit the number of splits. That would handle foo=bar=baz
Beautiful! I didn't know explode() had a $limit parameter.
2

You could also use parse_ini_string

$object = (object) parse_ini_string(join(PHP_EOL, $array));

As long as all your values are key=val this will work.

All rules for ini files apply, meaning it will choke on foo=bar=baz but not on foo="bar=baz" because ini files allow additional = when it's put into quotes.


If you are not on PHP 5.3 yet, you can use parse_ini_file to accept the joined array contents from a data stream wrapper. This requires allow_url_include to be enabled in your php.ini though.

$object = (object) parse_ini_file(
    'data:text/plain,' . urlencode(join(PHP_EOL, $array))
);

In both cases, the output is the same.

4 Comments

Brilliant! That's exactly the kind of answer I've been hoping for. Btw, is passing PHP_EOL necessary? It seems to be working with just the one argument - join($array).
@Emanuil Are you sure? It doesnt work for me when I omit the PHP_EOL. In an ini file, new entries are separated by new lines, so I'd say you have to have it.
@Gordon, No, you are right - it doesn't. I missed that because I tested it on a single item array at first. I commented hasty. I apologize.
@Gordon, Thanks for adding a before PHP 5.3 approach! It's very nice to know as my server runs an earlier version. Would it be a bad idea to define a parse_ini_string function using if (!function_exists('parse_ini_string')){ function parse_ini_string ... in order for my code to work on versions prior to 5.3?
2

Best way to do this:

<?php
$arr = array('en=english','test=blaat','foo=bar=test');


foreach ($arr as $item)
{
  $opts = explode('=',$item);
  $name = array_shift($opts);
  $obj->$name = implode('=',$opts);
}

print_r($obj);

Returns:

stdClass Object  
(  
    [en] => english  
    [test] => blaat  
    [foo] => bar=test  
)  

Comments

1

You could do this

foreach ($arr as $item)
{
   $things=explode('=',$item);
   $newArray[$things[0]]=$things[1];
}
$obj=(object) $newArray;

4 Comments

Same issue here: Won't work if you have an element "foo=bar=test".
@Pelle ten Cate: As OP did not posted this type of example. The answer is completely based on shown examples.
You have a point there. Though, if the example would be exactly the only case he wanted to deal with, just setting the values manually would be even faster. :)
@Pelle ten Cate: Nice talk, But I have a small common sense so I know foo=bar indicates that foo is act as a variable and bar act as a value. There is not meaning of third thing "test"

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.