Arrays arrays arrays, creating a manipulating (PHP)

  • -->gm<--
  • Student
  • Student
  • User avatar
  • Posts: 68

Post 3+ Months Ago

Hi everyone,

I have some content returned from a db field that I need to retrieve the values form.
The data in the field is just a string with different values seperated by "/n"

The content returned from the query is:

Code: [ Select ]
searchable=1
some_value=asdadadadad
show_title=0
link_titles=
show_intro=
show_section=0
link_section=
show_category=0
link_category=
show_vote=
show_author=
show_create_date=
show_modify_date=
show_pdf_icon=
show_print_icon=
show_email_icon=
language=
keyref=
readmore=
  1. searchable=1
  2. some_value=asdadadadad
  3. show_title=0
  4. link_titles=
  5. show_intro=
  6. show_section=0
  7. link_section=
  8. show_category=0
  9. link_category=
  10. show_vote=
  11. show_author=
  12. show_create_date=
  13. show_modify_date=
  14. show_pdf_icon=
  15. show_print_icon=
  16. show_email_icon=
  17. language=
  18. keyref=
  19. readmore=


I then created an array from the text using (not sure if this is the right way of approaching it)...
Code: [ Select ]
$articleParams = explode("\n", $articleParams);
print_r($articleParams);
  1. $articleParams = explode("\n", $articleParams);
  2. print_r($articleParams);


and I get:

Code: [ Select ]
Array ( [0] => searchable=1 [1] => some_value=asdadadadad [2] => show_title= [3] => link_titles= [4] => show_intro= [5] => show_section= [6] => link_section= [7] => show_category= [8] => link_category= [9] => show_vote= [10] => show_author= [11] => show_create_date= [12] => show_modify_date= [13] => show_pdf_icon= [14] => show_print_icon= [15] => show_email_icon= [16] => language= [17] => keyref= [18] => readmore= )


I am trying to end up with an array like this:

$array = array(
'searchable' => '1',
'some_value' => 'asdadadadad',
);

so I can retrieve a value from the array...

Does anyone have any ideas???
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6255
  • Loc: Seattle, WA

Post 3+ Months Ago

The first thing that comes to mind is parse_ini_file(), but as the name suggests, the text needs to be located in a file.

You can use a regular expression to parse out each key/value pair in the contents of the query, rather than splitting the lines into an array:

Code: [ Select ]
// assuming text from the query is in $contents
$results = array();
$entry_regex = '/^\(w+)\=(\w*)$/';
$num_entries = preg_match_all($entry_regex, $contents, $matches);
for( $i = 0; $i < $num_entries; ++$i ) {
$results[$matches[1][$i]] = $matches[2][$i];
}

print_r($results);
  1. // assuming text from the query is in $contents
  2. $results = array();
  3. $entry_regex = '/^\(w+)\=(\w*)$/';
  4. $num_entries = preg_match_all($entry_regex, $contents, $matches);
  5. for( $i = 0; $i < $num_entries; ++$i ) {
  6. $results[$matches[1][$i]] = $matches[2][$i];
  7. }
  8. print_r($results);


Not tested.
  • -->gm<--
  • Student
  • Student
  • User avatar
  • Posts: 68

Post 3+ Months Ago

Hi Spork,

Thanks for the reply,

I came up with something else that works as well, figured if I grabbed the content before the '=' as the key and the content after the equals being the value.

Code: [ Select ]
$articleParams = explode("\n", $articleParams);

foreach($articleParams as $articleParam){
    $positionOfEquals = strrpos($articleParam, '=');
    $key = substr($articleParam, 0, $positionOfEquals);
    $value = substr($articleParam, $positionOfEquals + 1);
    $articleParamsArray[$key] = $value;
}

echo '<pre>';
print_r($articleParamsArray);
echo '</pre>';

echo $articleParamsArray['some_value'];
  1. $articleParams = explode("\n", $articleParams);
  2. foreach($articleParams as $articleParam){
  3.     $positionOfEquals = strrpos($articleParam, '=');
  4.     $key = substr($articleParam, 0, $positionOfEquals);
  5.     $value = substr($articleParam, $positionOfEquals + 1);
  6.     $articleParamsArray[$key] = $value;
  7. }
  8. echo '<pre>';
  9. print_r($articleParamsArray);
  10. echo '</pre>';
  11. echo $articleParamsArray['some_value'];


By the way, the article params are the values in 'attribs' field from Joomla 1.5 jos_content table
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6255
  • Loc: Seattle, WA

Post 3+ Months Ago

That will work, but the regular expression engine is usually faster than multiple calls to string manipulation functions in the long run.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

I don't remember what it was exactly, but I had parse problems that forced me to stay away from parse_ini_file a few months ago and do the parsing myself.

I remember my ini files were language files from existing applications, I think it had something to do with quotes or special characters. :scratchhead:
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6255
  • Loc: Seattle, WA

Post 3+ Months Ago

It probably doesn't matter in this case, since the overhead involved in creating a file from the query results before parsing the key/value pairs would be ridiculously unnecessary. And impractical.
  • Truce
  • Guru
  • Guru
  • Truce
  • Posts: 1477
  • Loc: Washington DC

Post 3+ Months Ago

PHP Code: [ Select ]
$lines = explode("\n", $queryResult);
 
foreach ($lines as $line) {
  list($property, $value) = explode('=', $line, 2);
  $setting[$property] = $value;
}
  1. $lines = explode("\n", $queryResult);
  2.  
  3. foreach ($lines as $line) {
  4.   list($property, $value) = explode('=', $line, 2);
  5.   $setting[$property] = $value;
  6. }

Personally, I try to keep things less complicated...

There's a joke about property values exploding (real estate) somewhere in there but I can't manage to find it.



:)
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6255
  • Loc: Seattle, WA

Post 3+ Months Ago

:lol:
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

lol I see that :lol:
  • cjxxi
  • Expert
  • Expert
  • cjxxi
  • Posts: 566
  • Loc: Fort Worth, TX

Post 3+ Months Ago

:lol: :lol: :lol: :lol:

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 23 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.