FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


array question





golles
how can I add information to all array items ie:
Quote:
<?php
$add = 'test';

$item[0] = $item[0] . $add;
$item[1] = $item[1] . $add;
$item[2] = $item[2] . $add;
?>


I think that could be done much easier??? all ideas are welcome (maybe example??)

I made the array with the str_split() function.

thanks in advance
hexkid
Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
header('Content-Type: text/plain');

$item = array('one', 'two', 'three');
print_r($item);

$item2 = $item;
foreach ($item2 as &$x) {
  $x .= ' test';
}
print_r($item2);

$item3 = $item;
function example(&$v, $k, $x) {
  $v .= $x;
}
array_walk($item3, 'example', ' test');
print_r($item3);
?>


Links
array_walk()
foreach()
References Explained
golles
hexkid wrote:
Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
header('Content-Type: text/plain');

$item = array('one', 'two', 'three');
print_r($item);

$item2 = $item;
foreach ($item2 as &$x) {
  $x .= ' test';
}
print_r($item2);

$item3 = $item;
function example(&$v, $k, $x) {
  $v .= $x;
}
array_walk($item3, 'example', ' test');
print_r($item3);
?>


Links
array_walk()
foreach()
References Explained


I found out about the foreach function on php.net, but now I understand it. thanks hexkid!!
sonam
Code:
<?php

$add = "test";
$item = array('one', 'two', 'three');

print_r($item);  print  "<br>";

foreach($item as $key => $val) {
$item[$key] = $item[$key] . $add;
}

print_r($item);
?>


Results:
Quote:
Array ( [0] => one [1] => two [2] => three ) // before
Array ( [0] => onetest [1] => twotest [2] => threetest ) // after


Sonam
degenre
hexkid, please can you explain to me the use of & in the foreach loop.. What exact purpose does the & server?
Code:
foreach ($item2 as &$x) {
  $x .= ' test';
}
hexkid
degenre wrote:
hexkid, please can you explain to me the use of & in the foreach loop.. What exact purpose does the & server?
Code:
foreach ($item2 as &$x) {
  $x .= ' test';
}


Let's see ... ignore the "&" for a bit ...

foreach() operates on copies of the array elements.
For the same array I gave earlier as $info, the foreach() would be something like
Code:
### first time through the loop
$x = $info[0]; // 'one' copied to $x
$x .= ' test'; // does not change $info[0]
### next loop
$x = $info[1]; // 'two' copied to $x
$x .= ' test'; // does not change $info[1]
### and so on


The "&" makes PHP create a reference instead
Code:
### first time through the loop
$x = &$info[0]; // $x is another name for $info[0]
$x .= ' test'; // changes $info[0]
### next loop
$x = &$info[1]; // $x is another name for $info[1]
$x .= ' test'; // changes $info[1]
### and so on



Here's a simpler example
Code:
<?php
$x = 'hex';
$y = $x; // $y is a copy of $x
$z = &$x; // $z is a reference to $x

### changing the copy does not change the original
$y .= 'kid';
echo "x = $x, y = $y<br>\n";

### changing the reference also changes the original
$z .= 'kid';
echo "x = $x, z = $z<br>\n";
?>
golles
it works on my localhost, but on frihost it gives me errors Confused
Quote:
Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /home/golles/public_html/powerball/powerball.php on line 26



heres the code:
Code:
<?php
$stand = $_GET['stand'];

if (empty($stand)){
//empty stand empty images
   $stand = 'lllll';
}
elseif(!is_numeric($stand)){
//no numbers ?'s
   $stand = 'qqqqq';
}

$split = str_split($stand);

do{
//bring empty values to the front
  if ($split[4] == ''){
     $split[4] = $split[3];
     $split[3] = $split[2];
     $split[2] = $split[1];
     $split[1] = $split[0];
     $split[0] = l;
  }
}while ($split[4] == '');

foreach ($split as &$arr) {
//change values to image
   $arr = '<img src="Images/'. $arr .'.jpg" />';
}
?>

<form method="get" action="<?php $_SERVER['PHP_SELF']; ?>">
   <input type="text" maxlength="5" name="stand" />
   <input type="submit" value="Stand invoeren" />
</form>
<img src="Images/boven.jpg" /><br />
<img src="Images/mlinks.jpg" /><?php echo $split[0] . $split[1] . $split[2] . $split[3] . $split[4]; ?><img src="Images/mrechts.jpg" /><br />
<img src="Images/onder.jpg" />


I only edited the comments, the files on my local webserver and frihost server are the same.
can you tell me whats wrong??? Confused Confused Confused Confused Confused Confused

PS tips/feedback about my code is really really welcome, but please pm me then so we don't mess up this topic (ty)
hexkid
golles wrote:
it works on my localhost, but on frihost it gives me errors Confused
Quote:
Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /home/golles/public_html/powerball/powerball.php on line 26

That's because frihost uses PHP 4.4.4 which doesn't support the reference in the foreach() construct.

Use one of the other options.


I'm very short of time right now. I'll have a better look at your code later.
golles
I finally got the array_walk() working (foreach was much easier)
Code:
function arrimg(&$arr) {
  $arr = '<img src="Images/'. $arr .'.jpg" />';
}
array_walk($split, 'arrimg');


but now I get another error
Quote:

Fatal error: Call to undefined function: str_split() in /home/golles/public_html/powerball/powerball.php on line 13


now I need to find an alternative for str_split()
maybe I can better try something else for the image, anyone a good tutorial or idea???
hexkid
golles wrote:
now I need to find an alternative for str_split()


Supposing you want to str_split() to isolate characters, include this file in your script and continue to use str_split() as if it existed Smile

Code:
<?php
if (!function_exists('str_split')) {
  function str_split($str, $len) {
    if ($len != 1) return false;
    return preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
  }
}
?>
Related topics
I Have a question . Is it the server support Chinese?
Question before requesting free hosting
question abt posts/mth
Kinda dumb question regarding DNS
A question
UT2004 U like the game? Do u have any question post here.
Question about DNS!
Question about Domain!
another uni.cc question
Question
Implementing your database access in PHP as a layer.
php multidimension array question
php or python question about some processing.
Using an if statement inside a string variable
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.