I've got an array called $bigArrayWithLinks that stores a string with a url in each element. Here's a sample from a var_dump:
array
0 => string 'http://en.wikipedia.org/wiki/England' (length=36)
1 => string 'http://www.bbc.co.uk/news/england/' (length=34)
2 => string 'http://www.thefa.com/' (length=21)
3 => string 'http://www.thefa.com/England/' (length=29)
What I want to do is iterate through the array, adding an integer of value '0' to each element so it becomes
array
0 => string 'http://en.wikipedia.org/wiki/England' => int '0'
1 => string 'http://www.bbc.co.uk/news/england/' => int '0'
2 => string 'http://www.thefa.com/' => int '0'
3 => string 'http://www.thefa.com/England/' => int '0'
I tried:
for($x=0; $x<sizeof($arr); $x++)
{
$score = $arr[$x]['score'];
$score = '0';
}
I'm very new to php so I wasn't amazed that it didn't work. Could someone help me out please? Thanks in advance!
You are confused about PHP arrays.
An array is an index (or hash when using associative arrays) and it has 1(!) value. That value CAN be another array.
You second example (what you wish) just shows:
0 => string 'http://en.wikipedia.org/wiki/England' => int '0'
That doesn't make sense.
I EXPECT what you are really looking for is some structure like this:
$mySiteScore = array(
array('url'=>'http://en.wikipedia.org/wiki/England', 'score' =>0),
array('url'=>'http://www.bbc.co.uk/news/england/', 'score' =>0),
array('url'=>'http://www.thefa.com/', 'score' =>0)
);
That way you create an array of associative arrays. An associative array can use a descriptive key, like 'url' or 'score'. Now, if you want to add all scores:
$totalScore = 0;
foreach ($mySiteScore as $oneSiteScore){
$totalScore = $totalScore + $oneSiteScore['score'];
}
Or alternatively: create two seperate arrays: one with the URLs, the other with the scores. But you must make sure the indexes match.
Just make another 1D array of zeros, indices would be the same.
In almost all programming languages, "new dimension" is impossible without reallocating and copying to new container.
foreach($bigArrayWithLinks as $key => $url) {
$bigArrayWithLinks[$key] = array('url' => $url, 'x' => 0);
}