0

I have a multidimensional array of 10000 rows, and each row is like this:

^ array:n [▼
  "static1" => "<HMI screen>"
  "static2" => "Testo ON"
  "static3" => "s+W+OzXRzgfKrp3VB ▶"
  "static4" => ""
  "dynamic1" => "foo"
  "dynamic2" => "bar"
  "dynamic3" => "baz"
  "static5" => null
  "static6" => null
  "static7" => null
  "static8" => null
  "static9" => null
  "static10" => null
  ...
    ]

I need to check the first dynamic data, and in some conditions I need to copy the value in dynamic1 key, in all the others dynamic keys

During the iteration of the big array, in some cases I do something like this:

        $page[$i] = $row;
        $value = $row[$dynamic1];
        foreach($dynamicKeys as $key){
            $page[$i][$key] = $value;
        }

So I overwrite the all the dynamic key values with the firs dynamic key value.

In the $dynamicsKey I have all the keys value of the dynamics keys except the first one, example:

$dynamicKeys = ['dynamic2','dynamic3'..];

Exist a native PHP function to do this:

$value = $page[$i]['dynamic1']; (in most case non everytime)
$page[$i] = magicNativePHPFunction($value,$dynamicKeys);

and the result should be:

^ array:n [▼
  "static1" => "<HMI screen>"
  "static2" => "Testo ON"
  "static3" => "s+W+OzXRzgfKrp3VB ▶"
  "static4" => ""
  "dynamic1" => "foo"
  "dynamic2" => "foo"
  "dynamic3" => "foo"
  "static5" => null
  "static6" => null
  "static7" => null
  "static8" => null
  "static9" => null
  "static10" => null
  ...
    ]
Stefano Martini
  • 408
  • 2
  • 17
  • From my understanding, you want to do something like this - https://stackoverflow.com/questions/15472033/how-to-update-specific-keys-value-in-an-associative-array-in-php – Suryapal Rao Apr 13 '22 at 15:00
  • why do you need magic native php function ? your `foreach($dynamicKeys, ...) { ... }` do exactly what you need, or am i wrong ? – Kazz Apr 13 '22 at 15:10
  • @Kazz yes, but if a one line function exist I prefer to make the code more readable – Stefano Martini Apr 13 '22 at 15:11
  • well there are other ways to do it, like array_map, array_walk but you need closures which will do exactly the same thing as your foreach, so technically the complexity increase a bit therefore readability is bit worst and whats is more important (or should be) their perfomence is way more ineffective because they goes throught every single item in the array, in your case 10000 you will notice this greatly – Kazz Apr 13 '22 at 15:19
  • 1
    maybe what you are really looking for is `array_keys` in combination with `array_filter` to actually get the list of dynamic keys and then use this result in your foreach – Kazz Apr 13 '22 at 15:31

1 Answers1

0

If you need a one-line solution, try this,

$inputArr = [
  // other static keys
  "static2" => "Testo ON",
  "dynamic1" => "foo",
  "dynamic2" => "bar",
  "dynamic3" => "baz"
  // other static keys
];
$dynamic1 = $inputArr["dynamic1"];
$dynamicKeys = ['dynamic2','dynamic3'];

// Maybe this is the line that you are looking for
$outputArr = array_replace($inputArr, array_fill_keys($dynamicKeys, $dynamic1));

print_r($outputArr);

The result will be,

Array
(
    // other static keys
    [static2] => Testo ON
    [dynamic1] => foo
    [dynamic2] => foo
    [dynamic3] => foo
    // other static keys
)
  • just technical and funny comment, the foreach can be written on one line too: `foreach($dynamicKeys as $k) $inputArr[$k] = $dynamic1;` doing exactly the same thing with better performance, but to be fair you actually found the native-magic he was looking for – Kazz Apr 13 '22 at 15:44