Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.