Jump to content

two arrays in one


Joak
Go to solution Solved by Psycho,

Recommended Posts

Hello

I have two arrays, and both I want to convert in one like this structure .... both arrays are Equipos array and Jugadores array, how can I do?

 

Regards

 

 

 

Equipos

  0

    id_equipo : 5

    nom_equipo: San Blas

    locvis: L

    Jugadores

      0

        id_jugador : 1

        name: Robert

 

         

 

Link to comment
Share on other sites

Hello

 

 
I still unresolved my case .... I use the array_slice function, but "Jugadores" it's wrong level I want to put down "locvis" level .....I get this array ..

{
  "0": {
    "id_equipo": "7",
    "nom_equipo": "Vodka Juniors",
    "locvis": "L"
  },
  "Jugadores": [
    {
      "id_jugador": "10",
      "jugador_nom": "Mario Jaxiel",
      "jugador_pat": null,
      "jugador_mat": "Vargas",
      "jug_repre": "N",
      "jug_playera": "898",
      "fechareg": null
    },
    {
      "id_jugador": "6",
      "jugador_nom": "Misael Yahir",
      "jugador_pat": null,
      "jugador_mat": "Morlan",
      "jug_repre": "N",
      "jug_playera": "1",
      "fechareg": null
    }
  ]
}

 

The correct array

 

{
  "0": {
    "id_equipo": "7",
    "nom_equipo": "Vodka Juniors",
    "locvis": "L",
    "Jugadores": [
      {
        "id_jugador": "10",
        "jugador_nom": "Mario Jaxiel",
        "jugador_pat": null,
        "jugador_mat": "Vargas",
        "jug_repre": "N",
        "jug_playera": "898",
        "fechareg": null
      },
      {
        "id_jugador": "6",
        "jugador_nom": "Misael Yahir",
        "jugador_pat": null,
        "jugador_mat": "Morlan",
        "jug_repre": "N",
        "jug_playera": "1",
        "fechareg": null
      }
  ]
}
}

Link to comment
Share on other sites

please use this code and post the results (so we can see what your arrays look like BEFORE you manipulate them)

echo '<pre>';
print_r($jugador);
print_r($equipo);
echo '</pre>';

(sorry, edited post because I forgot to put the variables inside the print_f() print_r() functions)

Edited by Zane
Link to comment
Share on other sites

The plus sign (+) is used for arithmetic statements, not for concatenation like Javascript.

 

array_slice is going to do exactly as it's name suggests.. Slice the array.

If you simply want to add an array to an array, then this should work

$equipo['Jugadores'] = $jugador;

Still, you haven't provided enough code for an effective answer.

Link to comment
Share on other sites

Hello WebStyles

 

The results...

 

//Jugadores Array
Array
(
    [0] => Array
        (
            [id_jugador] => 12
            [jugador_nom] => Omar
            [jugador_pat] => Ortiz
            [jugador_mat] => Flores
            [jug_repre] => N
            [jug_playera] => 16
            [fechareg] =>
        )

    [1] => Array
        (
            [id_jugador] => 1
            [jugador_nom] => Francisco
            [jugador_pat] => Rojas
            [jugador_mat] => Ortega
            [jug_repre] => N
            [jug_playera] => 17
            [fechareg] =>
        )
)
//Equipo Array
Array
(
    [0] => Array
        (
            [id_equipo] => 7
            [nom_equipo] => Vodka Juniors
            [locvis] => L
        )

)

Link to comment
Share on other sites

Zane .... I have gotten the same result .... Jugadores doesen't part for the Equipo array

 

$equipo['Jugadores'] = $jugador;

 

{
  "0": {
    "id_equipo": "7",
    "nom_equipo": "Vodka Juniors",
    "locvis": "L"
  },
  "Jugadores": [
    {
      "id_jugador": "12",
      "jugador_nom": "Omar",
      "jugador_pat": "Ortiz",
      "jugador_mat": "Flores",
      "jug_repre": "N",
      "jug_playera": "16",
      "fechareg": null
    }
  ]
}

 

I want this....

 

{
  "0": {
    "id_equipo": "7",
    "nom_equipo": "Vodka Juniors",
    "locvis": "L",
  "Jugadores": [
    {
      "id_jugador": "12",
      "jugador_nom": "Omar",
      "jugador_pat": "Ortiz",
      "jugador_mat": "Flores",
      "jug_repre": "N",
      "jug_playera": "16",
      "fechareg": null
    }
  ]
}
}

Link to comment
Share on other sites

What is the difference between the two you just posted?!

 

Anyway, does this give you what you are wanting?

$newArray = $equipo;
$newArray['Jugadores'] = $jugador;
echo "<pre>" . print_r($newArray, 1) . "</pre>";

Edit: this would be the same as you just posted where you appended $jugador to the $equipo array. So, I guess it isn't what you are looking for. But, again, you state that doesn't give you what you want and then post the same thing as what you do want. I'm confused.

Edited by Psycho
Link to comment
Share on other sites

The code Zane gave you about 5 posts ago should work nicely. I re-created your arrays so you can see it working, because (as Zane pointed out) we're not sure if they're arrays or json objects. try this and tell us if it's what you wanted: (I'm assuming you want ALL the players to be in the team ?)

 

<?php
$equipo = array(
'id_equipo' => '7',
'nom_equipo' => 'Vodka Juniors',
'locvis' => 'L'
);
$jugadores = array(
0 => array(
	'id_jugador' => '12',
	'jugador_nom' => 'Omar',
	'jugador_pat' => 'Ortiz',
	'jugador_mat' => 'Flores',
	'jug_repre' => 'N',
	'jug_playera' => '16',
	'fechareg' =>''),

1 => array(
	'id_jugador' => '1',
	'jugador_nom' => 'Francisco',
	'jugador_pat' => 'Rojas',
	'jugador_mat' => 'Ortega',
	'jug_repre' => 'N',
	'jug_playera' => '17',
	'fechareg' =>'')
);

$equipo['Jugadores'] = $jugadores;

echo '<pre>';
print_r($equipo);
?>
Link to comment
Share on other sites

Thanks Psycho

The problem it's when I want to convert to JSON format

with your code I got this...
{JSON}
 {0}
   id_equipo:"7"
   nom_equipo:"Vodka Juniors"
   locvis:"L"
 Jugadores
   {0}
   {1}



And I want this format ..... notice Jugadores it's another level of my first array(Equipos)
{JSON}
 {0}
   id_equipo:"7"
   nom_equipo:"Vodka Juniors"
   locvis:"L"
   Jugadores
     {0}
     {1}

Link to comment
Share on other sites

WEBSTYLES ... you right .... ALL are player's Vodka team .. they have to exist in the first level

 

{JSON}

 {0}

    id_equipo:"7"

    nom_equipo:"Vodka Juniors"

    locvis: "L"

    [ ]Jugadores

       {0}

         id_jugador:"1"

         jugador_nom:"Juan Carlos"

Link to comment
Share on other sites

  • Solution

Why is the data for $equipo in a subarray for the index 0? Unless there are multiple 'records' I would put the data at the root of the array

 

array {

    id_equipo => "7"

    nom_equipo => "Vodka Juniors"

    locvis => "L"

}

 

But, if you want to use the current format, the use this:

 

$newArray = $equipo;
$newArray[0]['Jugadores'] = $jugador;
echo "<pre>" . print_r($newArray, 1) . "</pre>";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.