1

I have an array in PHP and I don't know how to delete all the elements from every array element from certain character on, icluding that character. Is there a way to do this?

array(1092) {
  ["Piper;Rosii;Sare;Test;Vinete#####Piper ->Negru;Rosii ->Călite;Sare ->De masă, grunjoasă;Vinete ->Prăjite"]=>
  int(124)
}

In my example I want to delete all text from "#####", including "#####" to the end, foreach array element. Is this possible? Or is there a PHP function for this?

UPDATE

My result should look like this:

array(1092) {
  ["Piper;Rosii;Sare;Test;Vinete]=>
  int(124)
}
6
  • I think you made a little mistake with creating your array, regarding the key value .. Commented Nov 12, 2015 at 14:45
  • What do you mean? I want that key because it's a count. Long story. All i need is a way to delete from every array element the content from this point on '######'. Is there a PHP function for that? Commented Nov 12, 2015 at 14:49
  • Update your question and show us what you want the end results to look like. Commented Nov 12, 2015 at 14:52
  • And can you show the code where you create the array? Commented Nov 12, 2015 at 14:52
  • @Fluinc, I've updated my question with that. It helps? Commented Nov 12, 2015 at 14:55

3 Answers 3

1

You can use array_walk to apply the substr and strpos to each element:

$array = [
    '23845637;54634;345;3453345;#####morestuff',
    '234234#####34596078345j34534534',
    '34343245dfg#####asdfsadf;23452345;sdfsdf;345345'
];

array_walk($array, function(&$value, $key) {
    $value = substr($value, 0, strpos($value, '#####'));
});

var_dump($array);

Will result in:

array(3) {
    [0]=>
    string(27) "23845637;54634;345;3453345;"
    [1]=>
    string(6) "234234"
    [2]=>
    string(11) "34343245dfg"
}

This will modify the original array.

For each element in the array, we search for the position of '#####' in the string and only take the part from 0 to the position in the string where '#####' occurs.

Sign up to request clarification or add additional context in comments.

1 Comment

What they wanted to change was the was the key not the value.
1

This will do that by looping through the array exploding the key by ##### and adding it to a new array. I did it in a loop in case your array is bigger than 1

<?php
$oldArray = array("Piper;Rosii;Sare;Test;Vinete#####Piper ->Negru;Rosii ->Călite;Sare ->De masă, grunjoasă;Vinete ->Prăjite" => 124);
$newArray = array();

foreach ($oldArray as $key => $row) {
    $newKey = explode('#####', $key);
    $newArray[$newKey[0]] = $row;
}
var_dump($newArray);

Comments

1

You can use substr and strpos to add the new entry and then unset the old entry in the array like this example:

$array = array(
    "Piper;Rosii;Sare;Test;Vinete#####Piper ->Negru;Rosii ->Călite;Sare ->De masă, grunjoasă;Vinete ->Prăjite" => 124
);

foreach ($array as $key => $value) {
    $array[substr($key, 0, strpos($key, '#####'))] = $value;
    unset($array[$key]);
}

var_dump($array);

Will result in:

array(1) {
  ["Piper;Rosii;Sare;Test;Vinete"]=>
  int(124)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.