1

I have an array like such:

array('some_key' => 'some_value');

I would like to take that and transform it to, this should be done programatically

array('some_key' => array('some_value'));

This should be rather simple to do but the only think I can find is answers on string split and explode, to explode strings into arrays. I thought PHP, like other languages, had something called to_array or toArray.

I am assuming this is super easy to do?

1
  • $arr['some_key'] = explode('', $arr['some_key']);? Commented Nov 18, 2013 at 18:41

3 Answers 3

1

If you're just doing the one array element, it's as simple as:

$newarray['some_key'] = array($sourcearray['some_key']);

Otherwise if your source array will have multiple entries, you can do it in a loop:

foreach($sourcearray AS $key => $value) {
    $newarray[$key] = array($value);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Something as simple as $value = array($value) should work:

foreach ($array as &$value) {
    $value = array($value);
}
unset($value); //Remove the reference to the array value

If you prefer to do it without references:

foreach ($array as $key => $value) {
    $array[$key] = array($value);
}

Comments

0

You can try like this

<?php
$arr=array('some_key' => 'some_value');
foreach($arr as $k=>$v)
{
$newarr[$k] = array($v);
}

var_dump($newarr);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.