I wish to add string keys to my inner PHP arrays. So, I want to convert this:
array (size=2)
0 => array (size=3)
0 => string 'X705' (length=4)
1 => string 'X723' (length=4)
2 => string 'Sue' (length=0)
1 => array (size=3)
0 => string 'X714' (length=4)
1 => string 'X721' (length=4)
2 => string 'John' (length=0)
to this:
array (size=2)
0 =>
array (size=3)
'code1' => string 'X705' (length=4)
'code2' => string 'X723' (length=4)
'name' => string 'Sue' (length=0)
1 =>
array (size=3)
'code1' => string 'X714' (length=4)
'code2' => string 'X721' (length=4)
'name' => string 'John' (length=0)
I think I need to use array_walk but cannot fathom it out. Any help appreciated.
You can use array_map for that purpose:
$newarray = array_map(function($x) {
return array("code1" => $x[0], "code2" => $x[1], "name" => $x[2]);
}, $array);
where $array is your input array.
Start with this:
foreach ($array as $key=>$item) {
$item['code1']=$item[0];
unset($item[0]);
$item['code2']=$item[1];
unset($item[1]);
$item['name']=$item[2];
unset($item[2]);
$array[$key]=$item;
}
I would use array_map() but here's an alternate:
foreach($array as &$v) {
$v = array_combine(array('code1','code2','name'), $v);
}
Related
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);
I have an array of arrays like this one below and I want to convert it to object array.
array (size=3)
'declaration' =>
array (size=99)
'GO_IMPZONK_ID' => string '130334' (length=6)
'ID' => string '19802862' (length=8)
'CUSTE' => string '10100' (length=5)
'DCLEXP' => null
'DCL_BROKER_CODE' => string '' (length=0)
'RLCCODE' => string '' (length=0)
'items' =>
array (size=1)
0 =>
array (size=50)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TARIFYEAR' => string '85' (length=2)
'extensions' =>
array (size=6)
0 =>
array (size=5)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TOTVALUE' => string '0.00' (length=4)
'EXPDATE' => string '2004-03-20' (length=10)
1 =>
array (size=5)
'GO_IMPDCL_ID' => string '19802862' (length=8)
'TOTVALUE' => string '0.00' (length=4)
'EXPDATE' => string '2004-03-20' (length=10)
I did try casting it like this but it only makes Object with arrays inside.
$obj=(object)$array;
what can i do to have nesting multi level Objects from my array?
i improvised another way to do so:
$ar=[
'a'=>[
'field1'=>52,
'field2'=>52,
'field3'=>52,
],
'b'=>[
'field1'=>52,
'field2'=>52,
'field3'=>52,
]
];
function ToObj($data) {
if (gettype($data) == 'array')
return (object)array_map("ToObj", $data);
else
return $data;
}
$ObjectResult = array_map("ToObj", $ar);
I have this result from var_dump for a multidimensional array:
array (size=6)
'sambalpur.in.net' =>
array (size=2)
'classkey' => string 'indotnet' (length=8)
'status' => string 'available' (length=9)
'sambalpur.com' =>
array (size=2)
'classkey' => string 'domcno' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.info' =>
array (size=2)
'classkey' => string 'dominfo' (length=7)
'status' => string 'regthroughothers' (length=16)
'sambalpur.net' =>
array (size=2)
'classkey' => string 'dotnet' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.biz' =>
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)
'sambalpur.in' =>
array (size=2)
'classkey' => string 'dotin' (length=5)
'status' => string 'regthroughothers' (length=16)
Now say I want to shift this specific array to the beginning of array:
'sambalpur.biz' =>
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)
I have tried:
array_unshift($array,array('sambalpur.biz'));
But what I am getting is like this:
array (size=7)
0 =>
array (size=1)
0 => string 'sambalpur.biz' (length=13)
'sambalpur.in.net' =>
array (size=2)
'classkey' => string 'indotnet' (length=8)
'status' => string 'available' (length=9)
'sambalpur.com' =>
array (size=2)
'classkey' => string 'domcno' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.info' =>
array (size=2)
'classkey' => string 'dominfo' (length=7)
'status' => string 'regthroughothers' (length=16)
'sambalpur.net' =>
array (size=2)
'classkey' => string 'dotnet' (length=6)
'status' => string 'regthroughothers' (length=16)
'sambalpur.biz' =>
array (size=2)
'classkey' => string 'dombiz' (length=6)
'status' => string 'available' (length=9)
'sambalpur.in' =>
array (size=2)
'classkey' => string 'dotin' (length=5)
'status' => string 'regthroughothers' (length=16)
What is the correct way to shift the array?
I thought I had done this before but couldn't find a duplicate:
$array = array_splice($array,
array_search('sambalpur.biz', array_keys($array)), 1) + $array;
Get a numerically indexed array of the keys with array_keys()
Search the returned array for sambalpur.biz with array_search()
Use the returned index to cut out that element with array_splice()
Add that to the existing array
Along the same line as Don't Panic:
$array = array_merge(array('sambalpur.biz' => $array['sambalpur.biz']), $array);
No need to unset as the order of insertion dictates which key overwrites the other so this one overwrites the previous one.
You can uksort the array, using the specific key you want to move to the beginning in the comparison function.
$key = 'sambalpur.biz';
uksort($array, function($a, $b) use ($key) {
if ($a == $key) return -1;
if ($b == $key) return 1;
return 0;
});
This should move that item to the beginning without changing the order of the other items.
Another possibility is to remove the child array, and then merge the main array back onto it.
$key = 'sambalpur.biz';
$x = $array[$key];
unset($array[$key]);
$array = array_merge([$key => $x], $array);
The issue seems to stem from array_unshift() reindexing elements you are passing. If you wanted to prepend your second array to your first array though and preserve indexes, you can use the + operator ($firstArray = $secondArray + $firstArray);
I'm really stuck at a certain point in my project. I'm trying to convert a two-dimensional array into a three-dimensional array. Currently I´m at this point:
array (size=4)
0 =>
array (size=4)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
1 =>
array (size=4)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
2 =>
array (size=4)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '1' (length=1)
3 =>
array (size=4)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
3 => string '0' (length=1)
What I would like to do is make a new array with key values of array[ ][3] (I hope this makes any sense) so something like this:
array (size=2)
0 => array (size=2)
0 => array (size=2)
0 => string 'foo' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'tas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => array (size=2)
0 => string 'bar' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
1 => array (size=2)
0 => string 'bas' (length=3)
1 => string 'a' (length=0)
2 => string 'b' (length=0)
Right now this is the best way I can explain it, I'm not sure if I make any sense or if this is even possible, but if anyone could shed a light on this it would be much appreciated!
Thanks in advance,
Thomas
$array = array();
$array[] = array('foo','foo','foo',1);
$array[] = array('a','a','a',1);
$array[] = array('b','a','a',0);
$array[] = array('c','a','a',0);
$new_array =group_by_sub_array_key($array,3);
print_r($new_array);
function group_by_sub_array_key($array,$kn){
$new_array = array();
foreach($array AS $v){
$key=$v[$kn];
if(!array_key_exists($v[$kn],$new_array)){
$new_array[$key]=array();
}
unset($v[$kn]);
$new_array[$key][] = $v;
}
return $new_array;
}
I have a array:
$array1 = array
0 =>
array
0 => string 'biodata' (length=7)
1 => string 'family_name' (length=11)
1 =>
array
0 => string 'biodata' (length=7)
1 => string 'first_name' (length=10)
2 =>
array
0 => string 'biodata_education' (length=17)
1 => string 'subject' (length=7)
3 =>
array
0 => string 'biodata_education' (length=20)
1 => string 'year' (length=5)
which need to converted like:
array
biodata =>
array
0 => string 'family_name' (length=7)
1 => string 'first_name' (length=11)
biodata_education =>
array
0 => string 'subject' (length=7)
1 => string 'year' (length=10)
as it can be done by simple iteration, I tried this one and done.
foreach($array1 as $tbl):
$table[$tbl[0]][] = $tbl[1];
endforeach;
<?php
//map the array using a foreach loop
foreach($array1 as $tbl)
{
$table[ $tbl[0] ][] = $tbl[1];
}