Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,608 members, 7,809,230 topics. Date: Friday, 26 April 2024 at 06:07 AM

Getting The First Character Of Array Values, Php ? - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Getting The First Character Of Array Values, Php ? (6495 Views)

How To Load Form Inputs Into An Array. / Php Quiz: And A Short Array Issue :: Copy Array Values Into Key / Why Does This Mysql Query Display "array" Instead Of The Result (2) (3) (4)

(1) (Reply) (Go Down)

Getting The First Character Of Array Values, Php ? by sayhi2ay(m): 10:01pm On Nov 10, 2010
anyone has a better way for achieving this ?

$data = array('dayo','tobi','john');
$firstChar = array();
for ($i=0; $i < count($data); $i++){
$firstChar[$i] = $data[$i][0];
}

for($letter = ord('a'); $letter <= ord('z'); $letter++) {

if (in_array(chr($letter), $firstChar)) {
print chr($letter);
echo "<br/>";
}
}


just need a better function cos my $data set has more than 100k records, want something faster to look through the array instead of parsing through it and then using in_array
Re: Getting The First Character Of Array Values, Php ? by sayhi2ay(m): 10:32pm On Nov 10, 2010
maybe something faster? here's what i have now.
thanks.

function firstChar($string)
{
return($string[0]);
}

$data = array('dayo','tobi','john');
$data = array_map("firstChar", $data);

for($letter = ord('a'); $letter <= ord('z'); $letter++) {

if (in_array(chr($letter), $data)) {
print chr($letter);
echo "<br/>";
}
}
Re: Getting The First Character Of Array Values, Php ? by DualCore1: 6:13am On Nov 11, 2010
$my_array = array("buy","a","bad", "egg"wink;

for($i=0; $i <= count($my_array); $i++){
$first_char = $my_array[$i]{0};
echo $first_char;
}


hope this helps somehow, boss smiley
Re: Getting The First Character Of Array Values, Php ? by sayhi2ay(m): 3:19pm On Nov 11, 2010
thanks dualcore.
my main goal is to check if letters a to z are the first letter of my data array. i have a search function that returns a list of names depending on your search string.  so say i search 'ay' in my database, it should return all the names that contains 'ay', then my snippet above will return the list of first letters of my record set. that way, i can make a list of A-Z, and just make the whatever letter i got from my snippet an active link.

so you search 'ay', you get your result.

then you can click an active link from A-Z.

say D is an active link, then the search condition will be , from the search 'ay' result set, find the records that starts with 'D'.

if the first search returns this list:
Ayo
Layo
Dayo
Tayo
Mayo
Morayo
Tunrayo
Desayo

then my snippet will return A, D, L, M, T

so when i echo letters A - Z, only A,D,L,M and T will be active links. and when you click on D, the search result = 'Dayo', 'Desayo'

(1) (Reply)

How To Monetize Your Blog & Make Money / What Does Mysql Database Means? / How To Solve Adsense Showing 403 Forbidden Error {steps}

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 9
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.