Jump to content

[SOLVED] Splitting Arrays based on first characters


Recommended Posts

Hi everybody,

 

  I am not getting anywhere with this. Could somebody throw me a pointer into the right direction? I have an array with a whole bunch of phrases. To make it easier for my user to navigate through this huge list I want to do split that array based on the first characters of the words. So I need a function that goes through the array and collects all the text strings that _start_ with "A" and puts them into a new array. I also have a special case where I need to find all the text strings that start with "Color ".

 

  What function would be best to use here?

 

Greetings,

 

Napper

 

 

try

<?php
$ar = array (
      'A stitch in time saves nine',
      'Birds of a feather flock together',
      'All that glisters is not gold',
      'Bat and ball',
      'Color red',
      'A fool and his money are soon parted',
      'Color yellow'
);

$phrases = array();

foreach ($ar as $line)
{
    if (strpos($line, 'Color')===0)
    {
        $phrases['color'][] = $line;
    }
    else {
        $phrases[$line[0]][] = $line;          // $line[0] is first char of the line
    }
}

echo '<pre>', print_r($phrases, true), '</pre>';
?>  

What can I say Barand? Beautiful! I was just fiddling around with Zivs array_walk idea, when your post poppled up. I rebuilt the function now to be case-insensitive:

 

<?php
$phrases = array();



foreach ($array_all_keywords as $line) {

    if (stripos($line, 'a')===0) { $phrases['A'][] = $line; }
    elseif (stripos($line, 'b')===0) { $phrases['B'][] = $line; }

    elseif (stripos($line, 'c')===0) { $phrases['C'][] = $line; }
    elseif (stripos($line, 'd')===0) { $phrases['D'][] = $line; }
    elseif (stripos($line, 'e')===0) { $phrases['E'][] = $line; }
    elseif (stripos($line, 'f')===0) { $phrases['F'][] = $line; }
    elseif (stripos($line, 'g')===0) { $phrases['G'][] = $line; }
    elseif (stripos($line, 'h')===0) { $phrases['H'][] = $line; }
    elseif (stripos($line, 'i')===0) { $phrases['I'][] = $line; }
    elseif (stripos($line, 'j')===0) { $phrases['J'][] = $line; }
    elseif (stripos($line, 'k')===0) { $phrases['K'][] = $line; }
    elseif (stripos($line, 'l')===0) { $phrases['L'][] = $line; }
    elseif (stripos($line, 'm')===0) { $phrases['M'][] = $line; }
    elseif (stripos($line, 'n')===0) { $phrases['N'][] = $line; }
    elseif (stripos($line, 'o')===0) { $phrases['O'][] = $line; }
    elseif (stripos($line, 'p')===0) { $phrases['P'][] = $line; }
    elseif (stripos($line, 'q')===0) { $phrases['Q'][] = $line; }
    elseif (stripos($line, 'r')===0) { $phrases['R'][] = $line; }
    elseif (stripos($line, 's')===0) { $phrases['S'][] = $line; }
    elseif (stripos($line, 't')===0) { $phrases['T'][] = $line; }
    elseif (stripos($line, 'u')===0) { $phrases['U'][] = $line; }
    elseif (stripos($line, 'v')===0) { $phrases['V'][] = $line; }
    elseif (stripos($line, 'w')===0) { $phrases['W'][] = $line; }
    elseif (stripos($line, 'x')===0) { $phrases['X'][] = $line; }
    elseif (stripos($line, 'y')===0) { $phrases['Y'][] = $line; }
    elseif (stripos($line, 'z')===0) { $phrases['Z'][] = $line; }
    else { $phrases['#'][] = $line; }

}


echo '<pre>', print_r($phrases, true), '</pre>';
?>

 

A bit more tricky however, is the next bit. I want the programm to bundle Strings that start the same (basically strings that are the same until the first emtpy space) in order to manipulate of how they are displayed. Let's say you have that array:

 

<?php

$ar = array (
      'A stitch in time saves nine',
      'Birds of a feather flock together',
      'All that glisters is not gold',
      'type Hat',
      'Bat and ball',
      'Color red',
      'Type trouser',
      'A fool and his money are soon parted',
      'Color yellow',
      'color green',
      'type baggy'
);
?>

 

And you want to find all the Colors and types, except that you don't know that the strings you are looking for start with "Color " or "Type" you just want all the instances that start the same bundled together. The occurance of strings that start the same is user dependent, so I don't know, which words to search for. Trying to figure this out right now. Any ideas?

<?php

$ar = array (
      'A stitch in time saves nine',
      'Birds of a feather flock together',
      'All that glisters is not gold',
      'type Hat',
      'Bat and ball',
      'Color red',
      'Type trouser',
      'A fool and his money are soon parted',
      'Color yellow',
      'color green',
      'type baggy'
);
foreach ($ar as $s){
$k = explode(' ',$s);
$k = $k[0];
$k = strtolower($k); //remove for case sensitiv
$out[$k][] = $s;
}
foreach ($out as $k => $s){
if (count($s) == 1){
	$out[substr($k, 0, 1)][]=$s[0];
	unset ($out[$k]);
}
}
print_r($out);
?>

Hi Sasa! Thanks for the quick help. This is almost working the way I need it. I will post my complete code once it is done (still a few quirks, because the actual code is more complex...)

 

Still, this is already doing wonders. Problem definetly solved!

 

Kind regards,

 

Napper

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.