Multiple foreach loops and accessing values outside

I'm trying to access multiple values in multiple foreach loops outside of the loops:

foreach(array1 as arr1) {
    $var1 = arr2['value1'];  //$array is associative array with mutliple keys value1
}

Then I have another

foreach(array2 as arr2) {
    $var2 = arr2['value']; //$array2 is another associative array with multiple keys value
}

All of this is within another big foreach loop and now I want to create an array within the big foreach con $var1 y $var2 being used. This array I'm going to be creating is going to be an associative array as well. Any ideas how I can do this?

Matriz 1:

Array
(
  [0] => Array
    (
        [id] => 1
        [id_name] => 251452
        [name] => bob
    )

[1] => Array
    (
        [id] => 2
        [id_name] => 251453
        [name] => bob

    )

)

Matriz 2:

Array
(
[0] => Array
    (
        [id_person] => 4
        [id_last_name] => 251452
        [last_name] => smith

    )

[1] => Array
    (
        [id_person] => 15
        [id_last_name] => 251453
        [last_name] => johnson
    )

)

Assume these come from two different queries from the database.

I want to get the first name from the first array for each one and get the last name from the second array for each one and make one array that has this data along with others.

preguntado el 24 de agosto de 12 a las 00:08

It would help if we had an idea about what your source array(s) and your expected output should be. It's entirely possible that your inner foreach loops could benefit from using the key/value style: foreach($array1 as $key1 => $value1) { ... } -

no key=>value wont help me ill try to update my question to make it better -

1 Respuestas

Have a look into multi dimensional arrays.

Also have a look at stdclass and maybe creating an array of these which can store many variables within a single definition - which can help in many ways. (standard class)

Nested looping - this would just dump all sub arrays into an object // Object and array examples for an InnerArray

 $Object = new stdclass();
 $AllOfIt = array();
 $cnt = 0;
 foreach($OuterArray as $OuterKey => $InnerArray)
 {
      $cnt++;
      foreach($InnerArray as $InnerKey => $InnerValue)
      {
           $Object->$cnt = $InnerValue;
           $AllOfIt[$cnt] = $InnerValue;
      }
 }

Respondido 24 ago 12, 00:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.