Jump to content

[SOLVED] Perform a function with every bit of data in an array


Dale_G

Recommended Posts

Hey everyone, quick question.

 

Alright, for example, let's say I have an array of random text.

 

$mytext = array( 'ololol', 'hilol', 'WTFBBQ', 'OMFGPWNISw##@234' );

 

Right? And I have a function that adds bold tags to text.

 

function bold_text( $text )
{
    return '<b>'.$text.'</b>';
}

 

Now, how can I apply the function to every item in the array of $mytext?

 

Yes I know I could just do it manually as I would only have to do it 4 times, but I have a more complicated use for this using an array that contains hundreds of items in the array.

 

So uh, thanks for the help fellow PHPers!

Link to comment
Share on other sites

try

<?php
$mytext = array( 'ololol', 'hilol', 'WTFBBQ', 'OMFGPWNISw##@234' );
function bold_text( &$text )
{
    $text = '<b>'.$text.'</b>';
}
array_walk($mytext, 'bold_text');
print_r($mytext);
?>

Link to comment
Share on other sites

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.