0

I couldn't find it on Google, or used the wrong keyword. How can i merge a variable value "[contestantvariable_number-fieldname]" to a multidimensial array?

Example of the array:

array (
  [contestant1-gender] => M
  [contestant1-first-name] => Wyatt
  [contestant1-last-name] => Heath
  [contestant1-occupation] => Ipsa architecto vol
  [contestant1-email] => [email protected]
  [contestant1-phone] => +1 (992) 741-8123
  [contestant2-gender] => M
  [contestant2-first-name] => Leonard
  [contestant2-last-name] => Acosta
  [contestant2-occupation] => Aut sunt qui offici
  [contestant2-email] => [email protected]
  [contestant2-phone] => +1 (462) 687-8393
  [department] => In mollitia impedit
  [street] => Sed error magnam obc
  [number] => 41
  [bus] => In iste ut perspicia
  [postalcode] => Commodo impedit vol
  [city] => Ad iure veniam veli
  [country] => nl
)

To something like this:

Array (
   [0] => contestant (
     [0] => gender ( )
     [1] => first-name ()
   )
   [1] => contestant (
     [0] => gender ( )
     [1] => first-name ()
   )
   [2] => contestant (
     [0] => gender ( )
     [1] => first-name ()
   )
)
4
  • 1
    Have you tried anything so far? Commented May 10, 2019 at 19:48
  • you can use the function array_merge_multidimesional you just have to write it first :D Commented May 10, 2019 at 19:50
  • What are the () in the result supposed to be? Commented May 10, 2019 at 20:31
  • See stackoverflow.com/questions/33854400/… and stackoverflow.com/questions/51573147/… for methods to convert keys like contestant-gender to nested arrays. Commented May 10, 2019 at 20:33

1 Answer 1

1

I think this at least approximates that for which you are looking:

$array = [
   'contestant-1-gender' => 'M',
   'contestant-1-first-name' => 'Wyatt',
   'contestant-1-last-name' => 'Heath',
   'contestant-1-occupation' => 'Bl;ah blah',
   'contestant-2-gender' => 'M',
   'contestant-2-first-name' => 'Leonard',
   'contestant-2-last-name' => 'Acosta',
   'contestant-2-occupation' => 'Foo',
   'department' => 'Who cares?'
];

$contestants = [];
array_walk($array, function($val, $key) use (&$contestants) {
   if (preg_match('/contestant\-(?P<id>[\d+])\-(?P<field>.*)/', $key, $matches))
   {
      $contestants[$matches['id']][$matches['field']] = $val;
   }
});

print_r($contestants);

Output:

Array
(
    [1] => Array
        (
            [gender] => M
            [first-name] => Wyatt
            [last-name] => Heath
            [occupation] => Bl;ah blah
        )

    [2] => Array
        (
            [gender] => M
            [first-name] => Leonard
            [last-name] => Acosta
            [occupation] => Foo
        )

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

Comments

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.