Member Avatar for LastMitch

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!

Recommended Answers

All 8 Replies

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");
Member Avatar for LastMitch

@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?

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 />";
commented: Thanks for the solution! +5

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)

Member Avatar for LastMitch

@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!

Member Avatar for LastMitch

@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!

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.