Forum

Thread tagged as: Question

Pass array into perch_content_custom each function

Hi!

I'm trying to pass an array into the perch_content_custom so I can then edit some values in each item, something like this

$result /*the array I want to use*/
perch_content_custom('Destaques',['template' => $template,'count'=>$limit,
                    'each' => function($item) {
                    global $index;
                    $index = ((isset($index)) ?  ($index+1) : 0 );

                    // process as necessary, then return the modified array\

                    $item['link'] = $result[$index]["permalink_url"];
                    $item['text'] = $result[$index]["message"];
                    $item['data']  = $result[$index]["created_time"];
                    $item['image']  = $result[$index]["full_picture"];
                    return $item;
                }
            ]);

Is it possible?

Gustavo Bica

Gustavo Bica 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Gustavo,

You have to use the use keyword: https://php.net/manual/en/functions.anonymous.php

$result = array();

perch_content_custom('Destaques',[
'template' => $template,
'count'=>$limit,
'each' => function($item) use ($result) {
}
]);

Thank you ;)