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?

mickmackusa
49.1k13 gold badges97 silver badges163 bronze badges
asked Nov 18, 2013 at 18:39
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);
}
answered Nov 18, 2013 at 18:42

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);
}
answered Nov 18, 2013 at 18:42

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);
answered Nov 18, 2013 at 18:43

Comments

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.