0

I have an array that looks like this:

  Array
(
    [basisprijs] => 17,00
    [basisstaffel] => 
    3-10:17;
    10-20:14;
    20-30:12;
    30-40:10;
    40-50:7,50;
    50-60:6,50;
    60-110:6;
    [minimaalformaat] => 10x3
    [maximaalformaat] => 120x5000
    [breedte] => 12
    [hoogte] => 4
    [aantal] => 1
    [Lijmlaag] => Wit(prijsberekening)+($m2*4);
)

I want to create a new array from [basisstaffel] with after each ; a new line starting, so the desired end result would be:

Array
(
    [0] = > 3-10:17;
    [1] = > 10-20:14;
    [2] = > 20-30:12;
    [3] = > 30-40:10;
    [4] = > 40-50:7,50;
    [5] = > 50-60:6,50;
    [6] = > 60-110:6;
)

How can I do that? Using explode on the ; makes me lose that part of the value. So is there another way?

The first array is called $productarray

twan
  • 2,450
  • 10
  • 32
  • 92
  • 2
    Have you tried exploding on `"\n"`? Because it seems each bit is on a new line. – KIKO Software Nov 22 '19 at 09:26
  • Not sure about the way you are storing the value in `basisstaffel`, this isn't normalised and could be better being split out in the database. https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad – Nigel Ren Nov 22 '19 at 09:29
  • @KIKOSoftware Thanks I'll try that. And Nigel ren yes I know but I am working with a CMS that has some limitations so this is how I have to store it. – twan Nov 22 '19 at 09:33
  • 2
    Possible duplicate of [Is there way to keep delimiter while using php explode or other similar functions?](https://stackoverflow.com/questions/2938137/is-there-way-to-keep-delimiter-while-using-php-explode-or-other-similar-function) – angel.bonev Nov 22 '19 at 09:37
  • you can try `explode` or `preg_split` – Gavin Kwok Nov 22 '19 at 10:07

4 Answers4

1

You could just use the explode function and after that, do a foreach loop and add the ';' symbol again, like so:

$newArray=array();
$myArray=array();

$myArray['basisprijs'] = '17,00';
$myArray['basisstaffel'] ='3-10:17;10-20:14;20-30:12;30-40:10;40-50:7,50;50-60:6,50;60-110:6';
$myArray['minimaalformaat'] = '10x3';
$myArray['maximaalformaat'] = '120x5000';
$myArray['breedte'] = '12';
$myArray['hoogte'] = '4';
$myArray['aantal'] = '1';
$myArray['Lijmlaag'] = 'Wit(prijsberekening)+($m2*4)';


$basisstafel=$myArray['basisstaffel'];
$tmp = explode(";", $basisstafel);

 foreach ($tmp as $ind){
     $newArray[]=$ind.';';
 }

echo "<pre>";
  print_r($newArray);
echo "</pre>";
Michel Kapelle
  • 149
  • 1
  • 2
  • 11
1

You can use preg_split and use the PREG_SPLIT_DELIM_CAPTURE flag. This will return matches array with delimiter = 0, match = 1

https://www.php.net/manual/en/function.preg-split.php

PREG_SPLIT_DELIM_CAPTURE If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well.

Darloz
  • 165
  • 11
0
<?php 
$productArray = [
    'basisprijs' => '17,00',
    'basisstaffel' =>' 
    3-10:17;
    10-20:14;
    20-30:12;
    30-40:10;
    40-50:7,50;
    50-60:6,50;
    60-110:6;',
    'minimaalformaat' => '10x3',
    'maximaalformaat' => '120x5000',
    'breedte' => 12,
    'hoogte' => 4,
    'aantal' => 1,
    'Lijmlaag' => 'Wit(prijsberekening)+($m2*4);'
];

$basisstaffel = explode(";", rtrim($productArray['basisstaffel'], ';'));

var_dump($basisstaffel);

Result : array(8) { [0]=> string(14) " 3-10:17" [1]=> string(14) " 10-20:14" [2]=> string(14) " 20-30:12" [3]=> string(14) " 30-40:10" [4]=> string(16) " 40-50:7,50" [5]=> string(16) " 50-60:6,50" [6]=> string(14) " 60-110:6" }
?>
0

It looks like you have newlines after ; so you can preg_split by whitespace:

$result = preg_split('/\s+/', $input['basisstaffel']);

To avoid first empty item add PREG_SPLIT_NO_EMPTY flag:

$result = preg_split('/\s+/', $input['basisstaffel'], -1, PREG_SPLIT_NO_EMPTY);

Or simply use explode with \n or \r\n\ (you must know type of newlines you have):

$result = explode("\n", $input['basisstaffel']);
Stalinko
  • 3,319
  • 28
  • 31