0

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

2 Answers 2

2

Perhaps array_column($myArray, "0") should be array_column($myArray, 0) instead (since it is not an associative array)? That way it seems to work for me.

From the PHP-manual

array array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )

column_key

The column of values to return. This value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. It may also be NULL to return complete arrays or objects (this is useful together with index_key to reindex the array).

Sign up to request clarification or add additional context in comments.

2 Comments

Hm, Because PHP has only associative arrays and numeric arrays pretend only to be numeric I thought it would not matter, see this post: stackoverflow.com/questions/37854038/php-numeric-array-order But thank you, I will look into it
I checked, your answer works. PHP is strange sometimes, at least to me :D I accepted your answer as it solved my particular problem, thank you :)
1

The solution using array_map and array_walk functions:

$numLinks = array_map(null, $number, $links);
// you may also iterate via a regular 'foreach' loop
array_walk($myArray, function(&$v, $k) use($numLinks){
    if ($v[0] == $numLinks[$k][0]) $v[1] = $numLinks[$k][1];
});

print_r($myArray);

The output:

Array
(
    [0] => Array
        (
            [0] => 100
            [1] => newFancyLink
        )
    [1] => Array
        (
            [0] => 200
            [1] => newFancyLink2
        )
)

3 Comments

Hm, this may work but I already have some working code and am simply curios why my first try is not working because as far as I understood it it should...
this may work - It works fine. As for your code, the second argument for the array_column function - this value may be an integer key of the column you wish to retrieve, or it may be a string key name for an associative array or property name. But you have specified an integer key as a string
Hm, Because PHP has only associative arrays and numeric arrays pretend only to be numeric I thought it would not matter, see this post: stackoverflow.com/questions/37854038/php-numeric-array-order But thank you, I will look into it :) PHP is strange sometimes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.