PHP - Multidimensional Array, Summing Same Keys
Code: [Select]
array ( 0 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 1 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 2 => array ( 'id' => '2', 't' => 'Automotive', ), ) how to sum it, to get: Code: [Select] array ( 0 => array ( 'id' => '6', 't' => 'Food & Beverage', 'cnt' => '2' ), 1 => array ( 'id' => '2', 't' => 'Automotive', 'cnt' => '1' ), ) Similar TutorialsCode: [Select] array ( 0 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 1 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 2 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 3 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 4 => array ( 'id' => '2', 't' => 'Automotive', ), 5 => array ( 'id' => '2', 't' => 'Automotive', ), ) I've get after launching. foreach($array as $value) { if(!isset($new_array[$value['id']])) { $new_array[$value['id']] = $value; $new_array[$value['id']]['cnt'] = 0; } $new_array[$value['id']]['cnt']++; } I get. Code: [Select] array ( 6 => array ( 'id' => '6', 't' => 'Food & Beverage', 'cnt' => 10, ), 2 => array ( 'id' => '2', 't' => 'Automotive', 'cnt' => 2, ), ) where I should get. Code: [Select] array ( 6 => array ( 'id' => '6', 't' => 'Food & Beverage', 'cnt' => [b]4[/b], ), 2 => array ( 'id' => '2', 't' => 'Automotive', 'cnt' => 2, ), ) How can I fix this issue? ----------------------------------------------------- I've received solution on other board. $array = array ( 0 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 1 => array ( 'id' => '6', 't' => 'Food & Beverage', ), 2 => array ( 'id' => '2', 't' => 'Automotive', ), ); $return = array(); $count = array(); foreach( $array as $item ) { if ( !in_array($item, $return) ) { $return[] = $item; $count[ $item['id'] ] = 1; } else { $count[ $item['id'] ]++; } } foreach( $return as & $item ) { $item['cnt'] = (int)$count[ $item['id'] ]; } var_dump( $return ); thank you --- the code above works too, I've put it in WHILE() and didn't noticed it. Sorry Thanks Say I have this array: Code: [Select] <?php $array = array('one' => array(), 'two' => array(), '_three' => array()); ?> How could I sort that array so it ends up like so: Code: [Select] Array ( [_three] => Array ( ) [one] => Array ( ) [two] => Array ( ) ) Thanks!! Hi there, Basic question, but I can't seem to find a page in the PHP docs that answers (though I'm sure such a one exists, I just can't seem to think up the right search terms - anyway) My question is: is there a short-hand way to use a value from an associative array as the key in another associative array? So for example, given this array: $the_array = array(0 => array('name': 'the_name', 'value' : 'the_value'), 1 => etc....); then this kind of foreach loop doesn't work Code: [Select] $new_data = array(); foreach($the_array as $element){ $new_data[$element['name']] = $element['value']; } Obviously I can just save out the values, like $name = $element['name'] --- but I was just wondering if there was a more efficient way to do it? Cheers, PS. I tried encapsulating with {} - i.e. $new_data[{$element['name']}] --- but that also fails. Is it possible to create a PHP array that contains only keys? array 1 3 5 6 7
I have a query that returns row ID's that I want to put into an array to do some work. There is no real concept of a "value" - I just need to store a list of the database table rows that i want and that's it! Edited February 16 by SaranacLake Hi .. i have an array format like (mysql result array) $new_array=Array ( [0] => Array ( [quantity] => 5 ) [1] => Array ( [quantity] => 25 ) [2] => Array ( [quantity] => 20 ) ) I have to convert this array into the following format $new_array['quantity'][0]=5 $new_array['quantity'][1]=25 $new_array['quantity'][2]=20 How to convert this array format using any standard array method. hey guys im trying to result an array key and value if it has one...the code and results are below if anyone can help with this simple problem...thanks alot Code: [Select] <?php $columns = array('news_id' => 'id', 'news'); foreach ($columns as $column => $column_alias) { echo $column . ' ' . $column_alias; } ?> the result im getting is: news_id id 0 news but im just after the result of just result the column name and alias (if it has one) so im looking for the result news_id id news Hello I am having some trouble with array_keys I have an array for which I want to return all the keys based on value X This is my code: Code: [Select] $depth = array_keys(X', $Array); echo print_r ($depth,1); I have checked the content of my array $Array and it contains at least 1 instance of 'X' but I am getting nothing when I do the print_r Any suggestions would be appreciated Thanks is there a way to reset the array keys? to 0,1,2,3,... also for example that i have something like this: $array['dog']='dog'; $array['mouse']='mouse'; $array['cat']='cat'; and i want it like this: $array[0]='dog'; $array[1]='mouse'; $array[2]='cat'; is there a function for that already in php? I have an array of type Code: [Select] Array ( [2] => Array ( [name] => Shelby Baptist Medical Ctr [city] => Alabaster ) [1] => Array ( [name] => Andalusia Regional Hospital [city] => Andalusia ) [0] => Array ( [name] => Long Term Hospital of Anniston [city] => Anniston )) How can sort the keys of an array but there content remains the same so that they are like Code: [Select] Array ( [0] => Array ( [name] => Shelby Baptist Medical Ctr [city] => Alabaster ) [1] => Array ( [name] => Andalusia Regional Hospital [city] => Andalusia ) [2] => Array ( [name] => Long Term Hospital of Anniston [city] => Anniston )) Can someone please point me if there is any func. for this The title makes it sound more complicated than it is. I have a recurring function that searches for a specific value in a multi-dimensional array. When it finds it, it returns an array of the index. For example, if it found the value at $array[1][4][2][0], it would return an array like this: [0]=>1 [1]=>4 [2]=>2 [3]=>0 Here's the function. Code: [Select] <?php // Other Code /** * Find's the parent of an item * Unfortunately, it takes a bit more work than just using the loudspeaker at a grocery store * @param int $parent The parent to be searched for * @param array $array * @param array $cur_index */ private function findParent($parent, $array, $cur_index) { for($i = 0; $i < count($array); $i++) { // Search the array if($array[$i]['id'] == $parent) { // If the parent is found $cur_index[count($cur_index)] = $i; return array(true, $cur_index); } if(count($array[$i][1]) > 1) { // Call findParent again to search the child $cur_index[count($cur_index)] = $i; $found_in_child = $this->findParent($parent, $array[$i], $cur_index); if($found_in_child[0]) { return $found_in_child; } } } return array(false); // Return no matches } I need to be able to call that index using the keys from the key array (as we will call it). How can I do that? Thank you in advance for your help. I have a sequential multidimensional array with many outer rows where all the inner arrays have the same number of elements as another sequential array which represents metadata of the first array's inner arrrays. $keys: ["time","system1_typeA","system2_typeA","system1_typeB","system3_typeC","system2_typeB","system1_typeC"] $arrayValues: [ ["2020-06-27 07:40:06",2.16,9.25,9.44,7.76,5.43,1.12], ["2020-06-27 07:40:23",2.66,8.93,4.31,6.59,8.44,3.42], ["2020-06-27 07:41:24",8.71,2.78,8.92,6.58,3.65,4.38], ["2020-06-27 07:41:58",1.86,6.36,2.65,3.99,7.25,3.32], ["2020-06-27 07:42:33",8.71,3.66,8.82,8.54,8.22,4.51] ]
{ "time": ["2020-06-27 07:40:06", "2020-06-27 07:40:23", "2020-06-27 07:41:24", "2020-06-27 07:41:58", "2020-06-27 07:42:33"], "system1": { "typeA": [2.16, 2.66, 8.71, 1.86, 8.71], "typeB": [9.44, 4.31, 8.92, 2.65, 8.82], "typeC": [1.12, 3.42, 4.38, 3.32, 4.51] }, "system2": { "typeA": [9.25, 8.93, 2.78, 6.36, 3.66], "typeB": [5.43, 8.44, 3.65, 7.25, 8.22] }, "system3": { "typeC": [7.76, 6.59, 6.58, 3.99, 8.54] } } Is there much faster way to do so than the following? I would like to use a built in function like array_combine() if possible, but grouping by system and type creates an issue. function transform(array $keys, array $arrayValues):array { $map=[]; foreach ($keys as $key) { $map[] = explode('_',$key); } $count=count($keys); $newValues=[]; foreach ($arrayValues as $i=>$arrayValue) { $newValues['time'][]=$arrayValue[0]; for ($i = 1; $i < $count; $i++) { $newValues[$map[$i][0]][$map[$i][1]][]=$arrayValue[$i]; } } return $newValues; }
Hey guys so I've been trying to figure something out for a day now but I really don't know where to start, basically I'm trying to create some kind of function that goes through my array and checks if the keys value is less than 5, if it is then I want to unset the key removing it from the array,
I programmed this underneath as this is basically what I want to achieve however there's a syntax issue as the array_key_excists function doesn't seem to allow the if statement, if anyone knows anything about it it would be very helpful!
if (array_key_exists(($key > 5),$data)) { unset($data[$key]); }any help is very much appreciated, Thanks very much in advance! Hi guys, I am getting data from a json source: ``` $jsondata = file_get_contents($url);$response = json_decode($jsondata, true) echo "<pre>"; print_r(array_keys($response));; echo "</pre>"; ``` and this returns: ``` Array ( [0] => jsonapi [1] => data [2] => links ) ``` I want to extract data from specific key in the data array (eg 'title'): ``` [data] => Array ( [0] => Array ( [type] => recipes [id] => d6cd7643-6e44-4d26-9ed9-9da7196cef12 [attributes] => Array ( [internalId] => 7 [isPublished] => 1 [title] => Deep mediterranean quiche [createdAt] => 2019-05-07T13:34:27+0200 [updatedAt] => 2019-05-07T13:34:27+0200 [isPromoted] => 1 [path] => /recipes/deep-mediterranean-quiche [cookingTime] => 30 [difficulty] => medium [ingredients] => Array ( [0] => For the pastry: [1] => 280g plain flour [2] => 140g butter [3] => Cold water [4] => For the filling: [5] => 1 onion [6] => 2 garlic cloves [7] => Half a courgette [8] => 450ml soya milk [9] => 500g grated parmesan [10] => 2 eggs [11] => 200g sun dried tomatoes [12] => 100g feta ) [numberOfservings] => 8 [preparationTime] => 40 [instructions] => ``` How do I loop through the data array and select just the 'title' and 'difficulty' for example? Edited May 12, 2019 by ludo1960Hi, I have the following code that takes a list from a mySQL db and puts it into an array that shows 'gig date, venue, city' per entry: - Code: [Select] while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $array[] = ($row['date'].', '.$row['gl_venue'].', '.$row['gl_city']); } $output = json_encode($array); this outputs JSON like so: Code: [Select] ["06-05-2011, O'neills, Southend","07-05-2011, Power League, Peterborough","14-05-2011, Queen Victoria hall, Oundle",.......] how do I add keys into the array so I get something like: Code: [Select] {"gigs":[ { "gig": "date, venue, city",} ]} at least I think so anyway!?! What I want to do is eventually be able to parse a JSON listing into Objective-C and be able to call key/pairs into various different parts of an app. I'm pretty new to this so any advice would be great. Say i have two arrays with custom keys. What would be the easiest way to write a function that would determine if all the keys in arrayOne are in arrayTwo? The example below would return false because keys 3 and 4 are missing in arrayTwo. Any help would be much appreciate. thanks! Code: [Select] <?php $arrayOne['1'] = "adg"; $arrayOne['2'] = "a4g"; $arrayOne['3'] = "346"; $arrayOne['4'] = "etwe"; $arrayTwo['1'] = "xcb"; $arrayTwo['2'] = "acbr"; $arrayTwo['6'] = "yiy"; $arrayTwo['7'] = "mmm"; ?> Hi, I'm not sure if this is a wierd one, I have lots of arrays i'm looping through like this to see them:- for($i=0; $i <count($confirmed); $i++){//loop through print_r_html($confirmed[$i]); } Which shows the following :- Code: [Select] Array ( [0] => AAAAAA-BBBBBB [1] => 2 [2] => 2010-09-29 ) Array ( [0] => AAAAAA-BBBBBB [1] => 2 [2] => 2010-09-28 ) Array ( [0] => CCCCCC-DDDDDD-EEEEEE [1] => 3 [2] => 2010-09-29 ) Key 0 is a Groupconcat split by "-" Key 1 shows how many fields are in 0 Key 2 is just the date What I would like to do is split the first array into 2 arrays, the second in 2, and the third into 3 (therefore ungrouping the groupconcat if you will) as such:- Code: [Select] First into these two:- Array ( [0] => AAAAAA [1] => 2 [2] => 2010-09-29 ) Array ( [0] => BBBBBB [1] => 2 [2] => 2010-09-29 ) Second into these two:- Array ( [0] => AAAAAA [1] => 2 [2] => 2010-09-28 ) Array ( [0] => BBBBBB [1] => 2 [2] => 2010-09-28 ) Third into these two:- Array ( [0] => CCCCCC [1] => 3 [2] => 2010-09-29 ) Array ( [0] => DDDDDD [1] => 3 [2] => 2010-09-29 ) Array ( [0] => EEEEEE [1] => 3 [2] => 2010-09-29 ) ) I know I can get out the info from the first of the keys in each array by putting this into the loop:- for($i=0; $i <count($confirmed); $i++){//loop through if($i==0){ $seperated[] = explode("-",$confirmed[$i]);//split by "-" } } print_r_html($seperated); but i'm not sure how to copy the other keys at the same time? If anyone has got any ideas that would be great! thanks in advance Kev. I need some help converting my captured array values into keys and later add arrays as the values. This might help explain what I'm looking for: I currently have Code: [Select] Array ( [0] => Meat [1] => Veg ) But I'm after Code: [Select] Array ( Meat => Veg => ) My end result will be: Code: [Select] [Veg] => Array ( [Potatoe] => 12 [Cabbage] => 24 [Cucumber] => 56 ) [Meat] => Array ( [Chicken] => 23 [Beef] => 90 ) I think either array_values() may help but I'm not too sure how to use it. Hi, I'm trying to explode an array into a multidimensional array using <br /> tags as splitters but just can't get the syntax right! I can do lines of the array one by one, but not the whole lot at once. I'd really appreciate a quick example of an explode inside an array. $myarray looks something like this with print_r: ( [0] => [1] => some_html<br />some_html<br />some_html<br /> [2] => some_html<br />some_html<br />some_html<br /> [3] => some_html<br />some_html<br />some_html<br /> ) and I want to turn it into this: ( [0] => [1] => [0]some_html [1]some_html [2]some_html [2] => [0]some_html [1]some_html [2]some_html [3] => [0]some_html [1]some_html [2]some_html ) I've tried this $myarray = explode ("<br />", $myarray); but it doesn't work … It doesn't matter too much if there's are a few empty lines in the array from the final <br /> tags. Many thanks, as ever, for any help. I have the following array structu Code: [Select] [0] => Array ( [id] => Array ( [$t] => http://www.google.com/mate/ ) [updated] => Array ( [$t] => 2011-08-31T11:43:05.942Z ) [category] => Array ( [0] => Array ( [scheme] => http://schemas.google.com/g/ [term] => http://schemas.google.com/contact/ ) ) [title] => Array ( [type] => text [$t] => Name ) [link] => Array ( [0] => Array ( [rel] => http://schemas.google.com/contacts/2008/rel#edit-photo [type] => image/* [href] => https://www.google.com/mate/feeds/photos/media/ ) [1] => Array ( [rel] => self [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/contacts ) [2] => Array ( [rel] => edit [type] => application/atom+xml [href] => https://www.google.com/mate/feeds/ ) ) [gd$email] => Array ( [0] => Array ( [rel] => http://schemas.google.com/g/2005#other [address] => email_address@gmail.com [primary] => true ) ) ) I am tried to display name and email address. I am using following method, can anyone tell me is there any better way? Thank u 4u help. NAME $name=( $emp_det[0]['title'][t]); $email =( $emp_det[0]['gdemail'][0][address]); |