I have the following array that I want to modify the key for another value that I have in another array:
$respuesta = array(
0=> array:16 (
0=> null
1=> "1313"
2=> "01/01/2015"
3=> "01/01/2015"
4=> "27/04/1983"
5=> "01"
6=> "41821550"
7=> "ACOSTA LOPEZ LUIS GIANCARLO"
8=> "604311LALSE0"
9=> null
10=> null
11=> "0"
12=> "03"
13=> "AFP PROFUTURO"
14=> null
15=> null
16=> "4342.50"
)
1=> array(
0=> null 1=> ""
2=> "04/05/2015"
3=> "04/05/2015"
4=> "01/01/01"
5=> "01"
6=> "41414242"
7=> "AGUIRRE VALDERRAMA DANIEL"
8=> ""
9=> null
10=> null
11=> "1"
12=> "08"
13=> "O.N.P."
14=> null
15=> null
16=> null
)
)
And in this other array I have the values that I want to color in the keys of my array:
$claves= array(
0 => "l_ubig"
1 => "n_cusp"
2 => "f_ingr"
3 => "f_inic"
4 => "f_naci"
5 => "c_docu"
6 => "n_docu"
7 => "xl_nomc"
8 => "c_autg"
9 => "k_cont"
10 => "l_cont"
11 => "Situacion"
12 => "c_afp"
13 => "l_afp"
14 => "xp_totafp"
15 => "01001"
16 => "01002"
)
And the result I want to get is the following:
$respuesta = array(
0=> array:16 (
l_ubig=> null
n_cusp=> "1313"
f_ingr=> "01/01/2015"
f_inic=> "01/01/2015"
f_naci=> "27/04/1983"
c_docu=> "01"
n_docu=> "41821550"
xl_nomc=> "ACOSTA LOPEZ LUIS GIANCARLO"
c_autg=> "604311LALSE0"
k_cont=> null
l_cont=> null
Situacion=> "0"
c_afp=> "03"
l_afp=> "AFP PROFUTURO"
xp_totafp=> null
01001=> null
01002=> "4342.50"
)
1=> array(
l_ubig=> null
n_cusp=> ""
f_ingr=> "04/05/2015"
f_inic=> "04/05/2015"
f_naci=> "01/01/01"
c_docu=> "01"
n_docu=> "41414242"
xl_nomc=> "AGUIRRE VALDERRAMA DANIEL"
c_autg=> ""
k_cont=> null
l_cont=> null
Situacion=> "1"
c_afp=> "08"
l_afp=> "O.N.P."
xp_totafp=> null
01001=> null
01002=> null
)
)
I have been trying to do it with the function array_combine()
, which creates a new array, using one array for the keys and another for their values.
I have been doing it in the following way, I don't know if it is correct:
$result = array_combine($claves, $respuesta);
But I get the following error:
Error_Exception: array_combine{}: Both parameters should have an equal number of elements.
I hope you can help me how to get the result I want and thank you very much in advance.
You must do the array merging within each of the response array elements, and merging with the values. I show it in the following example with a
var_dump
of the resulting array.Explanation of the problem
You're trying to apply
array_combine
to$respuesta
, which is an array with two elements which, in turn, are the array you actually want to apply that change to.Proposed solution
You must iterate through each element of
$respuesta
and each element is the one you want to change the indices to.You can do it in the following way:
full example
A complete example:
And the output:
array_combine
has restrictions that could be very problematic.$claves
have more or fewer elements than any of the sub-arrays, the code will not return the expected results.$claves
the code will fail.In those cases a solution like this could be feasible because here
$new_array
only those keys that are found in are created in$claves
.Test when they are all the same:
Departure:
Try having
$claves
more elementsDeparture:
You can also try removing elements from
$respuesta
. In no case will the code work unexpectedly.