M
M
Mykola2014-06-13 21:59:25
PHP
Mykola, 2014-06-13 21:59:25

How to connect 2 associative arrays?

Hello! There are 2 arrays:

array (size=8)
  0 => 
    array (size=1)
      'characters' => 
        array (size=2)
          'name' => string 'Ginko' (length=5)
          'url' => string '/character/425/Ginko' (length=20)
  1 => 
    array (size=1)
      'characters' => 
        array (size=2)
          'name' => string 'Isaza' (length=5)
          'url' => string '/character/24126/Isaza' (length=22)
  2 => 
    array (size=1)
      'characters' => 
        array (size=2)
          'name' => string 'Narrator' (length=8)
          'url' => string '/character/39597/Narrator' (length=25)
  3 => 
    array (size=1)
      'characters' => 
        array (size=2)
          'name' => string 'Rokusuke' (length=8)
          'url' => string '/character/104353/Rokusuke' (length=26)
  4 => 
    array (size=1)
      'characters' => 
        array (size=2)
          'name' => string 'Toki' (length=4)
          'url' => string '/character/105349/Toki' (length=22)
  5 => 
    array (size=1)
      'characters' => 
        array (size=2)
          'name' => string 'Mina' (length=4)
          'url' => string '/character/105473/Mina' (length=22)
  6 => 
    array (size=1)
      'characters' => 
        array (size=2)
          'name' => string 'Sakichi' (length=7)
          'url' => string '/character/105501/Sakichi' (length=25)
  7 => 
    array (size=1)
      'characters' => 
        array (size=2)
          'name' => string 'Tatsu' (length=5)
          'url' => string '/character/105703/Tatsu' (length=23)

array (size=8)
  0 => 
    array (size=1)
      'characters' => 
        array (size=1)
          'role' => string 'Main' (length=4)
  1 => 
    array (size=1)
      'characters' => 
        array (size=1)
          'role' => string 'Supporting' (length=10)
  2 => 
    array (size=1)
      'characters' => 
        array (size=1)
          'role' => string 'Supporting' (length=10)
  3 => 
    array (size=1)
      'characters' => 
        array (size=1)
          'role' => string 'Supporting' (length=10)
  4 => 
    array (size=1)
      'characters' => 
        array (size=1)
          'role' => string 'Supporting' (length=10)
  5 => 
    array (size=1)
      'characters' => 
        array (size=1)
          'role' => string 'Supporting' (length=10)
  6 => 
    array (size=1)
      'characters' => 
        array (size=1)
          'role' => string 'Supporting' (length=10)
  7 => 
    array (size=1)
      'characters' => 
        array (size=1)
          'role' => string 'Supporting' (length=10)

You need to connect them so that it turns out:
array (size=8)
  0 => 
    array (size=1)
      'characters' => 
        array (size=2)
          'name' => string 'Ginko' (length=5)
          'url' => string '/character/425/Ginko' (length=20)
          'role' => string 'Main' (length=10)
  1 => 
    array (size=1)
      'characters' => 
        array (size=2)
          'name' => string 'Isaza' (length=5)
          'url' => string '/character/24126/Isaza' (length=22)
          'role' => string 'Supporting' (length=10)
..........................................................

I tried the array_merge_recursive function, but it simply joins the arrays by adding the 2nd array to the end of the first one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikola, 2014-06-13
@iSensetivity

Solved like this:
if(count($chr) == count($r)){
for($i = 0; $i< count($chr); $i++){
$result[] = array_merge_recursive($chr[$ i], $r[$i]);
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question