Joak Posted February 21, 2014 Share Posted February 21, 2014 HelloI 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 Quote Link to comment Share on other sites More sharing options...
WebStyles Posted February 21, 2014 Share Posted February 21, 2014 take a look at array_merge() Quote Link to comment Share on other sites More sharing options...
Joak Posted February 24, 2014 Author Share Posted February 24, 2014 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 } ]}} Quote Link to comment Share on other sites More sharing options...
Zane Posted February 24, 2014 Share Posted February 24, 2014 You haven't provided any code whatsoever for us to use to point out the error. Quote Link to comment Share on other sites More sharing options...
Joak Posted February 24, 2014 Author Share Posted February 24, 2014 This is my code$equipojug = array_slice($equipo,0,3,true)+array("Jugadores"=>$jugador);$equipo and $jugador are my two arrays Quote Link to comment Share on other sites More sharing options...
WebStyles Posted February 24, 2014 Share Posted February 24, 2014 (edited) 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 February 24, 2014 by Zane Quote Link to comment Share on other sites More sharing options...
Zane Posted February 24, 2014 Share Posted February 24, 2014 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. Quote Link to comment Share on other sites More sharing options...
Joak Posted February 24, 2014 Author Share Posted February 24, 2014 Hello WebStyles The results... //Jugadores ArrayArray( [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 ArrayArray( [0] => Array ( [id_equipo] => 7 [nom_equipo] => Vodka Juniors [locvis] => L )) Quote Link to comment Share on other sites More sharing options...
Joak Posted February 24, 2014 Author Share Posted February 24, 2014 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 } ]}} Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 24, 2014 Share Posted February 24, 2014 (edited) 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 February 24, 2014 by Psycho Quote Link to comment Share on other sites More sharing options...
Zane Posted February 24, 2014 Share Posted February 24, 2014 This went from being an array to being a JSON object, I'm just as confused now as I was to begin with. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted February 24, 2014 Share Posted February 24, 2014 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); ?> Quote Link to comment Share on other sites More sharing options...
Joak Posted February 24, 2014 Author Share Posted February 24, 2014 Thanks PsychoThe problem it's when I want to convert to JSON formatwith 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} Quote Link to comment Share on other sites More sharing options...
Zane Posted February 24, 2014 Share Posted February 24, 2014 We understand what you want; a multidimensional array. Provide all of your code, maybe that will help? Quote Link to comment Share on other sites More sharing options...
Joak Posted February 24, 2014 Author Share Posted February 24, 2014 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" Quote Link to comment Share on other sites More sharing options...
Solution Psycho Posted February 24, 2014 Solution Share Posted February 24, 2014 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>"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.