Welcome Guest, Not a member yet? Register   Sign In
How to remove the last element of Array?
#1

[eluser]sovandy[/eluser]
Hello Codeigniter's members,

I want to view CategoryName in my Book Management. I used form_dropdown() . I can insert Cat_id into database. It works well, but the problem is it left an empty record in that dropdown box.

In that function, it return $array and it include a space record too. I don't know how to remove the last element of array.

Here is my code

Code:
function call_cat_name(){
                
                $cat_id="";
                $category="";
                $query=$this->book_model->get_cat_name();
                foreach($query as $row){
                    $cat_id .= $row['cat_id'];
                    $cat_id .= ",";
                    $category .= $row['cat_name'];
                    $category .= ",";
                }
                $comma=",";//separate by commas
                $arr_cat_id=split($comma,$cat_id);
                $arr_category=split($comma,$category);
                $options=array_combine($arr_cat_id,$arr_category);
        
                return $options;
}
#2

[eluser]Dam1an[/eluser]
To remove the last item you can use the array_pop function
Code:
$options = array_pop($options);
#3

[eluser]xwero[/eluser]
why create a string if you need an array?
Code:
function call_cat_name(){
                
                $arr_cat_id=array();
                $arr_category=array();
                $query=$this->book_model->get_cat_name();

        
                foreach($query as $row){
                    $arr_cat_id[] = $row['cat_id'];
                    $arr_category[] = $row['cat_name'];
                }
                
                return array_combine($arr_cat_id,$arr_category);
}
If you create a string with a postfix in a loop you know you need to cut off the postfix, an easy way to do it is to use the substr function
Code:
$category = "";
$comma=",";//separate by commas // make the variable as useful as possible

foreach($query as $row){
                    $category .= $row['cat_name'];
                    $category .= $comma;
                }

$category = substr($category,0,-1);
from the split function php documentation
Quote:preg_split(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to split(). If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.

That snippet you showed has quite a number of beginners mistakes and removing the last array element is the least.
#4

[eluser]xwero[/eluser]
Dam1an your answer will not learn sovandy how to code well, like i mentioned in my response popping the array is the least of the mistakes.

I almost cut the lines of code in half with my code. And it can get better still
Code:
function call_cat_name(){
                
                $options=array();
                $query=$this->book_model->get_cat_name();

        
                foreach($query as $row){
                    $options[$row['cat_id']] = $row['cat_name'];
                }
                
                return $options;
}
#5

[eluser]Dam1an[/eluser]
[quote author="xwero" date="1245762512"]Dam1an your answer will not learn sovandy how to code well, like i mentioned in my response popping the array is the least of the mistakes.[/quote]

Sorry, please forgive me Wink
I just saw the question, saw the last line of the function (didn't even look at some of the flaws in the part leading up to it) and gave them what they asked for, instead of looking into what was the cause of the problem

I'll try harder next time
#6

[eluser]xwero[/eluser]
I just think if you offer a solution on the forum you should provide the best solution you know. If you just answer the question beginners will keep on writing excess code. It provides work for people who like refactoring code but i rather create new things than do maintenance work.

It's not personal, there are others who provide lazy solutions on the forum but i hope they read this and they will step up too. I have no doubt you provide good solutions most of the time, and even i get lazy but it's the one rule i use when i post a solution.
#7

[eluser]Dam1an[/eluser]
Don't worry, I'm not taking it personally, and you're right, it was me being lazy (I'm still not fully awake yet)
And if you look through some of my history, you'll see a lot of the time I do try to come up with better solutions Smile

And I'll shut up before I derail this topic
#8

[eluser]sovandy[/eluser]
Thank you very much everyone for your help.
I can do it now. I'm sorry if I have any mistakes with English because English is my second language.

Best regards,
<i>SOVANDY</i>
#9

[eluser]jdfwarrior[/eluser]
Dam1an always beats me to every response! You and your 6 hour advantage. It's just not fair.
#10

[eluser]Thorpe Obazee[/eluser]
[quote author="jdfwarrior" date="1245778867"]Dam1an always beats me to every response! You and your 6 hour advantage. It's just not fair.[/quote]

Probably should have a "CI! Answers" in the CodeIgniter website Tongue




Theme © iAndrew 2016 - Forum software by © MyBB