php - multidimensional array (keep index array) - php

I have array like this:
array:2 [▼
0 => array:3 [▼
0 => array:1 [▼
"2018-06-13" => "hadir"
]
1 => array:1 [▼
"2018-06-12" => "hadir"
]
2 => array:1 [▼
"2018-06-11" => "alfa"
]
]
1 => array:3 [▼
0 => array:1 [▼
"2018-06-13" => "hadir"
]
1 => array:1 [▼
"2018-06-12" => "hadir"
]
2 => array:1 [▼
"2018-06-11" => "hadir"
]
]
]
But I want convert this array to:
array:2 [▼
0 => array:3 [▼
"2018-06-13" => "hadir"
"2018-06-12" => "hadir"
"2018-06-11" => "alfa"
]
1 => array:3 [▼
"2018-06-13" => "hadir"
"2018-06-12" => "hadir"
"2018-06-11" => "hadir"
]
]
I have tried my own solutions using things like array_merge, array_walk_recursive, and RecursiveIteratorIterator with RecursiveArrayIterator. But in my practice it doesn't work.
Help Me

Hard part was re-creating the original array :)
Loop through the top level array with a counter
Each element of that array is an indexed array. Loop through each of htem with a counter.
Each of those elements is an associative array containing the data you want to use as keys/values in the new array. Pop through each of those wtih a foreach and build the new array, top level being indexed on $i and each of those elements containing your associative array.
<?php
$arr[0][]=array('2018-06-13'=>"hadir");
$arr[0][]=array('2018-06-12'=>"hadir");
$arr[0][]=array('2018-06-11'=>"alfa");
$arr[1][]=array('2018-06-13'=>"hadir");
$arr[1][]=array('2018-06-12'=>"hadir");
$arr[1][]=array('2018-06-11'=>"hadir");
print_r($arr);
for($i=0;$i<count($arr);$i++){
for($j=0;$j<count($arr[$i]);$j++){
foreach($arr[$i][$j] as $k=>$v){
$newarr[$i][$k]=$v;
}
}
}
print_r($newarr);
?>

Here are a couple of clean/direct approaches using the splat operator (...). Both effectively "shift the deepest subarrays up a level" by merging the individual subarrays.
Code: (Demo) *use one approach or the other depending on style preference
$array = [
[
['2018-06-13' => "hadir"],
['2018-06-12' => "hadir"],
['2018-06-11' => "alfa"]
],
[
['2018-06-13' => "hadir"],
['2018-06-12' => "hadir"],
['2018-06-11' => "hadir"]
]
];
var_export(
array_reduce(
$array,
function ($carry, $item) {
$carry[] = array_merge(...$item);
return $carry;
},
[]
)
);
echo "\n---\n";
foreach ($array as &$sub1) { // modify by reference
$sub1 = array_merge(...$sub1);
}
var_export($array);
Output:
array (
0 =>
array (
'2018-06-13' => 'hadir',
'2018-06-12' => 'hadir',
'2018-06-11' => 'alfa',
),
1 =>
array (
'2018-06-13' => 'hadir',
'2018-06-12' => 'hadir',
'2018-06-11' => 'hadir',
),
)
---
array (
0 =>
array (
'2018-06-13' => 'hadir',
'2018-06-12' => 'hadir',
'2018-06-11' => 'alfa',
),
1 =>
array (
'2018-06-13' => 'hadir',
'2018-06-12' => 'hadir',
'2018-06-11' => 'hadir',
),
)

Related

how to get first key (0 or associative ) form array and output in new array

I Would Like To Get The First Element Of This Array And Put In New Same Array Output
One Requirement: It Cannot Be Done With Passing By reference Index eg 0
This Input Array
[ 'id','ID','dt-text' ] ,
[ 'name','Name','dt-text' ] ,
[ 'artistList'=>['list','mm','defalut'] ,'Artist List','dt-select'] ,
[ 'nationality'=>['nationality','mm','defalut'] ,'Nationality','dt-select'] ,
[ 'view','View',''],
[ 'status','Status' ,'']
array:6 [▼
0 => array:3 [▼
0 => "id"
1 => "ID"
2 => "dt-text"
]
1 => array:3 [▼
0 => "name"
1 => "Name"
2 => "dt-text"
]
2 => array:3 [▼
"artistList" => array:3 [▼
0 => "list"
1 => "mm"
2 => "defalut"
]
0 => "Artist List"
1 => "dt-select"
]
3 => array:3 [▼
"nationality" => array:3 [▼
0 => "nationality"
1 => "mm"
2 => "defalut"
]
0 => "Nationality"
1 => "dt-select"
]
4 => array:3 [▼
0 => "view"
1 => "View"
2 => ""
]
5 => array:3 [▼
0 => "status"
1 => "Status"
2 => ""
]
]
The New Array I Needed
This IS OutPUT Array
['id','name','artistList'=>['list','mm','defalut'] ,'nationality'=>['nationality','mm','defalut'] ,'view','status']
array:6 [▼
0 => "id"
1 => "name"
"artistList" => array:3 [▼
0 => "list"
1 => "mm"
2 => "defalut"
]
"nationality" => array:3 [▼
0 => "nationality"
1 => "mm"
2 => "defalut"
]
2 => "view"
3 => "status"
]
Note
I Can Controll in Input Array Same , I Try with foreach in php And Tray In Laravel Helper Function head Put I get S
array:6 [▼
0 => "id"
1 => "name"
2 => array:1 [▼
"artistList" => array:3 [▼
0 => "list"
1 => "mm"
2 => "defalut"
]
]
3 => array:1 [▼
"nationality" => array:3 [▼
0 => "nationality"
1 => "mm"
2 => "defalut"
]
]
4 => "view"
5 => "status"
]
Put I Cant Get Resslut So , How Can I Do this?
Since you are changing the keys (structure) of the array, there is no way to do that without either generating a new array or passing the array by reference. One way to do it by generating a new array is with array_reduce:
$array = [
[ 'id','ID','dt-text' ] ,
[ 'name','Name','dt-text' ] ,
[ 'artistList'=>['list','mm','defalut'] ,'Artist List','dt-select'] ,
[ 'nationality'=>['nationality','mm','defalut'] ,'Nationality','dt-select'] ,
[ 'view','View',''],
[ 'status','Status' ,'']
];
$array = array_reduce($array, function ($c, $v) {
$first_key = array_keys($v)[0];
return array_merge($c, array($first_key => $v[$first_key])); }, []);
print_r($array);
Output:
Array (
[0] => id
[1] => name
[artistList] => Array (
[0] => list
[1] => mm
[2] => defalut
)
[nationality] => Array (
[0] => nationality
[1] => mm
[2] => defalut
)
[2] => view
[3] => status
)
Demo on 3v4l.org

php - Convert array to an array

I have an array like below:
array:3 [▼
"_token" => "hLuF5TLimRRkxRUOeeWoOby7iXLNXU7EiHRmTAso"
"room_id" => array:3 [▼
0 => "089a6ca8-26de-3ca2-bbbf-17c410d15266"
1 => "13c56ac2-6c77-3ca7-b24a-2a70f4f0526c"
2 => "2d9b7392-aa50-37e4-882a-804affb87223"
]
"new_electric" => array:3 [▼
0 => "123"
1 => "234"
2 => "345"
]
]
How can I slice it into new array which like
array:4 [▼
"_token" => "hLuF5TLimRRkxRUOeeWoOby7iXLNXU7EiHRmTAso"
"1" => array:2 [▼
"room_d" => "089a6ca8-26de-3ca2-bbbf-17c410d15266"
"new_electric => "123"
]
"2" => array:2 [▼
"room_id" => "13c56ac2-6c77-3ca7-b24a-2a70f4f0526c"
"new_electric" => "234"
]
"3" => array:2 [▼
"room_id" => "2d9b7392-aa50-37e4-882a-804affb87223"
"new_electric" => "345"
]
]
I think I need to use an array_push but I don't know how to do that ? Please help.
You have to use foreach
$arr = array(
"_token" => "hLuF5TLimRRkxRUOeeWoOby7iXLNXU7EiHRmTAso",
"room_id" => array(
0 => "089a6ca8-26de-3ca2-bbbf-17c410d15266",
1 => "13c56ac2-6c77-3ca7-b24a-2a70f4f0526c",
2 => "2d9b7392-aa50-37e4-882a-804affb87223",
),
"new_electric" => array(
0 => "123",
1 => "234",
2 => "345",
)
);
$final = array();
$final[ "_token" ] = $arr[ "_token" ];
foreach( $arr[ "room_id" ] as $key => $value ) {
$final[ ( $key + 1 ) ] = array(
"room_id" => $value,
"new_electric" => $arr[ "new_electric" ][ $key ]
);
}
This will result to:
Array
(
[_token] => hLuF5TLimRRkxRUOeeWoOby7iXLNXU7EiHRmTAso
[1] => Array
(
[room_id] => 089a6ca8-26de-3ca2-bbbf-17c410d15266
[new_electric] => 123
)
[2] => Array
(
[room_id] => 13c56ac2-6c77-3ca7-b24a-2a70f4f0526c
[new_electric] => 234
)
[3] => Array
(
[room_id] => 2d9b7392-aa50-37e4-882a-804affb87223
[new_electric] => 345
)
)
Note: Use $key + 1 because you want the index to start from 1
You can use array_map with no callback for that (check example #4 in the manual for more detail):
$result = array_map(null, $arr["room_id"], $arr["new_electric"]);
$result['_token'] = $arr['_token'];
other way
<pre>
<?php
$array=array(3=>array("_token"=>"hLuF5TLimRRkxRUOeeWoOby7iXLNXU7EiHRmTAso","room_id"=>array("0"=>"089a6ca8-26de-3ca2-bbbf-17c410d15266"
,"1"=>"13c56ac2-6c77-3ca7-b24a-2a70f4f0526c","2"=>"50-37e4-882a-804affb87223"),"new_electric"=>array(0=>"123","1"=>"234","2"=>"345")));
print_r($array);
$i=0;
$array4=array();
array_push($array4,$array[3]["_token"]);
foreach($arr2 as $value){
array_push($array4,array($array[3]["room_id"][$i],$array[3]["new_electric"][$i]));
$i++;
}
print_r($array4);
?>
</pre>

PHP Multidimensional Array Megre

I have a multidimensional array as
array:3 [▼
0 => array:3 [▼
"product_id" => "8"
"qty" => 1
]
1 => array:3 [▼
"product_id" => "9"
"qty" => 2
]
]
and I would like to merge a static associative array ['invoice_id' => 1] in to all the arrays in the multidimensional array. and the end result should be something like this
array:3 [▼
0 => array:3 [▼
"product_id" => "8"
"qty" => 1,
"invoice_id" => 1
]
1 => array:3 [▼
"product_id" => "9"
"qty" => 2,
"invoice_id" => 1
]
]
is there a way to do this with out looping through the multidimensional array
Try using Array_Map
suppose Array data store in $testArray variable
$testArray= array_map(function($arr){
return $arr + ['invoice_id' => 1];
}, $testArray);

array_merge_recursive on multidimensional array

Please how do i apply array_merge_recursive on the code below
<?php
array:4 [▼
0 => array:1 [▼
1 => "1_1"
]
1 => array:1 [▼
1 => "1_2"
]
2 => array:1 [▼
2 => "2_1"
]
3 => array:1 [▼
2 => "2_2"
]
]
?>
I need the output to be
<?php
array:2 [▼
0 => array:2 [▼
0 => "1_1",
1 => "1_2"
]
1 => array:2 [▼
0 => "2_1",
1 => "2_2"
]
]
?>
Note: the inner array might increase from 4 to 1000
Thank you.
Short and to the point.
$array = [
0 => [
0 => '1_1',
],
1=> [
0 => '1_2',
],
2 => [
0 => '2_1',
],
3 => [
0 => '2_2',
],
];
foreach(array_chunk($array, 2) as $value){
print_r(array_merge(array_shift($value), array_pop($value)));
}
Get a chunk of 2 from the big array. Loop over the chunks, get the first element in the array and the last element and merge them.
Result:
Array
(
[0] => 1_1
[1] => 1_2
)
Array
(
[0] => 2_1
[1] => 2_2
)
Remove the print_r and adapt to your own needs.
array_merge_recursive can also be used instead of array_merge but it this case it does exactly the same thing. You'd still need to somehow get the chunks.

Cross compare two array elements

After a lot of API calls and loops I have created an array. This array takes the following form
2 => array:3 [▼
"someInfo" => array:1 [▶]
"existingIDs" => array:1 [▼
0 => array:1 [▼
"id" => "123456"
]
]
"idList" => array:2 [▼
0 => array:1 [▼
"id" => "123456"
]
1 => array:1 [▼
"id" => "777564"
]
]
]
The part I am interested in is existingIDs and idList. The problem is that some ids are appearing in both array elements, so in the above example the id 123456 appears in both.
What I need to do is cross compare these two elements and perhaps create a new element with unique ids. So the above example may turn into something like this
2 => array:3 [▼
"someInfo" => array:1 [▶]
"existingIDs" => array:1 [▼
0 => array:1 [▼
"id" => "123456"
]
]
"idList" => array:2 [▼
0 => array:1 [▼
"id" => "123456"
]
1 => array:1 [▼
"id" => "777564"
]
]
"uniqueList" => array:2 [
0 => array:1 [
"id" => "123456"
]
1 => array:1 [
"id" => "777564"
]
]
]
How could something like this be achieved?
Thanks
Use the following approach with array_column(available since PHP 5.5), array_merge and array_unique functions:
$arr = [
"someInfo" => [],
"existingIDs" => [
["id" => "123456"]
],
"idList" => [
["id" => "123456"],
["id" => "777564"],
["id" => "777564"]
]
];
$all_ids = array_merge(array_column($arr['existingIDs'], 'id'), array_column($arr['idList'], 'id'));
$arr['uniqueList'] = array_unique($all_ids);
print_r($arr);
The output:
Array
(
[someInfo] => Array
(
)
[existingIDs] => Array
(
[0] => Array
(
[id] => 123456
)
)
[idList] => Array
(
[0] => Array
(
[id] => 123456
)
[1] => Array
(
[id] => 777564
)
[2] => Array
(
[id] => 777564
)
)
[uniqueList] => Array
(
[0] => 123456
[2] => 777564
)
)

Categories