views:

55

answers:

4

I have an array containing much more items than just this one. This is just an example of an item.

[0] => Array
        (
            [id] => 6739380664
            [created_at] => 1260991464
            [text] => @codeforge thx for following
            [source] => web
            [user] => Array
                (
                    [id] => 90389269
                    [name] => Lea@JB
                    [screen_name] => Lea_JB
                    [description] => Fan of JB and Daourite singers!! (:
                    [location] => Germany
                    [url] => 
                    [protected] => 
                    [followers_count] => 33
                    [profile_image_url] => http://a3.01/Usher_und_JB_normal.jpg
                )

            [truncated] => 
            [favorited] => 
            [in_reply_to_status_id] => 
            [in_reply_to_user_id] => 18055539
        )

And I have a function

function parseLink($text)
{
  $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $text);
  return $text;
}

How can I apply my function parseLink($text) for the array item text , without having to go through an loop?

that it returns the whole array containing all all fields as it was, but with the modiefied array field text ? its not just the item $myarray[0]; there are more items like $myarray[1],$myarray[2] and soon, I hope I could explain my question clearly.

Thanks

+6  A: 

You can accomplish this in 2 different ways:

1) You could use the return value of parseLink() and re-assign the variable in the array:

$myText = parseLink($myArray[0]['text']);
$myArray[0]['text'] = $myText;

2) You can modify your parseLink() function to accept the argument by reference which would cause it to be modified in place:

function parseLink(&$text)
{
    $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $text);
    return $text;
}

parseLink($myArray[0]['text']);
Asaph
but i dont have just one item in that array there is may be about 20 and with your soulution i cant access the text item $myarray[0]['text']
streetparade
sure you can jsut through it in a loop like`foreach($myArray as $item){ parseLink($item['text']);}`
prodigitalson
thats the point i dont want to go throw an loop,
streetparade
Then use my solution with array_map i guess... Why exactly are you so opposed to the loop?
prodigitalson
@ prodigitalson 4 hmm.. performance issues
streetparade
@streetparade: well if your only talking the realm of hundreds or less (as you alluded to with your comment above) thats not going to be that big of an issue. If there are more then ok, but i dont know that the functions that essentially loop over the array internally in C are going to get you that big of gain as opposed to using the most economic loop structure in php (i believe thats a `while` loop but i could be wrong). in fact in the case of array_map i think the loop might actually be faster for this simple thing but im not 100% on that.
prodigitalson
@streetparade: Don't worry about performance of a loop. The loop will perform as well as any other technique in PHP (which doesn't do anything threaded which might speed things up). Any other technique in PHP probably just uses a loop under the hood anyway (eg. `array_map`). In any case, it's unlikely that CPU time will be a bottleneck in a typical database driven web application written in PHP. If you haven't actually measured a performance problem, don't sweat it.
Asaph
well, i was wrong, i shouldnt think of performance issues, with an small array like this. your soulution worked, thanks very much i created an for loop it looks like this $directmessages = $twitter->getDirectMessages();for($i=0;$i<count($directmessages);$i++) { $mess[$i] = $directmessages;$mess[$i]['text']=parseLink($directmessages[$i]['text']); }print_r($directmessages);this prints i just cutted the array in the ['text'] part elseit would be to much text.[text] => Thank you for the twitter follow. <a href="http://www.bla.bla/">http://www.bla.bla/</a>
streetparade
+1  A: 

Edit: my mistake, try this:

$myFunction = function parseLink($text) { /* do stuff with $text */ };

array_map($myFunction,$myArray);
inkedmn
this doesnt work
streetparade
you can do that in php < 5.3 can you?
prodigitalson
Still won't work. Hint: $text is not a string.
Rob
+1  A: 
// assume your array is stored in $myArray
parseLink($myArray[0]['text']);

You should change your function to pass by reference as well:

function parseLink(&$text)
{
  $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $text);
}

Please note that you had a bug in your parseLink() function as well that I fixed.

cballou
+1  A: 
function parseLink($data)
{
  if(is_array($data) && isset($data['text']))
  {
    $data['text'] = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $data['text']);
  } elseif(is_string($data))
  {
     $data = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $data);
  }


  return $data;
}

array_map('parseLink', $myArray);
prodigitalson