Jump to content

cleaning up data in an array


cyberkiller

Recommended Posts

So I have an array with 10 items in it. How do I go about clearing all spaces before the data?

 

Ex:

Before:        jacob

After: jacob

 

Also some of the data looks like this, World of Warcraft Guide

 

How do I go about checking each value for this   and replacing it with a space if its there?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/50534-cleaning-up-data-in-an-array/
Share on other sites

Putting it all together

 

<?php

$ar = array (
      '        jacob',
      'Esau',
      'World of Warcraft Guide'
    );

foreach ($ar as $k => $val) {
    $ar[$k] = trim(str_replace(' ', ' ', $val));
}

/**
* check results
*/
echo '<pre>', print_r($ar, true), '</pre>';
?>

Doesn't seem to work for me, here is what I used

 

foreach ($parsed as $k => $val) {
    $parsed[0][$k] = trim(str_replace('&#32;', ' ', $val)); }

 

No errors, just that the values still have the spaces and &#32

 

Where the 10 values are in, $parsed[0][0], $parsed[0][1], etc..

I did start out with barand's code, however I don't think he knew I was using a multi array (whatever their called), after playing with it,

 

foreach ($parsed[0] as $k => $val) {
$parsed[0][$k] = trim(str_replace('&#32;', ' ', $val));
}

 

Seems to work, it wasn't barand's fault he just didnt know how my array was setup.

I did start out with barand's code, however I don't think he knew I was using a multi array (whatever their called)...

 

you could have created a recursive function:

<?php
        function stripString($array){
                foreach($array as $key => $val){
                        if(is_array($val)){ stripString($val); }
                        $array[$key] = trim(str_replace(' ', ' ', $val));
                }
        }

        $ar = array (
              '        jacob',
              'Esau',
              'World of Warcraft Guide'
            );

        stripString($ar);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.