php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #20893 Strings as Arrays...
Submitted: 2002-12-08 20:56 UTC Modified: 2003-02-07 15:39 UTC
From: davey at its-explosive dot net Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.3.0RC2 OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: davey at its-explosive dot net
New email:
PHP Version: OS:

 

 [2002-12-08 20:56 UTC] davey at its-explosive dot net
This is probably a strange feature request, but I think it should be given a lot of thought, if it has not done so already.

The request is this:

strings should be treated as arrays of characters (which they are) when given as the arguement for functions which work on arrays.

so for example:
function foo ($chr) {
  echo $chr . "<br />\n";
}
$string = "bar";
array_walk($string,foo); would give the result:
b
a
r

Also, to be able to do this with strings:

$string = "fo";
$string{} = "o";
so that
$string == "foo" == TRUE

Also, another thing that comes up often, is when trying to do this:

$string = "foo";
foreach ($string as $i) {
  echo $string ."<br />\n";
}

does not work, and you have to do:

$string = "foo";
for($i = 0;$i <= strlen($string);$i++) {
  echo $string{$i} ."<br />\n";
}

like I said, give this some thought, perhaps toss it around on php-dev etc..

- Davey

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-08 20:57 UTC] davey at its-explosive dot net
small code correction:

$string = "foo";
foreach ($string as $i) {
  echo $i ."<br />\n";
}
 [2003-02-07 15:39 UTC] iliaa@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at 
http://www.php.net/downloads.php

In PHP there will be a function, str_split(), this function can be used to break down a string into an array, allowing you to quickly traverse the said array and apply any mods on it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 14:01:28 2024 UTC