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
)
)
Related
I have two arrays and the First array name is $balances and output is below:
Array
(
[0] => Array
(
[index] => 0
[account_id] => 1
[company_id] => 6
[company_code] => ABBANK
)
[1] => Array
(
[index] => 6
[account_id] => 1
[company_id] => 147
[company_code] => IFIC
)
[2] => Array
(
[index] => 11
[account_id] => 1
[company_id] => 293
[company_code] => SOUTHEASTB
)
)
Second array name is $market and it's output:
Array
(
[0] => Array
(
[company] => SOUTHEASTB
[high] => 0
[low] => 0
)
[1] => Array
(
[company] => IFIC
[high] => 0
[low] => 0
)
[2] => Array
(
[company] => ABBANK
[high] => 0
[low] => 0
)
)
I merge this two array and getting bellow output. The 2nd array push the array value to 1st array using the code below producing a result which is not in my requirement:
$result = array();
foreach ($balances as $key => $value) {
$result[] = array_merge($value, $market[$key]);
}
Output:
Array
(
[0] => Array
(
[index] => 0
[company_code] => ABBANK
[company] => 1JANATAMF
[high] => 5.3
[low] => 5
)
[1] => Array
(
[index] => 6
[company_code] => IFIC
[company] => 1STPRIMFMF
[high] => 16.9
[low] => 16.2
)
[2] => Array
(
[index] => 11
[company_code] => SOUTHEASTB
[company] => AAMRANET
[high] => 44
[low] => 43
)
)
My challenge to check the $balances[company_code] == $market['company'] and append the 2nd array to 1st array which will produce the following result.
Array
(
[0] => Array
(
[index] => 0
[account_id] => 1
[company_id] => 6
[company_code] => ABBANK
[high] => 0
[low] => 0
)
[1] => Array
(
[index] => 6
[account_id] => 1
[company_id] => 147
[company_code] => IFIC
[high] => 0
[low] => 0
)
[2] => Array
(
[index] => 11
[account_id] => 1
[company_id] => 293
[company_code] => SOUTHEASTB
[high] => 0
[low] => 0
)
)
But above code block is not serving my demand. Because this code block only push the second array to 1st array using merge function. I tried every other solution in my stock. Is there anyone who can give me any solution will be highly appreciated.
This can be solved in minimal code by using array_column to re-index both arrays by the company name, the two arrays can then be merged using array_merge_recursive:
$output = array_merge_recursive(array_column($balances, null, 'company_code'),
array_column($market, null, 'company'));
Output:
Array
(
[ABBANK] => Array
(
[index] => 0
[account_id] => 1
[company_id] => 6
[company_code] => ABBANK
[company] => ABBANK
[high] => 0
[low] => 0
)
[IFIC] => Array
(
[index] => 6
[account_id] => 1
[company_id] => 147
[company_code] => IFIC
[company] => IFIC
[high] => 0
[low] => 0
)
[SOUTHEASTB] => Array
(
[index] => 11
[account_id] => 1
[company_id] => 293
[company_code] => SOUTHEASTB
[company] => SOUTHEASTB
[high] => 0
[low] => 0
)
)
If you want a numerically indexed result, just pass $output through array_values.
$output = array_values($output);
Demo on 3v4l.org
I have taken two arrays '$a' and '$b', in your case it is $balances and $market, respectively. array_push() is the simplest thing you can do to push/append values of array '$b' to array '$a'.
$a = array(array(
"index" => 0,
"account_id" => 1,
"company_id" => 6,
"company_code" => "ABBANK"
),
array
(
"index" => 6,
"account_id" => 1,
"company_id" => 147,
"company_code" => "IFIC"
),
array
(
"index" => 11,
"account_id" => 1,
"company_id" => 293,
"company_code" => "SOUTHEASTB"
));
$b = array
(
array
(
"company" => "SOUTHEASTB",
"high" => 0,
"low" => 0,
),
array
(
"company" => "IFIC",
"high" => 0,
"low" => 0,
),
array
(
"company" => "ABBANK",
"high" => 0,
"low" => 0
)
);
$result = array();
foreach($a as $key=>$value){
foreach($b as $key2 => $value2){
if($value['company_code'] === $value2['company']){
//append matched part of $b array to $a array value
array_push($value,$value2['high'],$value2['low']);
//append to result array
array_push($result, $value);
}
}
}
print_r($result);
//Output
Array
(
[0] => Array
(
[index] => 0
[account_id] => 1
[company_id] => 6
[company_code] => ABBANK
[0] => 0
[1] => 0
)
[1] => Array
(
[index] => 6
[account_id] => 1
[company_id] => 147
[company_code] => IFIC
[0] => 0
[1] => 0
)
[2] => Array
(
[index] => 11
[account_id] => 1
[company_id] => 293
[company_code] => SOUTHEASTB
[0] => 0
[1] => 0
)
)
While Nick's three-function approach is certainly attractive for its brevity, be aware that it stores the relating elements from both arrays (has redundant data in the output) and makes three iterating cycles.
A less concise alternative that doesn't require a recursive call is to forge a lookup array with one cycle (foreach()) and remove the unwanted element from the lookup, then iterate the balances array and swiftly merge related data set based on share company name values.
This assumes that the balances array should dictate which market values should be included in the result.
Code: (Demo)
$lookup = [];
foreach ($market as $row) {
$lookup[array_shift($row)] = $row;
}
var_export(
array_map(
fn($row) => $row + ($lookup[$row['company_code']] ?? []),
$balances
)
);
I have an issue with forming a recursive array. That is shifting the child array nodes to direct elements to a parent array.
like from,
Array
(
[0] => Array
(
[id] => 1
[category_name] => flare
[parent_category_id] => 0
[childrenrecursive] => Array
(
[0] => Array
(
[id] => 2
[category_name] => analytics
[parent_category_id] => 1
[braincount] => Array
(
[count] => 3
[category_id] => 2
)
[childrenrecursive] => Array
(
[0] => Array
(
[id] => 4
[category_name] => sports analytics
[parent_category_id] => 2
[braincount] => Array
(
[count] => 4
[category_id] => 4
)
[childrenrecursive] => Array
(
)
)
)
)
[1] => Array
(
[id] => 3
[category_name] => cluster
[parent_category_id] => 1
[braincount] => Array
(
[count] => 4
[category_id] => 3
)
[childrenrecursive] => Array
(
)
)
)
)
)
to,
Array
(
[0] => Array
(
[id] => 1
[category_name] => flare
[parent_category_id] => 0
[childrenrecursive] => Array
(
[0] => Array
(
[id] => 2
[category_name] => analytics
[parent_category_id] => 1
[count] => 3
[category_id] => 2
[childrenrecursive] => Array
(
[0] => Array
(
[id] => 4
[category_name] => sports analytics
[parent_category_id] => 2
[count] => 4
[category_id] => 4
[childrenrecursive] => Array
(
)
)
)
)
[1] => Array
(
[id] => 3
[category_name] => cluster
[parent_category_id] => 1
[count] => 4
[category_id] => 3
[childrenrecursive] => Array
(
)
)
)
)
)
only by moving following child array append to parent in a recursive way.
[braincount] => Array
(
[count] => 4
[category_id] => 3
)
can anybody help me to format the array like specified.
Let's say $input is the array posted in the question:
/**
* If $value is an array then move the content of 'braincount' one level up
* (in $value) then call recursively for all its children.
*/
function moveUp($value)
{
// Not an array? Nothing to do; return the input value unchanged
if (! is_array($value)) {
return $value;
}
// Move the content of 'braincount' (if present) one level up
if (array_key_exists('braincount', $value)) {
$value = array_merge($value, $value['braincount']);
unset($value['braincount']);
}
// Apply the same operation to all children
return array_map(__FUNCTION__, $value);
}
// Verify it works
print_r(moveUp($input));
I have array of categories like this and I want to merge the same value like 1 and 0 index value etc in another new array how it is possible.
Array
(
[0] => Array
(
[id] => 29
[parentId] => 0
[serviceName] => Hair
[parentServiceImg] => 7ffb7f5aa2a210ba927e9de64ef17f93.jpeg
[subCategory] => Array
(
[id] => 30
[parentId] => 29
[serviceName] => Blow Out
[subCategory] => Array
(
[id] => 31
[parentId] => 30
[serviceName] => Add Clients Hair Extensions & Style
[subCategory] => Array
(
[id] => 49
[parentId] => 31
[serviceName] => Hair style color
[servicePrice] => 100
)
)
)
)
[1] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 3
[parentId] => 2
[serviceName] => Add Airbrush
[servicePrice] => 232
)
)
)
[2] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 6
[parentId] => 1
[serviceName] => Photoshot Makeup
[subCategory] => Array
(
[id] => 7
[parentId] => 6
[serviceName] => Add Airbrush
[servicePrice] => 8
)
)
)
[3] => Array
(
[id] => 1
[parentId] => 0
[serviceName] => Makeup
[parentServiceImg] => e00d3576847fef9f717d1de2647ed954.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 4
[parentId] => 2
[serviceName] => Add Lashes
[servicePrice] => 6
)
)
)
[4] => Array
(
[id] => 29
[parentId] => 0
[serviceName] => Hair
[parentServiceImg] => 7ffb7f5aa2a210ba927e9de64ef17f93.jpeg
[subCategory] => Array
(
[id] => 2
[parentId] => 1
[serviceName] => Special Event Makeup
[subCategory] => Array
(
[id] => 47
[parentId] => 29
[serviceName] => Wedding Day Mother of the Bride / Mother of the Groom Hair Touch Ups
[servicePrice] => 5
)
)
)
)
I want to 0 index value merge with 4 index value and get on 0 index of new array and 1 to 3 index value merge and get on 1 index in new array using php. I need help for this. thanks.
It should help you:
<?php
$your_array = [0=>['array1'], 1=>['array2'],2=>['array3'], 3=>['array4'], 4=>['array5']];
$new_array = [];
$first = array_shift($your_array);
$last = array_pop($your_array);
$array = array_merge($first, $last);
$new_array[] = $array;
$new_array[] = $your_array;
?>
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);
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