Laravel array merge 3 arrays - php

I have 3 arrays that I need to merge together but can't figure out how?
array 1
array:4 [▼
0 => "admin98#wassiah.test"
1 => "admin69#wassiah.test"
2 => "admin25#wassiah.test"
3 => null
]
array 2
array:4 [▼
0 => "one"
1 => "three"
2 => "two"
3 => null
]
array 3
array:4 [▼
0 => "10"
1 => "11"
2 => null
3 => null
]
And I need to make new array like this:
array:4 [▼
0 => array:2 [▼
"email" => "admin98#wassiah.test"
"name" => "one"
"id" => "10"
]
1 => array:2 [▼
"email" => "admin69#wassiah.test
"name" => "three"
"id" => "11"
]
2 => array:2 [▼
"email" => "admin25#wassiah.test"
"name" => "two"
"id" => null
]
3 => array:2 [▼
"email" => null
"name" => null
"id" => null
]
]
Code
$mails = $request->input('mails'); // array 1
$names = $request->input('names'); // array 2
$heirIds = $request->input('ids'); // array 3
Any idea?

You can try the below code
<?php
$arr1 = array(
"admin98#wassiah.test",
"admin97#wassiah.test",
"admin96#wassiah.test",
"",
);
$arr2 = array(
"one",
"three",
"two",
"",
);
$arr3 = array(
"10",
"11",
"",
"",
);
$result = array();
$count = count($arr1);
for($i=0;$i<$count;$i++){
$result[$i]['email'] = $arr1[$i];
$result[$i]['name'] = $arr2[$i];
$result[$i]['id'] = $arr3[$i];
}
echo '<pre>'; print_r($result);

Related

php create an assoc array with customer_id

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.

PHP combine multi-dimensional arrays

I'm have the following array.
"rent" => array:3 [
0 => array:1 [
0 => "5000"
]
1 => array:3 [
0 => "10000"
1 => "60000"
2 => "80000"
]
2 => []
]
"house_quantity" => array:3 [
0 => array:1 [
0 => "2"
]
1 => array:3 [
0 => "3"
1 => "4"
2 => "6"
]
2 => []
]
"property_id" => array:3 [
0 => 1
1 => 2
2 => 3
]
"type_of_house" => array:3 [
0 => array:1 [
0 => array:1 [
"type" => "studio_apartment"
]
]
1 => array:3 [
0 => array:1 [
"type" => "studio_apartment"
]
1 => array:1 [
"type" => "one_bedroom"
]
2 => array:1 [
"type" => "two_bedroom"
]
]
2 => array:2 [
0 => array:1 [
"type" => "studio_apartment"
]
1 => array:1 [
"type" => "two_bedroom"
]
]
]
]
I want to combine the above array so that it forms an array that looks like this.
"0" => [
"property_id" => 1
"type_of_house" => array:3 [
"type"=> "studio_apartment"
"rent" => "5000"
"house_quantity" => "2"
]
]
"1" => [
"property_id" => 2
"type_of_house" => array:3 [
"type"=> "studio_apartment"
"rent" => "10000"
"house_quantity" => "3"
]
"type_of_house" => array:3 [
"type"=> "one_bedroom"
"rent" => "60000"
"house_quantity" => "4"
]
"type_of_house" => array:3 [
"type"=> "two_bedroom"
"rent" => "80000"
"house_quantity" => "6"
]
]
So far I'm using the foreach loop to loop over the properties and attach the type of houses in each of those properties as follows:
foreach ($request->property_id as $key=> $property_id) {
$result[$key] = array(
'property_id' => $property_id,
'type_of_house' => $request->type_of_house[$key]
);
foreach ($result as $property_key => $property) {
foreach ($property['type_of_house'] as $house_key => $house) {
$house[$key][$house_key] = array(
'rent' => $request->rent[$key][$house_key],
'house_quantity' => $request->house_quantity[$key][$house_key]
);
}
}
$merge = array_merge_recursive($result, $house);
dd($merge);
}
But the array I'm getting back is not quite right. This is the array that I'm getting back.
array:3 [
0 => array:2 [
"property_id" => 1
"type_of_house" => array:1 [
0 => array:1 [
"type" => "studio_apartment"
]
]
]
"type" => "studio_apartment"
1 => array:1 [
0 => array:2 [
"rent" => "5000"
"house_quantity" => "2"
]
]
]
How do I correctly merge such an array, thanks.
Ok, so let's suppose you have 2 arrays
$array1:
Array(2){
[number] => 1,
[address] => "Park Ave 273",
[name] => "Peter Jones"
}
And then a clean $array2, the one i'm gonna be putting my info
To mix them i would have to specify the index i want the first array to get in, for example:
$array1 = array(
"number" => 1,
"address" => "Park Ave 273",
"name" => "Peter Jones"
);
$array2 = array();
$array2['client'] = $array1;
Would return:
Array
(
[client] => Array
(
[number] => 1
[address] => Park Ave 273
[name] => Peter Jones
)
)
In case you have more than one client (on this example) you have to do a foreach loop for every client.

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

Add new value in multiple array PHP

I was searching around but still I can't find an answer.
I have following array
array:2 [
0 => array:2 [
0 => "Name"
1 => "Age"
]
1 => array:2 [
0 => "Name"
1 => "Age"
]
]
and I want to add new value 0 in all arrays. This is the result I expect.
array:2 [
0 => array:2 [
0 => "Name"
1 => "Age"
2 => 0
]
1 => array:2 [
0 => "Name"
1 => "Age"
2 => 0
]
]
Thank you.
Try something like.
foreach($array as &$subArray) {
$subArray[] = 0;
}
var_dump($array);

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);

Categories