I have two arrays
First is
Array
(
[0] => Array
(
[image] => Copy1vs.jpg
[title] => V.S Achuthanandhan
[groupId] => 1
[masterId] => 1
[id] => 1
[itemId] => 1
[status] => 1
)
[1] => Array
(
[image] => Copy1pinarayi.jpg
[title] => Pinarayi Vijayan
[groupId] => 1
[masterId] => 2
[id] => 2
[itemId] => 2
[status] => 1
)
[2] => Array
(
[image] => Copy1chandy.jpg
[title] => Oommen Chandy
[groupId] => 1
[masterId] => 3
[id] => 3
[itemId] => 3
[status] => 1
)
)
And Second is
Array
(
[0] => Array
(
[image] => Copy1antony.jpg
[title] => A. K. Antony
[groupId] => 1
[masterId] => 4
[id] => 4
[itemId] => 4
[status] => 1
)
)
How can i combine these two arrays as a single array
like
Array
(
[0] => Array
(
[image] => Copy1vs.jpg
[title] => V.S Achuthanandhan
[groupId] => 1
[masterId] => 1
[id] => 1
[itemId] => 1
[status] => 1
)
[1] => Array
(
[image] => Copy1pinarayi.jpg
[title] => Pinarayi Vijayan
[groupId] => 1
[masterId] => 2
[id] => 2
[itemId] => 2
[status] => 1
)
[2] => Array
(
[image] => Copy1chandy.jpg
[title] => Oommen Chandy
[groupId] => 1
[masterId] => 3
[id] => 3
[itemId] => 3
[status] => 1
)
[3] => Array
(
[image] => Copy1antony.jpg
[title] => A. K. Antony
[groupId] => 1
[masterId] => 4
[id] => 4
[itemId] => 4
[status] => 1
)
)
i tried array_merge method but not working as per my requirement is it possible without using a for loop..?
these arrays gets from databse as
$itemListArray = array();
foreach($subcat as $sc){
$itemList = DB::table('votemasteritems')
->leftjoin('votemaster','votemaster.id','=','votemasteritems.masterId')
->leftjoin('items','items.id','=','votemasteritems.itemId')
->leftjoin('category','category.id','=','items.categoryId')
->select('items.image','votemaster.title','votemaster.groupId','votemaster.id as masterId','votemasteritems.*')
->where('votemaster.groupId',1)
->where('category.Id',$sc->id)
->get();
array_merge($itemListArray, $itemList);
}
Yes, PHP has the array_merge() function: http://php.net/array_merge
Use it like $combinedArray = array_merge($array1, $array2);.
array_merge doesn’t modify the arrays you pass to it. It only returns a new array containing all of the values, so in your example you’d have to replace the original array like:
$itemListArray = array_merge($itemListArray, $itemList);
check this one
foreach($subcat as $sc){
$itemList = DB::table('votemasteritems')
->leftjoin('votemaster','votemaster.id','=','votemasteritems.masterId')
->leftjoin('items','items.id','=','votemasteritems.itemId')
->leftjoin('category','category.id','=','items.categoryId')
->select('items.image','votemaster.title','votemaster.groupId','votemaster.id as masterId','votemasteritems.*')
->where('votemaster.groupId',1)
->where('category.Id',$sc->id)
->get();
$itemListArray[]=$itemList;
}
Try array_merge() function
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
In your case, it should be:
$itemListArray = array_merge($itemListArray, $itemList);
Related
I've merged two arrays. A duplicate entry is a deleted. Let me explain the as an example (I have compare array using user_id)
$array1 = Array
(
[0] => Array
(
[id] => 44
[user_id] => 2
[name] => jina_testl
[profilePhoto] =>
)
)
$array2 = Array
(
[0] => Array
(
[id] => 2
[user_id] => 3
[name] => demo_test1
[profilePhoto] =>
)
[1] => Array
(
[id] => 3
[user_id] => 2
[name] => abc
[profilePhoto] =>
)
[2] => Array
(
[id] => 4
[user_id] => 4
[name] => test
[profilePhoto] =>
)
)
I have merged array1 and array2 and duplicate array remove. I get the following output:
Array
(
[0] => Array
(
[id] => 44
[user_id] => 2
[name] => jina_testl
[profilePhoto] =>
)
[1] => Array
(
[id] => 2
[user_id] => 3
[name] => demo_test1
[profilePhoto] =>
)
[3] => Array
(
[id] => 4
[user_id] => 4
[name] => abc
[profilePhoto] =>
))
But I want such an output like this:
Array
(
[0] => Array
(
[id] => 2
[user_id] => 3
[name] => demo_test1
[profilePhoto] =>
)
[1] => Array
(
[id] => 4
[user_id] => 4
[name] => abc
[profilePhoto] =>
))
You could do something like the following: first get all the user_ids that we do not want to be included, by taking the intersection of the two columns.
If we then merge the two separate arrays and exclude the ones that are present in the array of unwanted IDs we should get our final result.
$userId = 'user_id';
$userIdsArray1 = array_column($array1, $userId);
$userIdsArray2 = array_column($array2, $userId);
// If we then take the negation we should get something similar to an exclusive or.
// Thus those in one or the other but not in both arrays.
$unwantedUserIds = array_intersect($userIdsArray1, $userIdsArray2);
$result = [];
foreach (array_merge($array1, $array2) as $record) {
if (!in_array($record[$userId], $unwantedUserIds)) {
$result[] = $record;
}
}
echo '<pre>', print_r($result, true), '</pre>';
Result:
Array
(
[0] => Array
(
[id] => 2
[user_id] => 3
[name] => demo_test1
[profilePhoto] =>
)
[1] => Array
(
[id] => 4
[user_id] => 4
[name] => test
[profilePhoto] =>
)
)
I have two arrays each coming from different DB queries, I can't join them in DB so I have to join them using php.
The first:
Array
(
[0] => Array
(
[id] => 23
[host_id] => 5
[pos_id] => 2
[status] => 1
)
[1] => Array
(
[id] => 25
[host_id] => 5
[pos_id] => 1
[status] => 1
)
[2] => Array
(
[id] => 24
[host_id] => 5
[pos_id] => 2
[status] => 1
)
)
And I wanna join it with this array on pos_id and id
Array
(
[0] => Array
(
[id] => 1
[name] => Front
)
[1] => Array
(
[id] => 2
[name] => Back
)
)
so that, the result is:
Array
(
[0] => Array
(
[id] => 23
[host_id] => 5
[pos_id] => 2
[name] => Back
[status] => 1
)
[1] => Array
(
[id] => 25
[host_id] => 5
[pos_id] => 1
[name] => Front
[status] => 1
)
[2] => Array
(
[id] => 24
[host_id] => 5
[pos_id] => 2
[name] => Back
[status] => 1
)
)
The code I have uses an inner loop, matches the value and pushes into array:
foreach($events as &$event) {
foreach($positions as $position) {
if($player[pos_id] == $position[id]){
array_push($event,"name",$position[name]);
}
}
}
My PHP array ($array) is as follows:
Array
(
[channels] => Array
(
[0] => Array
(
[position] => 5
[id] => 11
[name] => AFK
)
[1] => Array
(
[position] => 1
[id] => 22
[name] => ARK
)
[2] => Array
(
[position] => 2
[id] => 33
[name] => ESO
)
[3] => Array
(
[position] => 4
[id] => 44
[name] => semi-afk
)
[4] => Array
(
[position] => 0
[id] => 55
[name] => SPACE
)
[5] => Array
(
[position] => 3
[id] => 66
[name] => Tanks & Ships
)
)
[instant_invite] =>
[id] => 123
[members] => Array
(
[0] => Array
(
[username] => Chartographer
[status] => online
[nick] => Chaz Rambone
[avatar_url] => https://cdn.discordapp.com/embed/avatars/0.png
[avatar] =>
[discriminator] => 3270
[id] => 124
)
[1] => Array
(
[username] => Chukers
[status] => online
[mute] =>
[suppress] =>
[deaf] =>
[channel_id] => 789
[game] => Array
(
[name] => The Elder Scrolls Online
)
[avatar_url] => https://cdn.discordapp.com/embed/avatars/1.png
[avatar] =>
[self_deaf] =>
[discriminator] => 9851
[self_mute] =>
[id] => 456
)
)
[name] => TEST
)
I want to sort by "position" starting at 0 and ASC.
I tried some of the examples found here but not getting it yet. Usort and a few others are not working for me. Sure it's syntax but not sure
if ($array->channels) {
usort($array->channels, function($a, $b) {
return $a->position > $b->position ? 1 : -1;
});
As was mentioned in the comments, you're trying to use object notation, but you're referencing an array. The documentation has very helpful examples.
usort($array['channels'], function($a, $b) {
return $a['position'] > $b['position'] ? 1 : -1;
});
See this fiddle example.
I have the following array:
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
[2] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 3
[category_id] => 1
[amount] => 15
[cost] => 2000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:58
)
[0] => Array
(
[total_sell] => 6
)
)
[3] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 4
[category_id] => 1
[amount] => 50
[cost] => 8000
[paid] => 0
[comment] =>
[created] => 2015-06-23 01:10:10
)
[0] => Array
(
[total_sell] => 6
)
)
)
I want to remove duplicate entry of [Import][product_id]. So my expected result is :
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
)
Would you write a function to filter this type of array and produce expected result. I have been googling for 2 days but no luck.
This is a handy one liner that should do the trick:
$unique= array_map("unserialize", array_unique(array_map("serialize", $original)));
If the underlying arrays are not identical, that won't work, in which case I think you could do:
$unique = array_intersect_key($original ,
array_unique(
array_map(function($item) {
return $item['Import']['product_id'];
}, $original)
)
);
Tested: http://sandbox.onlinephpfunctions.com/code/8aee5cbd614e0ddd1a03dfaa7e98c72fbbe7d68d
Here is a quick stable sort and reduce which runs in linearithmic time. First-encountered product Id's are kept, and entries with duplicate product Id's are ignored.
// Stable sort
sort($in);
// Reduce
$out = array_reduce($in, function(&$acc, &$item){
if($item['Import']['product_id'] !== #$acc[sizeof($acc)-1]['Import']['product_id']) {
$acc[] = $item;
}
return $acc;
}, []);
Demo: http://ideone.com/BP0eUJ
Update: Here is an even better linear-time algorithm that does the same as above using a fast "hash table" lookup. Again, the first-encountered product Id is kept and subsequent ones of the same Id are ignored.
$out = [];
$hashTable = [];
foreach($in as $item) {
$pid = $item['Import']['product_id'];
if(!isset($hashTable[$pid])) {
$out[] = $item;
$hashTable[$pid] = true;
}
}
Demo: http://ideone.com/5RF0og
I am trying to insert an array into a 2-dimensional array at a specific position.
According the manual, i should be able to do this with array_splice(), but it only deletes the contents of my receiving array without insertion.
I want to get an array with all the values (arrays) of $receivingArray plus the new value (array).
What am I doing wrong??
manual info:
array array_splice ( array &$input , int $offset [, int $length [, mixed $replacement = array() ]] )
If length is specified and is zero, no elements will be removed.
If replacement is just one element it is not necessary to put array() around > it, unless the element is an array itself, an object or NULL.
input:
$newArray = array_splice($receivingArray, 0, 0, array($value));
result: $newArray is an empty array
input :
$newArray = array_splice($receivingArray, 1, 0, array($value));
result: $newArray is an empty array
this input:
print_r($receivingArray);
print_r(array($value));
$newArray = array_splice($receivingArray, 1, 1, array($value));
print_r($newArray);
gives: (interestingly)
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
Array
(
[0] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
)
Array
(
[0] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
From the docs for array_splice()
Return Values
Returns the array consisting of the extracted elements.
$newArray = array_splice($receivingArray, 0, 0, array($value));
array_splice modifies its input, so the results you're looking for are in $receivingArray and not $newArray
I missed the fact that array_slice() doesn't actually return its output, but rather acts upon the receiving array itself, which is passed by reference. I didn't notice that there is an ammpersand in front of the first parameter in the manual specification.
this input:
print_r($receivingArray);
print_r(array($value));
array_splice($receivingArray, 0, 0, array($value));
print_r($receivingArray);
gives the correct result:
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)
Array
(
[0] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
)
Array
(
[0] => Array
(
[id] => 1
[primaryID] => 0
[category_id] => 1
[title] => sports
[description] =>
[selected] =>
[level] => 0
)
[1] => Array
(
[id] => 2
[primaryID] => 1
[category_id] => 1
[title] => soccer
[description] =>
[selected] =>
[level] => 1
)
[2] => Array
(
[id] => 4
[primaryID] => 0
[category_id] => 0
[title] => programming
[description] =>
[selected] =>
[level] => 0
)
)