This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

ge.dalot28295
ge.dalot28295

Hello guys,

I've been trying to filter the last post of each main category on rainlab blog but I am struggling to find why am I having this error:

"Cannot use object of type stdClass as array"

function unique_multidim_array($array, $key) 
{ 
    $temp_array = array(); 
    $i = 0; 
    $key_array = array(); 

    foreach($array as $val) { 
        if (!in_array($val[$key], $key_array)) { 
            $key_array[$i] = $val[$key]; 
            $temp_array[$i] = $val; 
        } 
        $i++; 
    } 
    return $temp_array; 
} 

function onStart()
{
   $posts = Db::select("select rainlab_blog_categories.id, post_id, nest_depth, rainlab_blog_categories.slug, content, title, published_at  from rainlab_blog_posts_categories inner join rainlab_blog_categories on rainlab_blog_posts_categories.category_id=rainlab_blog_categories.id inner join rainlab_blog_posts on rainlab_blog_posts.id=rainlab_blog_posts_categories.post_id where nest_depth=0 ORDER BY published_at DESC;");
   $posts = $this->unique_multidim_array($posts, "id");
   dd($posts);
}
ge.dalot28295
ge.dalot28295

I solved it converting the object $array to an actual array, as well as $val, like this:

function unique_multidim_array($array, $key) { 
    $temp_array = array(); 
    $i = 0; 
    $key_array = array(); 
    $array = (array) $array;

    foreach( $array as $val) { 
         $val = (array) $val;
        if (!in_array( $val[$key], $key_array)) { 
            $key_array[$i] = $val[$key]; 
            $temp_array[$i] = $val; 
        } 
        $i++; 
    } 
    return $temp_array; 
} 

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.