Menu
  • HOME
  • TAGS

How to array merge in case same name in array

php,arrays,array-merge

$data = array( array('Time' => '00:45', 'PlottingType' => 'Research'), array('Time' => '00:45', 'PlottingType' => 'admin'), array('Time' => '00:45', 'PlottingType' => 'admin'), array('Time' => '00:45', 'PlottingType' => 'Research'), array('Time' => '00:30', 'PlottingType' => 'Research'), ); print_r(array_reduce($data, function($result, $item) { $key = $item['PlottingType']; if (!isset($result[$key])) $result[$key] = $item; else { list($h1, $m1)...

Merge 2 sorted arrays in C

c++,c,sorting,array-merge,array-algorithms

The problem is that you did not provide any space in which the merged information should be placed. You provided an uninitialized pointer c for the sorted parameter, which gets dereferenced inside the function, causing undefined behavior. To fix this, pass an array with sufficient capacity to hold elements of...

advanced unset the value form array PHP

php,arrays,array-merge,recursive-datastructures,array-unset

This: foreach ($allCat as $d => $mess) { $mess is a temporary COPY of whatever value your foreach() loop is currently working on. When you do your unset($mess...) later on, you're simply unsetting that temporary copy. While some might suggest making $mess a reference, this can/will cause problems later on,...

Push or Merge data into existing Array

php,arrays,array-merge,array-push

Just simply use the count() of your $g["DATA"] array as index and then you can merge it like this: $g['DATA'][count($g["DATA"])-1] = array_merge($g['DATA'][count($g["DATA"])-1], $ownArray); //^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^ // -1 Because an array is based index 0 -> means count() - 1 = last key ...

How can I merge one array with values into an array with stdClass objects?

php,arrays,multidimensional-array,array-merge

[[email protected] tmp]$ cat test.php <?php $first_array = array( (object)array("field_value"=>"green","count"=>null), (object)array("field_value"=>"yellow","count"=>null) ); $second_array = array(2,7); function simple_merge($arr1, $arr2) { return array_map(function($a,$b){ $a->count = $b; return $a; },$arr1,$arr2); } print_r($first_array); print_r($second_array); print_r(simple_merge($first_array,$second_array)); ?> Output [[email protected] tmp]$ php test.php Array ( [0] => stdClass Object ( [field_value] =>...

Merging and adding values of 2 JSON feeds in PHP

arrays,array-merge

You can use the following code to get the expected result: <?php $a = '{"ingredients":[{"name": "Sunflower oil","quantity": 100,"measure": "ml"},{"name": "Olive oil","quantity": 50,"measure": "ml"}]}'; $b = '{"ingredients":[{"name": "Sunflower oil","quantity": 10,"measure": "ml"},{"name": "Fennel seeds","quantity": 1,"measure": "teaspoon"},{"name": "Garlic","quantity": 1,"measure": "clove"}]}'; $aArr = json_decode($a, true); $bArr = json_decode($b, true); $sumArr = array("ingredients" => array());...

How I can calculate the MD5 in PHP of the all values of array

php,arrays,array-merge

... $dom = new DOMDocument; $dom->loadXML($hash); ... $mensagemTISS = ''; $output = array(); $els = $objDom->getElementsByTagName('*'); for($i=0;$i<$els->length;$i++) { if(! $els->item($i)->hasChildNodes() ) { } elseif ($els->item($i)->firstChild->nodeType == 3 ) { $tmp = $els->item($i)->textContent; if ( preg_replace('/\s+/', '', $tmp) ) { $tmp = preg_replace('/\s+/', '', $tmp);("\n", "", $tmp); } $mensagemTISS = $mensagemTISS...

Merging two ArrayLists of type String in Java

java,loops,array-merge

Indicies.add(0); for (int i = 0; i < SPK.size() - 1; i++) { if (SPK.get(i) != SPK.get(i + 1)) { Indicies.add(i + 1); } } for (int i = 0; i < Indicies.size() - 1; i++) { Count.add(Indicies.get(i + 1) - Indicies.get(i)); } Count.add((SPK.size() - Indicies.get(Indicies.size() - 1))); System.out.println("count:"...

How to use array_merge to display non-quoted commands in PHP/Highcharts?

javascript,php,highcharts,array-merge

couple of ways implement event handlers in the javascript itself, like options.credits = data["credits"]; options.chart.events = {}; options.chart.events.load = function(event){ // implementations go here } Not recommending though, using eval options.credits = data["credits"]; options.chart.events = eval(data.chart.events); Hope this helps...

In php how to assign specific array key to main array key?

php,arrays,multidimensional-array,array-merge

Following will work, foreach($array as $key => $value){ $array[$value["id"]] = $array[$key]; unset($array[$key]); } Side Note: I presume id here will be unique for each record....

Laravel select form default value

php,laravel,drop-down-menu,array-merge

array_merge will re-index the array when merging. You can use + for this - $user = array('' => 'Please Select') + $user; The indexs will not be changed....

PHP : merging array with same element

php,arrays,multidimensional-array,array-merge

This may help you [[email protected] tmp]$ cat test.php <?php $array= array( (object)array("exercise_name" => "Hang Clean","category_name" => "Heavy,Pull,Core"), (object)array("exercise_name" => "Ice Skaters","category_name" => "Ground,Lower,Plyometrics"), (object)array("exercise_name" => "Ice Skaters","category_name" => "Basketball,Sport Specific") ); function simple_merge($array) { foreach($array as $k) { if(isset($output[$k->exercise_name])) {...

PHP Merge (Add) Two Arrays by Same Key

php,arrays,array-merge

As mentioned in the comments, looping through the array will do the trick. $a = array('a' => 2, 'b' => 5, 'c' => 8); $b = array('a' => 3, 'b' => 7, 'c' => 10); $c = array(); foreach($a as $index => $item) { if(isset($b[$index])) { $new_value = $a[$index] +...

Laravel 5 / zofe/rapyd-laravel Datagrid : ErrorException in Factory.php line 150: array_merge(): Argument #2 is not an array

laravel,datagrid,array-merge

It's because you're passing your data in the wrong way to the view. Instead of return view('welcome',$grid); You should be doing: return view('welcome')->with('grid', $grid); or return view('welcome', ['grid' => $grid]); ...

Having trouble with merge function algorithm in C++

c++,arrays,algorithm,merge,array-merge

You have undefined behavior in the merge function, because you don't initialize the i or j variables. That means their value is indeterminate (and in reality will be seemingly random). When declaring variable, you need to initialize every variable separately, you can't do like you do now and hope all...

PHP append elements from another associative array into same array with same keys

php,arrays,array-merge

There is no native PHP functionality for this, since this is not a very standard way of merging arrays together. You can write a loop that will do it for you though, using foreach(): $new_array = []; foreach($array_1 as $user){ $temp = $user; $temp['account_data'] = $array_2[$user['user_id']]; $new_array[] = $temp; }...

Undefined Offset error although isset() present [on hold]

php,arrays,merge,array-merge

You are doing increment & assignment the value of $agents[$agent[0]][2] together. $agents[$agent[0]][2] += $agent[2]; is same as $agents[$agent[0]][2] = $agents[$agent[0]][2] + $agent[2]; Which needs $agents[$agent[0]][2] to be set first. The check should be - if(isset($agent[2]) && isset($agents[$agent[0]][2])) { ...

How to merge arrays in javascript in a different way?

javascript,arrays,merge,array-merge

Here's one way to do it by counting the occurrences of each item in each array: var arr1 = [1,2,3,4]; var arr2 = [1,1,2,4,5,5,5]; var arr3 = [1,3,3,5,5]; function joinCommon(/* list of arrays */) { var arr, arrayCounts, masterList = {}, item, output; // for each array passed in for...

I want to join 2 arrays with a standard usable

php,arrays,drupal,drupal-7,array-merge

Just use array_intersect(): $array3 = array_intersect($array2, $array1); ...

Creating new array from existing arrays in php

php,arrays,array-merge

Example of how you can achieve this: $array = array(4,5,6); $array2 = array(8,9,0,12,44,); $count1 = count($array); $count2 = count($array2); $count = ($count1 > $count2) ? $count1 : $count2; $rez = array(); for ($i = 0; $i < $count; $i++) { if ($i < $count1) { $rez[] = $array[$i]; } if...

Foreach array merge

php,arrays,merge,array-merge

I only needed to specify $menu_objects as an array: $menus = array( 'menu 1', 'menu 2', 'menu 3' ); $menu_objects = array(); foreach( $menus as $key => $menu ) { $menu_object = wp_get_nav_menu_items( esc_attr( $menu ) ); $menu_objects = array_merge( $menu_objects, $menu_object ); } ...

PHP - merge arrays without array_merge

php,arrays,function,array-merge

if and only if the arrays have different keys, you can use the + operator to union the two arrays. If the arrays contain the same key (eg. default indexes), only the first one will be kept and the rest will be omitted. eg: $arr1 = array("color1" => "red", "color2"...

PHP : Insert an array into another array

php,arrays,arraylist,insert,array-merge

Try this: <?php $array = ["1","2","5-10","15-20"]; $newdata = array(); foreach($array as $data){ if(strpos($data,'-')){ $range = explode('-', $data); for($i=$range[0];$i<=$range[1];$i++){ array_push($newdata, $i); } } else{ array_push($newdata, (int)$data); } } echo "<pre>"; print_r($array); echo "</pre>"; echo "<pre>"; print_r($newdata); echo "</pre>"; Result: Array ( [0] => 1 [1] => 2 [2] => 5-10 [3]...

How do I obtain the duplicated values of two ArrayLists?

java,arraylist,duplicates,array-merge

You can use the List#retainAll(Collection<?> c) method, which: Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection. List<Point> first = ... List<Point> second =...