Jump to content


Photo

two arrays in one

array

Best Answer Psycho, 24 February 2014 - 01:43 PM

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>";
Go to the full post


  • Please log in to reply
15 replies to this topic

#1 Joak

Joak

    Member

  • Members
  • PipPip
  • 20 posts

Posted 21 February 2014 - 04:02 PM

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

 

         

 



#2 WebStyles

WebStyles

    Advanced Member

  • Members
  • PipPipPip
  • 1,215 posts
  • LocationEurope

Posted 21 February 2014 - 04:09 PM

take a look at array_merge()


Need a Web Developer? Check out ClickWorks.org | I work on a PAY-WHAT-YOU-WANT basis (because the world needs a change)

 

"If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family anatidae on our hands!"

-- Douglas Adams


#3 Joak

Joak

    Member

  • Members
  • PipPip
  • 20 posts

Posted 24 February 2014 - 12:28 PM

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
      }
  ]
}
}



#4 Zane

Zane

    Advanced Member

  • Administrators
  • 3,865 posts
  • LocationMurphy, NC

Posted 24 February 2014 - 12:32 PM

You haven't provided any code whatsoever for us to use to point out the error.


btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank

401299.png

#5 Joak

Joak

    Member

  • Members
  • PipPip
  • 20 posts

Posted 24 February 2014 - 12:36 PM

This is my code
$equipojug = array_slice($equipo,0,3,true)+array("Jugadores"=>$jugador);

$equipo and $jugador are my two arrays



#6 WebStyles

WebStyles

    Advanced Member

  • Members
  • PipPipPip
  • 1,215 posts
  • LocationEurope

Posted 24 February 2014 - 12:46 PM

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, 24 February 2014 - 12:48 PM.

Need a Web Developer? Check out ClickWorks.org | I work on a PAY-WHAT-YOU-WANT basis (because the world needs a change)

 

"If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family anatidae on our hands!"

-- Douglas Adams


#7 Zane

Zane

    Advanced Member

  • Administrators
  • 3,865 posts
  • LocationMurphy, NC

Posted 24 February 2014 - 12:47 PM

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.


btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank

401299.png

#8 Joak

Joak

    Member

  • Members
  • PipPip
  • 20 posts

Posted 24 February 2014 - 12:52 PM

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
        )

)



#9 Joak

Joak

    Member

  • Members
  • PipPip
  • 20 posts

Posted 24 February 2014 - 01:00 PM

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
    }
  ]
}
}



#10 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,738 posts
  • LocationCanada

Posted 24 February 2014 - 01:04 PM

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, 24 February 2014 - 01:06 PM.

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#11 Zane

Zane

    Advanced Member

  • Administrators
  • 3,865 posts
  • LocationMurphy, NC

Posted 24 February 2014 - 01:10 PM

This went from being an array to being a JSON object, I'm just as confused now as I was to begin with.


btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank

401299.png

#12 WebStyles

WebStyles

    Advanced Member

  • Members
  • PipPipPip
  • 1,215 posts
  • LocationEurope

Posted 24 February 2014 - 01:18 PM

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);
?>

Need a Web Developer? Check out ClickWorks.org | I work on a PAY-WHAT-YOU-WANT basis (because the world needs a change)

 

"If it looks like a duck, and quacks like a duck, we have at least to consider the possibility that we have a small aquatic bird of the family anatidae on our hands!"

-- Douglas Adams


#13 Joak

Joak

    Member

  • Members
  • PipPip
  • 20 posts

Posted 24 February 2014 - 01:19 PM

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}



#14 Zane

Zane

    Advanced Member

  • Administrators
  • 3,865 posts
  • LocationMurphy, NC

Posted 24 February 2014 - 01:24 PM

We understand what you want; a multidimensional array.

Provide all of your code, maybe that will help?


btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank

401299.png

#15 Joak

Joak

    Member

  • Members
  • PipPip
  • 20 posts

Posted 24 February 2014 - 01:31 PM

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"



#16 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,738 posts
  • LocationCanada

Posted 24 February 2014 - 01:43 PM   Best Answer

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>";

The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com