up vote 0 down vote favorite

I have an array $data

 fruit => apple,
 seat => sofa,

etc. I want to loop through so that each key becomes type_key[0]['value'] so eg

 type_fruit[0]['value'] => apple,
 type_seat[0]['value'] => sofa,

and what I thought would do this, namely

foreach ($data as $key => $value)
{
        # Create a new, renamed, key. 
        $array[str_replace("/(.+)/", "type_$1[0]['value']", $key)] = $value;

        # Destroy the old key/value pair
        unset($array[$key]);

}

print_r($array);

Doesn't work. How can I make it work?

Also, I want everything to be in the keys (not the values) to be lowercase: is there an easy way of doing this too? Thanks.

link|flag

I'm not sure what you're expecting here. You want the KEY to look like type_fruit[0]['value'], so you can access it using $array["type_fruit[0]['value']"]? – zneak Mar 25 at 20:46

4 Answers

up vote 1 down vote accepted

Do you mean you want to make the keys into separate arrays? Or did you mean to just change the keys in the same array?

    $array = array();
    foreach ($data as $key => $value)
    {
        $array['type_' . strtolower($key)] = array(array('value' => $value));
    }

if you want your keys to be separate variables, then do this:

    extract($array);

Now you will have $type_fruit and $type_sofa. You can find your values as $type_fruit[0]['value'], since we put an extra nested array in there.

link|flag
Don't forget to strtolower() the keys :) – Dor Mar 25 at 20:56
up vote 0 down vote

Your requirements sound... suspect. Perhaps you meant something like the following:

$arr = array('fruit' => 'apple', 'seat' => 'sofa');
$newarr = array();

foreach ($arr as $key => $value)
{
  $newkey = strtolower("type_$key");
  $newarr[$newkey] = array(array('value' => $value));
}

var_dump($newarr);
link|flag
up vote 0 down vote

First I wouldn't alter the input-array but create a new one unless you're in serious, serious trouble with your memory limit.
And then you can't simply replace the key to add a deeper nested level to an array. $x[ 'abc[def]' ] is still only referencing a top-level element, since abc[def] is parsed as one string, but you want $x['abc']['def'].

$data = array(
 'fruit' => 'apple',
 'seat' => 'sofa'
);

$result = array();

foreach($data as $key=>$value) {
  $target = 'type_'.$key; 
  // this might be superfluous, but who knows... if you want to process more than one array this might be an issue.
  if ( !isset($result[$target]) || !is_array($result[$target]) ) {
    $result[$target] = array();
  }
  $result[$target][] = array('value'=>$value);
}

var_dump($result);
link|flag
up vote -1 down vote

for starters

$array[str_replace("/(.+)/", "type_$1[0]['value']", $key)] = $value;

should be

$array[str_replace("/(.+)/", type_$1[0]['value'], $key)] = $value;
link|flag
remove the " around type_$1 – Grumpy Mar 25 at 20:48
Are you kidding me? You just broke further his call to str_replace instead of using preg_replace to fix it. – zneak Mar 25 at 20:50
"type_$1[0]['value']" will be treated as a string so type_$1[0]['value'] will return the value he is looking for ( altough i agree he is using the wrong function ) – Grumpy Mar 25 at 20:59
No, it's not. There's a $ in the middle of it. It's just a very good way to get a syntax error: unexpected $. Besides, how/why is that better? Even if it worked, using string literals without quotes emits a notice telling it shouldn't be done. – zneak Mar 25 at 21:09

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.