i have array built like this
array (size=2)
0 =>
array (size=12)
'id_objects' => string '2876' (length=4)
'room_val' => string '1882840,1882841,1882842' (length=23)
'date_from' => string '2022-06-22' (length=10)
'date_to' => string '2022-06-22' (length=10)
1 =>
array (size=12)
'id_objects' => string '2876' (length=4)
'room_val' => string '3198723,3198724,3198726' (length=23)
'date_from' => string '2022-06-22' (length=10)
'date_to' => string '2022-06-22' (length=10)
What i need to achieve is , to explode 'room_val' but keep rest of the data same , it needs to look like this
array (size=2)
0 =>
array (size=12)
'id_objects' => string '2876' (length=4)
'room_val' => string '1882840' (length=23)
'date_from' => string '2022-06-22' (length=10)
'date_to' => string '2022-06-22' (length=10)
1 =>
array (size=12)
'id_objects' => string '2876' (length=4)
'room_val' => string '1882841' (length=23)
'date_from' => string '2022-06-22' (length=10)
'date_to' => string '2022-06-22' (length=10)
..... rest of array
Is it possible?
You need to parse on your array of arrays and create a new array that match what you are looking for.
Something like:
$myNewArray = [];
foreach ($arrays as $array) {
$room_vals = $array['room_val'];
foreach ($room_vals as $room_val) {
$newSubArray = $array;
$newSubArray['room_val'] = $room_val;
$myNewArray[] = $newSubArray;
}
}
Related
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 got a array of array in PHP that look like this :
array (size=3)
0 =>
array (size=3)
0 => string 'abc' (length=3)
1 => string 'def' (length=3)
2 => string 'ghi' (length=3)
1 =>
array (size=3)
0 => string '01234' (length=5)
1 => string '01234' (length=5)
2 => string '01234' (length=5)
2 =>
array (size=3)
0 => string '98765' (length=5)
1 => string '98765' (length=5)
2 => string '98765' (length=5)
Now I want the first array to be the key of a assosiative array for the rest of the parent array, or kind :
array (size=2)
0 =>
array (size=3)
'abc' => string '01234' (length=5)
'def' => string '01234' (length=5)
'ghi' => string '01234' (length=5)
1 =>
array (size=3)
'abc' => string '98765' (length=5)
'def' => string '98765' (length=5)
'ghi' => string '98765' (length=5)
EDIT: But I can only get the first array like this to define the header :
$header = reset($tabOfTabs);
You can try this -
$indexes = array_shift($your_array); // pop out the first array to set the indexes
foreach($your_array as $key => $array) {
$your_array[$key] = array_combine($indexes, $array); // combine the keys & sub-arrays
}
Demo
How to my array combine :
$visitor_tracvking_data = json_encode($array);
print_r($visitor_tracvking_data);
Output print_r this:
array (size=6)
0 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'direct' => string '261' (length=3)
1 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'social_media' => string '3' (length=1)
2 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'search' => string '3' (length=1)
3 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'chrome' => string '168' (length=3)
4 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'firefox' => string '68' (length=2)
5 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'netscape' => string '31' (length=2)
How to out put this type:-
array (size=2)
0 =>
array (size=2)
'tracktitle' => string 'search_engine' (length=13)
'direct' => string '261' (length=3)
'social_media' => string '3' (length=1)
'search' => string '3' (length=1)
1 =>
array (size=2)
'tracktitle' => string 'browser' (length=7)
'chrome' => string '168' (length=3)
'firefox' => string '68' (length=2)
'netscape' => string '31' (length=2)
Just loop through the array and generate a new array with tracktitle key -
$new = array();
foreach($array as $key_top => $value) {
foreach($value as $key => $val) {
$new[$value['tracktitle']][$key] = $val;
}
}
return $new;
OR for json encoded string -
return json_encode($new);
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);
}
I have array like this
array (size=6)
0 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '231' (length=3)
'document_title' => string 'Renstra 2014-2018 Distamben COVER' (length=33)
'document_date' => string '2014-10-15 14:09:00' (length=19)
'site' => string 'Propinsi Nusa Tenggara Barat' (length=28)
'url_download' => string 'http://ntbprov.sip-ppid.net/index.php/document/download/231' (length=59)
1 =>
array (size=1)
0 =>
array (size=5)
'document_id' => string '7' (length=1)
'document_title' => string 'Data Menara 2014' (length=16)
'document_date' => string '2015-01-08 13:05:00' (length=19)
'site' => string 'Kabupaten Bima' (length=14)
'url_download' => string 'http://bimakab.sip-ppid.net/index.php/document/download/7' (length=57)
how to sort by document_date? I try with usort() but not working.
I am Newbie.