I want to change the key of my array in php.
Here an exemple :
array (size=5)
0 =>
array (size=2)
'iden' => string '01' (length=8)
'don' => string '17' (length=2)
1 =>
array (size=2)
'iden' => string '02' (length=8)
'don' => string '17' (length=2)
2 =>
array (size=2)
'iden' => string '03' (length=8)
'don' => string '17' (length=2)
And I want to change my array like this :
array (size=5)
0 =>
array (size=2)
0 => string '01' (length=8)
1 => string '17' (length=2)
1 =>
array (size=2)
0 => string '02' (length=8)
1 => string '17' (length=2)
2 =>
array (size=2)
0 => string '03' (length=8)
1 => string '17' (length=2)
Thanks in advance
You can use the array_values function to remove named keys:
foreach($array as &$item) {
$item = array_values($item);
}
unset($item); // Remove reference
Note the & in the foreach. This creates a reference in the $item variable to the corresponding array element which means you can edit it in your loop.
If you want, you can also write this in a single line using array_map:
$array = array_map("array_values", $array);
This question already has answers here:
PHP append one array to another (not array_push or +)
(11 answers)
Closed 2 years ago.
I have the following associative array:
0 =>
array (size=2)
'id' => string '0000' (length=4)
'polling_id' => string '0' (length=1)
1 =>
array (size=2)
'id' => string '0001' (length=4)
'polling_id' => string '1' (length=1)
And I have this second associative array:
0 =>
array (size=3)
'id' => string '0002' (length=4)
'polling_id' => string '0' (length=1)
'backup_id' => string '4500' (length=4)
1 =>
array (size=3)
'id' => string '0003' (length=4)
'polling_id' => string '0' (length=1)
'backup_id' => string '4500' (length=4)
I want it to look like:
0 =>
array (size=2)
'id' => string '0000' (length=4)
'polling_id' => string '0' (length=1)
1 =>
array (size=2)
'id' => string '0001' (length=4)
'polling_id' => string '1' (length=1)
2 =>
array (size=3)
'id' => string '0002' (length=4)
'polling_id' => string '0' (length=1)
'backup_id' => string '4500' (length=4)
3 =>
array (size=3)
'id' => string '0003' (length=4)
'polling_id' => string '0' (length=1)
'backup_id' => string '4500' (length=4)
How can I achieve this?
Try array_merge($a1,$a2)to merge these two arrays.
I have array and i nees access to key:
var_dump($load);
array (size=18)
'4' =>
array (size=3)
0 => string '12.3%' (length=5)
1 => string '17.8%' (length=5)
2 => string '11.8%' (length=5)
'9' =>
array (size=3)
0 => string '5.0%' (length=4)
1 => string '8.2%' (length=4)
2 => string '8.9%' (length=4)
'24' =>
array (size=3)
0 => string '53.1%' (length=5)
1 => string '86.1%' (length=5)
2 => string '53.6%' (length=5)
'7' =>
array (size=3)
0 => string '95.1%' (length=5)
1 => string '90.1%' (length=5)
2 => string '80.7%' (length=5)
Hove a can get data on key?
var_dump($load["7"]);
var_dump($load["'7'"]);
var_dump($load['"7"']);
var_dump($load[7]);
All var_dump return null;
Var dump array keys, Keys match.
$keys=array_keys($load);
var_dump($keys);
array (size=18)
0 => string '14' (length=2)
1 => string '60' (length=2)
2 => string '19' (length=2)
3 => string '7' (length=1)
4 => string '15' (length=2)
5 => string '24' (length=2)
6 => string '1' (length=1)
7 => string '16' (length=2)
8 => string '5' (length=1)
9 => string '11' (length=2)
10 => string '17' (length=2)
11 => string '18' (length=2)
12 => string '3' (length=1)
13 => string '6' (length=1)
14 => string '2' (length=1)
15 => string '10' (length=2)
16 => string '9' (length=1)
17 => string '4' (length=1)
This question already has answers here:
PHP, Merging arrays with common keys
(2 answers)
Closed 6 years ago.
I have two arrays:
First
array (size=6)
0 =>
array (size=2)
'Age' => string '25-34' (length=5)
'Count' => string '45' (length=2)
1 =>
array (size=2)
'Age' => string '55-64' (length=5)
'Count' => string '1' (length=1)
2 =>
array (size=2)
'Age' => string '13-17' (length=5)
'Count' => string '3' (length=1)
3 =>
array (size=2)
'Age' => string '35-44' (length=5)
'Count' => string '11' (length=2)
4 =>
array (size=2)
'Age' => string '18-24' (length=5)
'Count' => string '46' (length=2)
5 =>
array (size=2)
'Age' => string '45-54' (length=5)
'Count' => string '2' (length=1)
Second:
array (size=5)
0 =>
array (size=2)
'Age' => string '65+' (length=3)
'Count' => string '1' (length=1)
1 =>
array (size=2)
'Age' => string '13-17' (length=5)
'Count' => string '4' (length=1)
2 =>
array (size=2)
'Age' => string '35-44' (length=5)
'Count' => string '3' (length=1)
3 =>
array (size=2)
'Age' => string '25-34' (length=5)
'Count' => string '11' (length=2)
4 =>
array (size=2)
'Age' => string '18-24' (length=5)
'Count' => string '20 |' (length=4)
Now what here I am getting is that First array size is larger than Second one, so I need a solution for making the small size array similar to larger size array.
With same keys, and add value zero to new added keys value.
array_merge( $firstArray, $secondArray );
Please see array_merge();
I've been trying to work with the array_search function.
This is my array :
array (size=23)
0 =>
array (size=3)
0 => string '15' (length=2)
1 => float 115.08386533184
2 => string '2014-02-06 21:00:00' (length=19)
1 =>
array (size=3)
0 => string '81' (length=2)
1 => float 100.41587590619
2 => string '2014-03-28 00:00:00' (length=19)
2 =>
array (size=3)
0 => string '65' (length=2)
1 => float 99.096448338334
2 => string '2014-02-08 21:00:00' (length=19)
3 =>
array (size=3)
0 => string '53' (length=2)
1 => float 98.752479251378
2 => string '2014-03-05 12:00:00' (length=19)
4 =>
array (size=3)
0 => string '24' (length=2)
1 => float 98.303557178126
2 => string '2014-02-07 21:00:00' (length=19)
5 =>
array (size=3)
0 => string '23' (length=2)
1 => float 98.270536817788
2 => string '2014-02-08 21:30:00' (length=19)
6 =>
array (size=3)
0 => string '37' (length=2)
1 => float 98.139812350661
2 => string '2014-02-06 23:00:00' (length=19)
7 =>
array (size=3)
0 => string '13' (length=2)
1 => float 97.810954038756
2 => string '2014-02-09 19:45:00' (length=19)
8 =>
array (size=3)
0 => string '22' (length=2)
1 => float 95.975221202728
2 => string '2014-02-11 20:30:00' (length=19)
9 =>
array (size=3)
0 => string '66' (length=2)
1 => float 94.115075316114
2 => string '2014-02-04 19:00:00' (length=19)
10 =>
array (size=3)
0 => string '52' (length=2)
1 => float 93.957291067159
2 => string '2014-03-15 00:00:00' (length=19)
11 =>
array (size=3)
0 => string '12' (length=2)
1 => float 89.188128768086
2 => string '2014-05-17 10:00:00' (length=19)
12 =>
array (size=3)
0 => string '51' (length=2)
1 => float 88.381986169995
2 => string '2014-03-27 12:00:00' (length=19)
13 =>
array (size=3)
0 => string '98' (length=2)
1 => float 85.240471279545
2 => string '2014-02-17 18:00:00' (length=19)
14 =>
array (size=3)
0 => string '92' (length=2)
1 => float 82.721601210972
2 => string '2014-02-06 20:15:00' (length=19)
15 =>
array (size=3)
0 => string '82' (length=2)
1 => float 82.473535719129
2 => string '2014-02-27 17:00:00' (length=19)
16 =>
array (size=3)
0 => string '121' (length=3)
1 => float 56.833974620724
2 => string '2014-02-07 22:30:00' (length=19)
17 =>
array (size=3)
0 => string '120' (length=3)
1 => float 36.269423317467
2 => string '2014-02-06 21:30:00' (length=19)
18 =>
array (size=3)
0 => string '83' (length=2)
1 => float 35.863780709688
2 => string '2014-02-21 07:00:00' (length=19)
19 =>
array (size=3)
0 => string '35' (length=2)
1 => float 24.322967350005
2 => string '2014-02-07 21:30:00' (length=19)
20 =>
array (size=3)
0 => string '6' (length=1)
1 => float 19.71405484708
2 => string '2014-02-14 22:00:00' (length=19)
21 =>
array (size=3)
0 => string '45' (length=2)
1 => float 19.142717452481
2 => string '2014-03-15 00:00:00' (length=19)
22 =>
array (size=3)
0 => string '117' (length=3)
1 => float 17.641278106673
2 => string '2014-02-18 17:30:00' (length=19)
Now - The thing is I'm using array_search in order to get the key of a specific ID (Note : The ID is the first value in each array). The thing is, it doesn't work, probably because it can't get the key of the array itself.
Let me show you an example : ($array = The array above)
$r = array_search(53, array_keys($array));
I want $r to be equal to 3, because thats they array's key position.
Any idea how I can do it?
$r = array_search('53', array_map(function($x) { return $x[0]; }, $array));
array_map will construct an array of element 0 of each element of the array. Then you search for 53 in this, and $r will be its position.
Or you could simply write a loop:
foreach ($array as $i => $el) {
if ($el[0] == '53') {
$r = $i;
$result = $el;
break;
}
}
I searched SO and found a similar topic: PHP Multidimensional Array - Search for value and get the sub-array.
I slightly modified one of the solutions provided above.
$id=53;
$r=null;
$data=null;
foreach ($array as $idx=>$subArray) {
if ( $subArray[0] == $id ) {
$r=$idx;
$target=$subArray;
break;
}
}
use array_map function and array_filter function.
function find($a, $id) {
return ($a[0] == $id) ? $a : false;
}
function filter($a) {
return $a;
}
$r = array_filter(array_map("find", $array, array(53)), "filter");
A better call than array_map() to isolate the first column of values is: array_column(). This is the task that the function was created for.
Use this implementation:
$r = array_search('53', array_column($array,0));
Done.