I have an array ($myArray) which I want to alter:
array(2) {
[0]=>
array(2) {
[0]=>
string(3) "100"
[1]=>
string(9) "fancyLink"
}
[1]=>
array(2) {
[0]=>
string(3) "200"
[1]=>
string(10) "fanyLink2"
}
}
I have two POST arrays which have the following structure.
First array $number:
array(2) {
[0]=>
string(3) "100"
[1]=>
string(3) "200"
}
and the second array $links:
array(2) {
[0]=>
string(12) "newFancyLink"
[1]=>
string(13) "newFancyLink2"
}
To alter the $myArray in order to replace one or more links according to the $number-value I wanted to use the following line
for($i = 0; $i < 2; $i++)
{
$myArray[array_search($number[$i], array_column($myArray, "0"))][1] = $links[$i];
}
But this does not work, it sets both links of 100 and 200 to the same value (the second link)
I wrote this code which works, but I would like to use the other line instead or at least compare the perfomance of it
for($i = 0; $i < 2; $i++)
{
for($j = 0; $j < 2; $j++)
{
if($myArray[$j][0] == $number[$i])
{
$myArray[$j][1] = $links[$i];
}
}
}
Using the $number-value as a key would make this a lot easier but this is not an option for me.
EDIT after Solving
In case anybody wants to know, both versions are alost identical fast. 10000 runs each result in this runtime:
0.10158801078796
0.10160994529724