Php ile bir dizi bir kullanıcı Fonksiyonu işletilmesi

3 Cevap php

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
        )

Ve ben bir işlevi var

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

Nasıl benim işlevi bir döngü üzerinden gitmek zorunda kalmadan dizi öğe text, {için [(0)]} başvurabilir?

That it returns the whole array containing all all fields as it was, but with the modiefied array field text? It's not just the item $myarray[0]; there are more items like $myarray[1],$myarray[2] and soon.

3 Cevap

2 farklı yolla gerçekleştirebilirsiniz:

1) parseLink() dönüş değerini kullanabilir ve dizi değişkeni yeniden atamak:

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

2) Bunu yerine modifiye neden hangi referansla argüman kabul etmek parseLink() fonksiyonunu değiştirebilirsiniz:

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

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

Edit: benim hatamı, bu deneyin:

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

array_map($myFunction,$myArray);
// assume your array is stored in $myArray
parseLink($myArray[0]['text']);

Siz de referans olarak geçmek için işlevini değiştirmek gerekir:

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

Sen de ben sabit sizin parseLink() fonksiyonunda bir hata vardı unutmayın.