How to use data from an Array without foreach

1

I have a function in my Model :

public function get_learning_category_list() {

    $categorias = $this->db->select('t1.id, t1.title, t1.metadata, t1.meta_title, t1.description, t1.meta_description, t1.meta_spam, t1.url, t1.alt_img')
        ->from('learning_category t1')
        ->join('learning_rel_category t4', 't1.id = t4.category_id', 'left')
        ->join('learning t2', 't4.learning_id = t2.id', 'left')
        ->join('learning_rel_language t3', 't2.id = t3.learning_id', 'left')
        ->where('t3.language', $this->language)
        ->group_by('t1.id')
        ->get()->result_array();

    foreach ($categorias as $c) {

        $total = $this->db->select('count(learning_rel_language.learning_id) as total', false)
            ->from('learning')
            ->join('learning_rel_language', 'learning.id = learning_rel_language.learning_id', 'inner')
            ->join('learning_rel_category', 'learning.id = learning_rel_category.learning_id', 'inner')
            ->where('learning_rel_category.category_id', $c['id'])
            ->where('learning_rel_language.language', $this->language)
            ->group_by('learning_rel_language.language')->get()->first_row('array');

        $metadata = json_decode($c['metadata']);
        $tt = $c['title'];
        $url = $c['url'];

        if ($this->language != 'pt_br') {
            $tt = $metadata->{$this->language};
            $tt_pt_br = $c['title'];
        }

        $return[] = array('url' => $url, 'title'=>$tt, 'id'=>$c['id'], 'total'=>$total['total'], 'title_pt_br'=>$tt_pt_br);
    }

    return $return; //$categorias;
}

It results in a Array with the list of learnings :

Array (  
    [0] => Array (  
        [url] =>  
        [title] => Cursos sobre como gerar leads qualificados  
        [id] => 2  
        [total] => 6  
        [title_pt_br] =>  
    )  
    [1] => Array (  
        [url] =>  
        [title] => Cursos de Social Media Marketing  
        [id] => 3  
        [total] => 2  
        [title_pt_br] =>  
    )  
    [2] => Array (  
        [url] =>  
        [title] => Cursos de SEO e otimização de sites  
        [id] => 4  
        [total] => 3  
        [title_pt_br] =>  
    )  
)

But I need to use data from these learnings , title , id , etc, in another function in Model :

public function url_format_category($category, $lang_domin) {

    if (lang('abbr') == 'en_US')
        $lang_domin = 'en/';
    else if (lang('abbr') == 'es_US')
        $lang_domin = 'es/';

    $categorias = $this->learn->get_learning_category_list();
    print_r($categorias);

    foreach ($categorias as $cat) {

        print_r($cat);
        $cat = (object) $cat;

        if ($cat->title != '') {
            $return = strtolower(url_title($cat->title)).'-cmdo-'.$cat->id;
        }
        else {
            $return = 'cursos-de-marketing-digital-online-'.$cat->id;
        }   
        return $return;   
    } 
}

I'm making a function to see if the site url is correct. This function (url_format_category) will define the format of the url, which in this case would be (The title) .'- cmdo - '. (The id) So what I need is to retrieve the title and id according to the learning that is find.

I've done so, but with foreach always results in the same data regardless of which page I am.

How can I use data from this Array without foreach ?

    
asked by anonymous 08.04.2015 / 16:06

2 answers

1

Beast, you can search within your array in conjunction with the functions array_search and array_column (This function only for version equal or higher than PHP 5.5.0 and if you are using a hosting enable this version available).

I think you can adapt your code with this solution.

For your function, I imagine you use slugs (unique and friendly urls), so follow an example below the example in the PHP manual comments.

// Buscar o nome do usuário com uid == 40489
<?php 
      $array = array(
                     array(
                           'uid' => '100',
                           'name' => 'Sandra Shush',
                           'url' => 'urlof100'
                      ),
                      array(
                            'uid' => '5465',
                            'name' => 'Stefanie Mcmohn',
                            'url' => 'urlof100'
                      ),
                      array(
                            'uid' => '40489',
                            'name' => 'Michael',
                            'url' => 'urlof40489'
                       )
               );

$key = array_search(40489, array_column($array, 'uid')); /*A partir do $array, retorna um array com a coluna 'uid' e dentro desse array, procura o valor 40489 e armazena o índice do array externo na variável $key. */

if($key != null && !empty($key)) // se não estiver vazio essa chave, pesquise direto.
    echo $array[$key]['name'];
else
    echo "sem dados";

link

link

    
08.04.2015 / 20:46
1

GWER, if you want the $ return to return all the values assigned to the variable, just concatenate example $ return. = hence you return or print after foreach the variable if that what you want

example:

public function url_format_category($category, $lang_domin) {
if (lang('abbr') == 'en_US')
    $lang_domin = 'en/';
else if (lang('abbr') == 'es_US')
    $lang_domin = 'es/';

$categorias = $this->learn->get_learning_category_list();
print_r($categorias);
foreach($categorias as $cat){
    print_r($cat);
    $cat = (object) $cat;
    if($cat->title != '') {

        //concatena  
        $return.= strtolower(url_title($cat->title)).'-cmdo-'.$cat->id;
    }else{
        //concatena  
        $return.= 'cursos-de-marketing-digital-online-'.$cat->id;
    }   

}
return $return; ou echo $result;
}
    
08.04.2015 / 16:28