Truncating PHP arrays

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi all

I have the following array:

Code: [ Select ]

$browser = array(
  'Firefox 3.0.1',
  'Firefox 3.5.13',
  'Firefox 3.6.12',
  'Internet Explorer 6.0',
  'Internet Explorer 7.0',
  'Internet Explorer 8.0',
  'Safari 525.13',
);
  1. $browser = array(
  2.   'Firefox 3.0.1',
  3.   'Firefox 3.5.13',
  4.   'Firefox 3.6.12',
  5.   'Internet Explorer 6.0',
  6.   'Internet Explorer 7.0',
  7.   'Internet Explorer 8.0',
  8.   'Safari 525.13',
  9. );


I want to truncate the array so that tha numerics are deleted to get:

Code: [ Select ]

$newarray = array(
  'Firefox',
  'Firefox',
  'Firefox',
  'Internet Explorer',
  'Internet Explorer',
  'Internet Explorer',
  'Safari',
);
  1. $newarray = array(
  2.   'Firefox',
  3.   'Firefox',
  4.   'Firefox',
  5.   'Internet Explorer',
  6.   'Internet Explorer',
  7.   'Internet Explorer',
  8.   'Safari',
  9. );


Now to get the totals for each browser I would use:

Code: [ Select ]

$browserout = array_count_values ($newarray);

  1. $browserout = array_count_values ($newarray);


Can anyone give me some ideas as I have not be able to do this

Thanks
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Look into this:
Code: [ Select ]
$newitem = ereg_replace("[^a-zA-Z]", "", $item);


The above code will remove anything that is not matched by the pattern and return it.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi SpooF

Apparently we are supposed to us preg_replace now

I took your idea and set up the following script:

Code: [ Select ]

$new=array();
foreach($browsers as $K=>$A)
{
$Key = preg_replace("[^a-zA-Z]", "", $K);
$ZKey = $browsers[$K];
$new[$ZKey] = $browsers[$Key];
}

echo "<pre>";
print_r ($new);
echo "</pre>";
  1. $new=array();
  2. foreach($browsers as $K=>$A)
  3. {
  4. $Key = preg_replace("[^a-zA-Z]", "", $K);
  5. $ZKey = $browsers[$K];
  6. $new[$ZKey] = $browsers[$Key];
  7. }
  8. echo "<pre>";
  9. print_r ($new);
  10. echo "</pre>";


The output I got is shown below;

Code: [ Select ]

Array
(
  [Firefox 3.0.1] => Firefox 3.0.1
  [Firefox 3.5.13] => Firefox 3.5.13
  [Firefox 3.6.12] => Firefox 3.6.12
  [Internet Explorer 6.0] => Internet Explorer 6.0
  [Internet Explorer 7.0] => Internet Explorer 7.0
  [Internet Explorer 8.0] => Internet Explorer 8.0
  [Safari 525.13] => Safari 525.13
)
  1. Array
  2. (
  3.   [Firefox 3.0.1] => Firefox 3.0.1
  4.   [Firefox 3.5.13] => Firefox 3.5.13
  5.   [Firefox 3.6.12] => Firefox 3.6.12
  6.   [Internet Explorer 6.0] => Internet Explorer 6.0
  7.   [Internet Explorer 7.0] => Internet Explorer 7.0
  8.   [Internet Explorer 8.0] => Internet Explorer 8.0
  9.   [Safari 525.13] => Safari 525.13
  10. )


Can you tell me what I missed here

Thanks
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

preg_replace returns the manipulated string. Also $K is a key, you wan to to use $A in the function.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi SpooF

I did as follows:

Code: [ Select ]

$new=array();
foreach($browsers as $K=>$A)
{
$Key = preg_replace("[^a-zA-Z]", "", $A);
$ZKey = $browsers[$A];
$new[$ZKey] = $browsers[$Key];
}

echo "<pre>";
print_r ($new);
echo "</pre>";
  1. $new=array();
  2. foreach($browsers as $K=>$A)
  3. {
  4. $Key = preg_replace("[^a-zA-Z]", "", $A);
  5. $ZKey = $browsers[$A];
  6. $new[$ZKey] = $browsers[$Key];
  7. }
  8. echo "<pre>";
  9. print_r ($new);
  10. echo "</pre>";


However, my output now is blank:

Code: [ Select ]

Array
(
  [] =>
)
  1. Array
  2. (
  3.   [] =>
  4. )


Am I still missing something

Thanks
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Use the following inside your foreach:

Code: [ Select ]
$new[] = preg_replace("[^a-zA-Z]", "", $A);
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Not sure what you mean

should I replace everything in the foreach loop with your suggestion

Code: [ Select ]

$new=array();
foreach($browsers as $K=>$A)
{
$new[] = preg_replace("[^a-zA-Z]", "", $A);
}
  1. $new=array();
  2. foreach($browsers as $K=>$A)
  3. {
  4. $new[] = preg_replace("[^a-zA-Z]", "", $A);
  5. }
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Yes
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I did that and now get the following:

Code: [ Select ]

Array
(
  [0] => Firefox 3.0.1
  [1] => Firefox 3.0.1
  [2] => Firefox 3.5.13
  [3] => Firefox 3.5.13
  [4] => Firefox 3.6.12
  [5] => Firefox 3.6.12
  [6] => Firefox 3.6.12
  [7] => Internet Explorer 6.0
  [8] => Internet Explorer 6.0
  [9] => Internet Explorer 7.0
  [10] => Internet Explorer 7.0
  [11] => Internet Explorer 8.0
  [12] => Safari 525.13
)
  1. Array
  2. (
  3.   [0] => Firefox 3.0.1
  4.   [1] => Firefox 3.0.1
  5.   [2] => Firefox 3.5.13
  6.   [3] => Firefox 3.5.13
  7.   [4] => Firefox 3.6.12
  8.   [5] => Firefox 3.6.12
  9.   [6] => Firefox 3.6.12
  10.   [7] => Internet Explorer 6.0
  11.   [8] => Internet Explorer 6.0
  12.   [9] => Internet Explorer 7.0
  13.   [10] => Internet Explorer 7.0
  14.   [11] => Internet Explorer 8.0
  15.   [12] => Safari 525.13
  16. )


It did not remove the numbers
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Ah, I see what happened, preg_replace needs a slightly different syntax.

Code: [ Select ]
$new[] = preg_replace("/[^a-zA-Z\ ]/s", "", $A);
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

You can put a trim() around the preg_replace function to remove the trailing white spaces.
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

I did the following

Code: [ Select ]

$new=array();
foreach($basicclean as $K=>$A)
{
$new[] = trim(preg_replace("[^a-zA-Z]", "", $A));
}
  1. $new=array();
  2. foreach($basicclean as $K=>$A)
  3. {
  4. $new[] = trim(preg_replace("[^a-zA-Z]", "", $A));
  5. }


It had no effect, the numbers are still there
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

SpooF wrote:
You can put a trim() around the preg_replace function to remove the trailing white spaces.


Hell, why not just add the numbers and stuff while we're at it? :D

Code: [ Select ]
$new = array();

foreach($old as $key => $val)
{
  $val = strtolower(trim($val, ' .1234567890/'));
  if(empty($new[$val])) $new[$val] = 0;
  $new[$val]++;
}
  1. $new = array();
  2. foreach($old as $key => $val)
  3. {
  4.   $val = strtolower(trim($val, ' .1234567890/'));
  5.   if(empty($new[$val])) $new[$val] = 0;
  6.   $new[$val]++;
  7. }


I'll admit, I was thinking preg_replace too until you mentioned trim(). It's easy to use regular expressions as a go-to. :D
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Tried your suggestion Captain Afro and it worked nicely.

What I also liked was that it also gives the count for each browser which actually saves me a step.

I want to thank you and SpooF for your help

You guys are great

Post Information

  • Total Posts in this topic: 14 posts
  • Users browsing this forum: No registered users and 70 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.