0
array
  0 => string 'profile' (length=8)
  1 => string 'helloworld' (length=8)
  2 => string 'string2' (length=7)

// lets say we have an defined string that we want to split it out. or make it disappear.

string 'profile' (length=8)

how can we just get helloworld and string2 in an array from the array useing the string we defined ? ( get the a defined string out of the array ) is there a good function for this problem ?

the result should be like

array
  1 => string 'helloworld' (length=8)
  2 => string 'string2' (length=7)

Thanks for looking in

Adam Ramadhan

4
  • 1
    Sorry it is not clear what you are asking, there are functions like in_array, array_search, array_keys and array_values at your disposal that should do what I think you are asking, if not please clarify. Commented Mar 6, 2011 at 12:01
  • i just want to split the array above, with the string we defined. and make a new string that have helloworld in it. wait let me edit Commented Mar 6, 2011 at 12:02
  • Still confusing. Could you please add some more keys to your main array and make us see what you really want? Commented Mar 6, 2011 at 12:09
  • i just want to get a defined string out of the array. what do we call that ? Commented Mar 6, 2011 at 12:11

5 Answers 5

3
$yourDefinedString = "profile";
foreach($yourArray as $myArray){
   if($myArray != $yourDefinedString){
      echo $myArray;
   }
}

Some variations possible, depending on how you would handle this array:

array
  0 => string 'profile' (length=8)
  1 => string 'helloworld' (length=8)
  2 => string 'anotherString' (length=8)

This example would print

helloworldanotherString

You could add newlins or spaces of course

after your edit: You could just remove stuff by getting the difference between two arrays? You could even remove more than just one string:

$theOffendingStrings = array("yourString");
$result = array_diff($youArray, $theOffendingStrings);
Sign up to request clarification or add additional context in comments.

1 Comment

how about turning it back to array ? like the result above, sory ive edited the question clearer. thanks btw.
3

The easiest way:

$array = array(
    'profile',
    'helloworld',
    'string2'
);

$str = 'profile';

$array = array_flip($array);
unset($array[$str]);
$array = array_flip($array);

// Array
// (
//     [1] => 'helloworld',
//     [2] => 'string2'
// )

Comments

1

echo $var[1] will display hello world, where $var is the variable your are doing var_dump($var) now.

Comments

0

You can use array_filter() as well:

$array    = ['profile', 'helloworld', 'string2'];
$ignore   = 'profile';
$filtered = array_filter(
    $arr,
    function ($i) use ($ignore) {
        return $i !== $ignore;
    }
);

Comments

-2

Seems like you’re printing something expecting it to be a string, but the value is actually an array.

So, if it’s going to be an array always, you may print all items comma separated -

echo implode(", ", $yourVar);

Or, it’s can be an array sometimes, you may concat conditionally-

 echo is_array($yourVar)? implode(", ", $yourVar) : $yourVar;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.