0

I'm using a foreach loop to go through my array. I'm making changes to specific values, but leaving alone the rest. (In this case, I'm making a break before every item with a colon, unless it's the first one)

If the current value contains a specific string, I want to remove that value and the value immediately after it within the array and continue from there in the foreach loop.

For example:

$items = array( 'Color:', 'blue', 'Size:', '12" x 12"', 'Weight:', '5 lbs' );
$first = true;

foreach ($items as $item) {

  if ((strpos($item, ':')) && $first) {
    $item = '<b>' . $item . '</b>';
    $first = false;
  } elseif (strpos($item, ':')) {
    $item = '<br>' . '<b>' . $item . '</b>';
  }

  if (strpos($item, 'Size:')) {
    // Remove this item (Size) and the one directly after it (12" x 12")
  }
}

This is what I'd like to return:

$items = array( 'Color:', 'blue', 'Weight:', '5 lbs' );

Since different items have a variety of values, which won't necessarily be in the same order every time, I can't select by item[2] and item[3], etc.

Chopin23
  • 87
  • 1
  • 8
  • Make use of [`in_array()`](http://php.net/manual/en/function.in-array.php) – Umair Shah Oct 25 '17 at 17:35
  • Couldn't you just parse the array? Like so: [https://eval.in/886921](https://eval.in/886921). Then, remove with `unset`. – FirstOne Oct 25 '17 at 17:39
  • Refer to this question for answer : https://stackoverflow.com/questions/3059392/how-to-find-a-value-in-array-and-remove-it-by-using-php-array-functions – Umair Shah Oct 25 '17 at 17:53
  • @FirstOne You may be on the right track, but I'm not attempting to remove the colon, just the key "Size:" and the value directly after it. Sorry if i'm misunderstanding something. I'm a noob :) – Chopin23 Oct 25 '17 at 17:57
  • @Chopin23 I Removed the colon so it becomes a more general-pupose array. I prefer to add the colon later. Besides, parsin that way makes it easier to work with and simpler to find or remove keys and values. – FirstOne Oct 25 '17 at 17:59
  • With my code, calling `unset($arr['Size']);` would remove the given item from it. And if you want a value, just call for example `$arr['Color']`. Adding would be simple too: `$arr['Foo'] = 'Bar';`. – FirstOne Oct 25 '17 at 18:00
  • @FirstOne Oh, I see! So, in this scenario, after you've got the array to equal this: `Array( [Color] => blue [Size] => 12" x 12" [Weight] => 5 lbs )` , how would your code look to add the breaks before the keys and bold them? – Chopin23 Oct 25 '17 at 18:21
  • @Chopin23 That's a different question now. Specially since we don't know how you present that information (echo etc). But: concatenate html and php to achieve your expected result. – FirstOne Oct 25 '17 at 18:27
  • @FirstOne That's fair! Well, if you submit an answer, I can mark yours as the correct one. :) – Chopin23 Oct 25 '17 at 18:42

3 Answers3

0

As far as I understand you want to get rid of the string array "Size". If so, then you can solve this problem in two ways. In the first (in your case) I suggest to use the unset function in the loop as in the example of colleague Lucas Vieira. In the second case, I suggest to use functions instead of for loops as in the example below:

if(array_key_exists('Size:',  $items))
    unset($items['Size:']);

btw. You have a weird array structure, it's built into an associative array, so it's easier to navigate through it. For example:

$items = array('Color:' => 'blue', 'Size:' => '12 x 12',  'Weight:' => '5 lbs' );

foreach ($items as $key => $item)
   if (strpos($key, 'Size:') !== false)
      unset($items[$key]);
0

If you parse that array, you can handle it better (such as using the code below to remove an item)

unset($arr['Size']);

To be able to do that, parse it like so:

$items = array( 'Color:', 'blue', 'Size:', '12" x 12"', 'Weight:', '5 lbs' );
$key = null;
$arr = array();
foreach($items as $i){
  if(empty($key)){
    $key = $i;
  }else{
    $arr[rtrim($key, ':')] = $i;
    $key = null;
  }
}

Now $arr looks like this:

Array
(
    [Color] => blue
    [Size] => 12" x 12"
    [Weight] => 5 lbs
)

See an example.


You probably noticed I removed the colon from the items. I prefer to handle the data as is, and concatenate when needed, just like you're concatenating the html tag.

FirstOne
  • 6,033
  • 7
  • 26
  • 45
-1

You can do the following

foreach ($items as $key => $item) {
    if (strpos($item, 'Size:') !== false) {
        unset($items[$key]);
        unset($items[$key + 1]);
    }
}
Lucas Vieira
  • 131
  • 9