Create 2 dimensional merged array based value

I have an array in the following format,

Array ( [0] => Array ( [expense_id] => 7 [type] => frfrf [mode] => rfr [amount] => 100 [tran_type] => expense [date] => 2015-06-29 ) [1] => Array ( [expense_id] => 8 [type] => frfrf [mode] => rfr [amount] => 100 [tran_type] => expense [date] => 2015-06-29 ) [2] => Array ( [expense_id] => 9 [type] => frfrf [mode] => rfr [amount] => 444 [tran_type] => expense [date] => 2015-06-29 ) [3] => Array ( [expense_id] => 10 [type] => frfrf [mode] => rfr [amount] => 1000 [tran_type] => income [date] => 2015-06-29 ) [4] => Array ( [expense_id] => 5 [type] => frfrf [mode] => rfr [amount] => 100 [tran_type] => expense [date] => 2015-06-01 ) )

I want merge the array based on date value and store it as an array with key as date

Array ( [2015-06-29] => Array ( [0] => Array ( [expense_id] => 7 [type] => frfrf [mode] => rfr [amount] => 100 [tran_type] => expense [date] => 2015-06-29 ) [1] => Array ( [expense_id] => 8 [type] => frfrf [mode] => rfr [amount] => 100 [tran_type] => expense [date] => 2015-06-29 ) [2] => Array ( [expense_id] => 9 [type] => frfrf [mode] => rfr [amount] => 444 [tran_type] => expense [date] => 2015-06-29 ) [3] => Array ( [expense_id] => 10 [type] => frfrf [mode] => rfr [amount] => 1000 [tran_type] => income [date] => 2015-06-29 ) ) [2015-06-01] => Array ( [0] => Array ( [expense_id] => 5 [type] => frfrf [mode] => rfr [amount] => 100 [tran_type] => expense [date] => 2015-06-01 ) ) )

Currently I'm doing it using foreach loop , is there any simple way to do this

foreach ($d as $v) { $d1[$v['date']][] = $v; }

Data is retrieved using mysql, mysql query is also acceptable

-------------Problems Reply------------

I know only this way

$newArray = [];

foreach ($array as $subArray)
{
$newArray[$subArray['date']][] = $subArray;
}

Category:php Views:3 Time:2019-03-14
Tags: php mysql arrays

Related post

  • How can I create a heat map based values (not frequencies)? 2012-02-01

    I am doing a project which is creating a heat map based on a set of data that includes 4 things: timestamp, longitude, latitude, one specific value. I use heatmap.py to read the data and create a KML file and png file, and then put them on google map

  • Create a new array based on an array of overlapping entities 2011-01-07

    Do you ever just have one of those days where your brain just doesn't get out of 1st gear? I have an array containing start and end times. I'd like to create a new array showing the keys of the overlapping entries from the initial array. So, say we h

  • iOS create a new array based on objects in an existing array? 2014-08-12

    I have an array(say expensesArray) of Expense objects. expense has property like catId, amount, name etc. Now I want to do something like group by and some other clauses in SQL. i.e in new array, my objects should be grouped based on catId and their

  • PHP / Mysql create a new array using values from an existing array, help? 2011-07-06

    I currently have the following array, Array (1) 0 => Array (5) productid => 1 qty => 1 materialid => 12 and I have the following two queries which are select name as productname, price from products where productid=1 and select name from

  • php - merger array keeping values from arr1 where duplicate 2015-01-11

    I have 2 arrays arr1 and arr2. I would like to merge them without duplicating or overwriting array values in arr1. arr1 ( [0] => Array ( [aName] => Doe jones [ref] => 3 [afield] => 01-06-2015 ) [1] => Array ( [aName] => John doe [re

  • How can I create a numpy array holding values of a multi-variable function? 2009-09-05

    I want to create an array holding a function f(x,y,z). If it were a function of one variable I'd do, for instance: sinx = numpy.sin(numpy.linspace(-5,5,100)) to get sin(x) for x in [-5,5] How can I do the same to get, for instance sin(x+y+z)? -------

  • Create amount of arrays based off variable number in python? 2011-04-16

    I was wondering how you create a variable amount of arrays in python. So if the user entered "3" you would get 3 arrays create. Might be obvios but got stuck after this: baseNumber = 0 #how many rows needed board_height = [] print "select a width" x

  • PHP split array based on search results 2011-05-14

    I have a multidimensional array which is created by a MySQL query which collects results based on a number of groups and sums. The array is below. I'm interested in the costtotal and hitcount for each type of 'ad_type', 'click_status' and 'link_statu

  • smaller array based on array contents 2011-06-05

    I am looking for a way to create a new array based on array contents. so i've got: Array ( [0] => 1 [type] => first_value_i_need [some_id] => 2 [hits] => 88 [some_other_id] => second_value_i_need ) and i would like to get Array ( [0] =

  • Create NSArray from Array within Plist 2011-12-08

    I have a plist structured like this: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>1</k

  • Objective C: Create arrays from first array based on value 2010-05-04

    I have an array of strings that are comma separated such as: Steve Jobs,12,CA Fake Name,21,CA Test Name,22,CA Bill Gates,44,WA Bill Nye,21,OR I have those values in an NSScanner object so that I can loop through the values and get each comma seperate

  • Create an array based of another arrays values by defining a key? 2011-01-08

    I'm trying to create an array based of another arrays values by defining a key? E.g. $old_array = array('hey', 'you', 'testing', 'this'); function get_new_array($key) { global $old_array; //return new array... } $new_array = get_new_array(2); //would

  • create intersection from two or more 2d numpy arrays based on common value in one column 2012-01-23

    I have 3 numpy recarrays with following structure. The first column is some position (Integer) and the second column is a score (Float). Input: a = [[1, 5.41], [2, 5.42], [3, 12.32], dtype=[('position', '<i4'), ('score', '<f4')]) ] b = [[3, 8.4

  • Rails model how to create an array based on last value? 2011-12-28

    In my model I have: attr_accessor :ppris def last(x) @ppris = [] if @ppris == nil @ppris << 1 else @ppris << 2 end end def pakke if self.pakkes.count > 0 self.pakkes.each{|pakke| beregn_pakke(pakke.price)} end end In view a have a norm

  • Pick value from two-dimensional array based on value stored inside key? 2012-04-25

    Hi friends i am working on survey i need to extract an array from an array stored inside it how to do it i am looking to extract answer array based on question id for example 13 . i have the question id with me . answer array is associated with quest

  • How do I merge array values? 2012-01-25

    If I have an array: Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => F [7] => r [8] => i [9] => e [10] => n [11] => d ) And wanted to merge the values of the array based on the keys and end up wit

  • How do I efficiently locate key-value pairs in a multi-dimensional PHP array? 2010-06-10

    I have an array in PHP as a result of the following query to a Wordpress database: SELECT * FROM wp_postmeta WHERE post_id = :id I am returned a multidimensional array that looks like this: Array ( [0] => Array ( [meta_id] => 380 [post_id] =

  • Create a new array from numpy array based on the conditions from a list 2010-08-31

    Suppose that I have an array defined by: data = np.array([('a1v1', 'a2v1', 'a3v1', 'a4v1', 'a5v1'), ('a1v1', 'a2v1', 'a3v1', 'a4v2', 'a5v1'), ('a1v3', 'a2v1', 'a3v1', 'a4v1', 'a5v2'), ('a1v2', 'a2v2', 'a3v1', 'a4v1', 'a5v2'), ('a1v2', 'a2v3', 'a3v2',

  • Using PHP's foreach to create array of values from JSON data 2010-11-14

    I'm working with JSON data for the first time and I've got some PHP to grab some JSON data like below (except there are hundreds of measuregrps within body). $json = file_get_contents("http://wbsapi.withings.net/measure?action=getmeasures"); $json_o

  • How to sort a multi-dimensional array by values which are dates? 2011-02-22

    I am struggling with this. Sorting a multi-dimensional array by value based on dates. Here is a snippet of the array: Array ( [0] => Array ( [clicks] => 14 [point] => 11 February 2011 ) [1] => Array ( [clicks] => 1 [point] => 14 Feb

  • How to split an array into sub-arrays based on specific values in php 2011-03-04

    I have an array as shown below, what i wanted to do is, create multiple arrays based on the datepart like(2011-02-04): Array ( [0] => Array ( [avgvalue] => 0 [maxvalue] => 0 [minvalue] => 0 [nrsamples] => 0 [stddeviation] => 0 [time

  • Merge array of hashes to get hash of arrays of values 2011-03-30

    This is the opposite of Turning a Hash of Arrays into an Array of Hashes in Ruby. Elegantly and/or efficiently turn an array of hashes into a hash where the values are arrays of all values: hs = [ { a:1, b:2 }, { a:3, c:4 }, { b:5, d:6 } ] collect_va

  • Creating an associative array with random values PHP 2011-05-07

    I am trying to generate an associate array with random values. For example, if I give you this string: something, anotherThing, foo, bar, baz (the length of the string is dynamic - so there could be 10 items, or 15); I would like to create an array b

  • create php array based on another array. 2011-07-28

    I want to sort all php arrays based on first main array. This is the main array which I want to use to sort all other arrays: Array ( [0] => 10 [1] => 14 [2] => 15 [3] => 20 [4] => 21 [5] => 24 [6] => 25 [7] => 28 [8] => 30

  • PHP - Create a single array from a multidimensional array based on key name 2011-09-14

    I know there are a lot of answers on multi-dimensional arrays but I couldn't find what I was looking for exactly. I'm new to PHP and can't quite get my head around some of the other examples to modify them. If someone could show me the way, it would

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.195 (s). 11 q(s)