We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How do I delete a word from a array?

Hi

I don't know how to do this. How can I delete a word from my array.

$cheesecake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

I want to take out the Blueberry Cheesecake from the array. How do I do that?

This is what I got so far:

<?php

$cheesecake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

$cake("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

$blueberry = str_replace($cheesecake, $cake);

echo "I am going to eat an ".$cake. "<br />";
?>

There's an error and it said Function name must be a string

When I echo it out it should said I am going to eat an Blueberry Cheesecake

Any Suggestions and explanation will help. I appreciate it. Thanks!

4
Contributors
8
Replies
10 Hours
Discussion Span
6 Months Ago
Last Updated
10
Views
Question
Answered
LastMitch
Industrious Poster
4,118 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

first i wanna ask, can you clarify this line

$cake("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

is that an array? it yes
it should be

$cake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");
vaultdweller123
Posting Pro
574 posts since Sep 2009
Reputation Points: 47
Solved Threads: 81
Skill Endorsements: 2

@vaultdweller123

Thanks for the reply! Yes, it's an array! I was learning OOP the whole day. I forgot to put it in there.

<?php

$cheesecake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

$cake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

$blueberry = str_replace($cheesecake, $cake);

echo "I am going to eat a ".$cake. "<br />";

?>

There's no error now!

But now it echo I am going to eat a Array

Maybe I wrote my thread wrong. I should have wrote take a word from an array and echo it out.

Not how can I delete a word from my array.

How do I take the Blueberry Cheesecake from the Array and echo it out?

LastMitch
Industrious Poster
4,118 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

honestly, i don't know what are you trying to achieve.... im confused why you have two arrays with the same values? well based on your comment, you want to echo the "Blueberry Cheesecake"? then you could just

echo "I am going to eat a ".$cake[1]. "<br />";
vaultdweller123
Posting Pro
574 posts since Sep 2009
Reputation Points: 47
Solved Threads: 81
Skill Endorsements: 2

not sure what your asking, (could just be me being tired...:( ) but can't you just remove blueberry cheesecake from the array?

$cheesecake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

I mean that would definetly get rid of blueberry cheesecake if you delete... so it will look like this...

$cheesecake = array("Apple Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

If there is another problem... it could be from somewhere else in your php...

(I CAN'T BELIEVE I AM ACTUALLY HELPING YOU!!! YOU ALWAYS ANSWER MY QUESTIONS, NOW I GET TO PARTICIPATE IN ANSWERING YOURS!!! WOOHOO)

<M/>
Senior Poster
3,611 posts since Apr 2012
Reputation Points: 64
Solved Threads: 77
Skill Endorsements: 90

@vaultdweller123

honestly, i don't know what are you trying to achieve.... im confused why you have two arrays with the same values? well based on your comment, you want to echo the "Blueberry Cheesecake"? then you could just

Yes, I think I wrote it wrong. It should be 1 array not 2 arrays.

I will make an adjustment!

LastMitch
Industrious Poster
4,118 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45

@vaultdweller123

It work! I think I was overthink about the array.

$cake can echo Blueberry Cheesecake

$cheesecake can also echo Blueberry Cheesecake.

I guess either array can echo Blueberry Cheesecake!

I figure out how you came up with the number!

It's from here:

<pre>
<?php
$cheesecake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");
$cake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");
print_r($cheesecake);
print_r($cake);
?>
</pre>

When I print_r($cheesecake); or print_r($cake);

This came out:

Array
(
    [0] => Apple Cheesecake
    [1] => Blueberry Cheesecake
    [2] => Strawberry Cheesecake
    [3] => Mango Cheesecake
    [4] => Raspberry Cheesecake
)

So when I echo either example:

<?php
$cake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");
echo "I am going to eat a ".$cake[1]. "<br />";
?>

-

<?php
$cheesecake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");
echo "I am going to eat a ".$cheeesecake[1]. "<br />";
?>

It will echo I am going to eat an Blueberry Cheesecake

Thanks for being patience and helping me out with this issue. Thanks!

LastMitch
Industrious Poster
4,118 posts since Mar 2012
Reputation Points: 132
Solved Threads: 334
Skill Endorsements: 45
Question Answered as of 6 Months Ago by vaultdweller123 and <M/>

Oh... now i understand what your doing... Great job in solving it!

<M/>
Senior Poster
3,611 posts since Apr 2012
Reputation Points: 64
Solved Threads: 77
Skill Endorsements: 90

You have remove the word start with "in" are still in word list once they' have "in" removed then you need something a little different.. Follow the array word code: newlist = [word for word in wordlist if word.startswith('in') and word[2:] in wordlist

paulinetaylor85
Newbie Poster
11 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1310 seconds using 2.67MB