Jump to content

Turn part of array value, into key.


Zugzwangle

Recommended Posts

Hi.

I have an array named $parts:

[0] => Event Rated game, 90m + 5s 
[1] => Site Room 1 
[2] => Date 2010.07.05... 

and I simply want to use the first word of the array 'Value', as the 'Key'.  so the new array would look like this:

[Event] => Rated game, 90m + 5s 
[site] => Room 1 
[Date] => 2010.07.05... 

Please help if you can.

Link to comment
https://forums.phpfreaks.com/topic/207137-turn-part-of-array-value-into-key/
Share on other sites

That kind of works.  It results in:

Array ( 
[Event] => Rated game, 90m + 5s 
[ Site] => Room 1 
[ Date] => 2010.07.05 
[ Round] => ? ...

However there is a 'space' before Site for example.  I need to manipulate the data. 

echo $new_array[' Site'];  // outputs nothing.. 

How do I go about removing the white space if this is necessary?

 

Do something like:

<?php
$new_array = array();
foreach ($parts as $value)
{
    list($key,$entry) = explode(' ', $value,2);
    $new_array[trim($key)]= trim($entry);
}
echo '<pre>' . print_r($new_array,true) . '</pre>';
?>

 

ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.