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>
Related
I want to create and php associative array from an array like bellow
array:3 [▼
0 => array:7 [▼
"item_id" => "1"
"customer_id" => "53453"
"name" => "Item3"
"price" => 0
"quantity" => 1
]
1 => array:7 [▼
"item_id" => "3"
"customer_id" => "53453"
"name" => "Item1"
"price" => 0
"quantity" => 1
]
2 => array:7 [▼
"item_id" => "2"
"customer_id" => "765656"
"name" => "Item2"
"price" => 0
"quantity" => 1
]
]
I want to create an assoc with customer_id from this array like bellow. My goal is to create an assoc array with common customer_id for get all customer itmes.
array:2 [▼
// if common customer id, make an item array for the customer
53453=> array:2 [▼
0 => array:3 [▼
"quantity" => "1"
"name" => "Item1"
"price" => 0
],
1 => array:3 [▼
"quantity" => "1"
"name" => "Item3"
"price" => 0
]
]
78640 => array:1 [▼
0 => array:3 [▼
"quantity" => "1"
"name" => "Item2"
"price" => 0
]
]
]
You can group by customer_id using the same key, and and the old array without the keys you don't want.
$array = [
["item_id" => "1", "customer_id" => "53453", "name" => "Item3", "price" => 0, "quantity" => 1],
["item_id" => "3", "customer_id" => "53453", "name" => "Item1", "price" => 0, "quantity" => 1],
["item_id" => "2", "customer_id" => "765656", "name" => "Item2", "price" => 0, "quantity" => 1],
];
$finalArray = [];
foreach ($array as $item)
{
$customer_id = $item['customer_id'];
unset($item['customer_id']);
unset($item['item_id']);
$finalArray[$customer_id][] = $item;
}
print_r($finalArray);
Outputs :
Array
(
[53453] => Array
(
[0] => Array
(
[name] => Item3
[price] => 0
[quantity] => 1
)
[1] => Array
(
[name] => Item1
[price] => 0
[quantity] => 1
)
)
[765656] => Array
(
[0] => Array
(
[name] => Item2
[price] => 0
[quantity] => 1
)
)
)
online example
Or if you only want the create some selected keys :
$finalArray[$customer_id][] = [
'name' => $item['name'],
'price' => $item['price'],
'quantity' => $item['quantity'],
];
online example
$newarray=[];
foreach ($firstarray as $fa)
{
$tarray=array($fa['qty'],$fa['name'],$fa['price']);
$newarray[$fa['customer_id']][]=$tarray;
}
... and the result you want will be in $newarray, more or less.
I have an array like the one below
array:4 [▼
0 => array:3 [▼
0 => array:2 [▼
"url" => "https://domain.com.vn"
"value" => "Keyword B3"
]
1 => array:2 [▼
"url" => "https://domain.com.vn"
"value" => "[IMAGES 1]"
]
]
1 => array:3 [▼
0 => array:2 [▼
"url" => "https://domain-4.com.vn"
"value" => "D1"
]
1 => array:2 [▼
"url" => "https://domain-4.com.vn"
"value" => "E3"
]
]
]
I want to combine the above 2 arrays into 1 array as follows (number of items in 2 arrays is always equal):
$result = [
[
"url" => [
"https://domain.com.vn",
"https://domain-4.com.vn"
]
"value" => [
"Keyword B3",
"D1"
]
],
[
"url" => [
"https://domain.com.vn",
"https://domain-4.com.vn"
],
"value" => [
"[IMAGES 1]",
"E3"
]
]
]
I have searched a lot but have not found a solution yet. Thanks for any help.
Because number of items in 2 sub-arrays is always equal, you can use a for loop like this:
$array = [
0 => [
0 => [
'url' => 'https://domain.com.vn',
'value' => 'Keyword B3'
],
1 => [
'url' => 'https://domain.com.vn',
'value' => '[IMAGES 1]'
]
],
1 => [
0 => [
'url' => 'https://domain-4.com.vn',
'value' => 'D1'
],
1 => [
'url' => 'https://domain-4.com.vn',
'value' => 'E3'
]
]
];
$result = [];
for ($i = 0 ; $i < count($array[0]); $i++) {
$result[] = [
'url' => [$array[0][$i]['url'], $array[1][$i]['url']],
'value' => [$array[0][$i]['value'], $array[1][$i]['value']],
];
}
print_r($result);
Result:
Array
(
[0] => Array
(
[url] => Array
(
[0] => https://domain.com.vn
[1] => https://domain-4.com.vn
)
[value] => Array
(
[0] => Keyword B3
[1] => D1
)
)
[1] => Array
(
[url] => Array
(
[0] => https://domain.com.vn
[1] => https://domain-4.com.vn
)
[value] => Array
(
[0] => [IMAGES 1]
[1] => E3
)
)
)
For a dynamic solution, you will need to iterate the first level of the array, then combine the first deep subarray's keys with the return value of array_map()'s special transposing behavior with null and the spread operator.
It sure is a bunch of voodoo, but it is dynamic.
Code: (Demo) (Demo showing different length first level data sets)
var_export(
array_map(
function ($subarray) {
return array_combine(
array_keys($subarray[0]),
array_map(null, ...array_values($subarray))
);
},
$array
)
);
See other other phptranspose tagged Stack Overflow pages to see other usages of the transposing technique.
I have 2 arrays
Array 1:
array:3 [▼
0 => 1
1 => 2.3
2 => 4.5
]
Array 2:
array:3 [▼
0 => array:2 [▼
"name" => "john"
"age" => 34
]
1 => array:2 [▼
"name" => "doe"
"age" => 12
]
2 => array:2 [▼
"name" => "kelvin"
"age" => 14
]
]
How do I merge array 1 into array 2 so that I have something like this-
array:3 [▼
0 => array:3 [▼
"name" => "john"
"age" => 34,
"score" => 1
]
1 => array:3 [▼
"name" => "doe"
"age" => 12,
"score" => 2.3
]
2 => array:3 [▼
"name" => "kelvin"
"age" => 14,
"score" => 4.5
]
]
Notice that the values of array 1 now have keys called 'score'.
You can use foreach loop with reference &:
$ar = [1,2,3.4];
$ar2 = [['name'=>'Joe','age' => 33],['name'=>'Joe2','age' => 33],['name'=>'Joe3','age' => 33]];
foreach($ar2 as $ind=>&$person){
$person['score'] = $ar[$ind];
}
print_r($ar2);
Demo
Output:
Array
(
[0] => Array
(
[name] => Joe
[age] => 33
[score] => 1
)
[1] => Array
(
[name] => Joe2
[age] => 33
[score] => 2
)
[2] => Array
(
[name] => Joe3
[age] => 33
[score] => 3.4
)
)
You can also use array_walk to walk through the array.
<?php
$a = [1,2.3,4.5];
$b = [
["name" => "john", "age" => 34],
["name" => "doe","age" => 12],
["name" => "kelvin", "age" => 14]
];
array_walk($a,function($val,$key) use (&$b){
$b[$key]['score'] = $val;
});
print_r($b);
Demo: https://3v4l.org/58rXG
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
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
)
)