I was wondering if it's possible to merge an array by a value of a cell (no doubles) right of the php box.
To clarify this is my current array :
Array
(
[0] => Array
(
[Score] => 90
[Name] => David,
)
[1] => Array
(
[Score] => 90
[Name] => Eric,
)
[2] => Array
(
[Score] => 77
[Name] => Eva,
)
[3] => Array
(
[Score] => 77
[Name] => Gila,
)
)
So we have:
2 persons (David and Eric) that have the same score (90)
2 persons (Eva and Gila) that have the same score (77)
So by the end of the day (in that case) I want to get only 2 rows (merged by score) and the names will be added to the same line that merged by score.
This is what I'm expecting to get :
Array
(
[0] => Array
(
[Score] => 90
[Name] => David,Eric,
)
[1] => Array
(
[Score] => 77
[Name] => Eva,Gila,
)
)
Here is my php code :
<?php
header("Content-Type:text/plain");
$myArray=array();
$myArray[]=array('Score'=>'90','Name'=>'David,');
$myArray[]=array('Score'=>'90','Name'=>'Eric,');
$myArray[]=array('Score'=>'77','Name'=>'Eva,');
$myArray[]=array('Score'=>'77','Name'=>'Gila,');
print_r($myArray);
Thanks!
Related
I need to create a db function for multidimensional array. How deep the array currently dont know, bcoz they will come from xml file.
I have a sample array
Array
(
[employee] => Array
(
[0] => Array
(
[name] => Array
(
[lastname] => Kelly
[firstname] => Grace
)
[hiredate] => October 15, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Printer
[id] => 111
[price] => $111.00
)
[1] => Array
(
[product] => Laptop
[id] => 222
[price] => $989.00
)
)
)
)
[1] => Array
(
[name] => Array
(
[lastname] => Grant
[firstname] => Cary
)
[hiredate] => October 20, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Desktop
[id] => 333
[price] => $2995.00
)
[1] => Array
(
[product] => Scanner
[id] => 444
[price] => $200.00
)
)
)
)
[2] => Array
(
[name] => Array
(
[lastname] => Gable
[firstname] => Clark
)
[hiredate] => October 25, 2005
[projects] => Array
(
[project] => Array
(
[0] => Array
(
[product] => Keyboard
[id] => 555
[price] => $129.00
)
[1] => Array
(
[product] => Mouse
[id] => 666
[price] => $25.00
)
)
)
)
)
)
I need to enter these type of array to db and then retrieve them in a good non programmer readable format
I created 2 table... 1st for array key with array level field and another for key=value
I tried this
function array_Dump($array, $d=1){
if (is_array($array)){
foreach($array as $key=>$val){
for ($i=0;$i<$d;$i++){
$level=$i;
}
if (is_array($val)){
if (is_int($key)){
array_Dump($val, $d+1);
}else{
$query = "insert into xml_array (level, input) VALUES ('$level','$key')";
insert_sql($query);
array_Dump($val, $d+1);
}
} else {
$query = "insert into xml_data (array_id,level_id, array_key,array_value) VALUES ('$insert_id','$level','$key','$val')";
insert_sql($query);
}
}
}
}
Create a table like this:
attributes(id, parent_id, properties)
where id will be the primary key, parent_id will be the id of the parent record and properties will be a small json field with the atomic properties. This way you support any depth the XML may throw towards your direction.
As about non-programmer representation. For instance you could use a table (you can solve that with divs as well) which will contain a row for each element in the top level array. Such a row would contain separate columns for each property. When a property is an array, then the given cell will be a table of its own, which will be handled similarly as the first table. It is advisable to make the inner tables collapsible, so if one wants to see the main levels only, the user will not have to scroll for a long while.
I want to determine what is the type of filed in my array from below code.
foreach($question->questionsOptions as $option){
$servey_detail_arr['option'][$option->question_id][] = array('value'=>$option->label,'id'=>$option->option_id,'option'=>$option->label);
}
The above code create below array. Now i want to insert type of filed in my array. i have written in below array in comment where i want the value.
Array
(
[option] => Array
(
[96] => Array
(
//i want add here a value means type radio
[0] => Array
(
[value] => karachi
[id] => 49
[option] => karachi
)
[1] => Array
(
[value] => islamabad
[id] => 50
[option] => islamabad
)
)
[97] => Array
(
**i want add here a value means type datepicker**
[0] => Array
(
[value] => date
[id] => 53
[option] => date
)
)
[100] => Array
(
//i want add here a value means type checbox
[0] => Array
(
[value] => checkbox1
[id] => 55
[option] => checkbox1
)
[1] => Array
(
[value] => checkbox2
[id] => 56
[option] => checkbox2
)
)
)
)
Any other solution will be appreciate. Thanks
Try adding the below condition inside foreach loop
if (array_key_exists('96', $servey_detail_arr['option'][$option->question_id])) {
$servey_detail_arr['option'][$option->question_id][] = array('type'=>'radio');
}
if (array_key_exists('97', $servey_detail_arr['option'][$option->question_id])) {
$servey_detail_arr['option'][$option->question_id][] = array('type'=>'datepicker');
}
if (array_key_exists('98', $servey_detail_arr['option'][$option->question_id])) {
$servey_detail_arr['option'][$option->question_id][] = array('type'=>'checkbox');
}
Given this array:
Array
(
[0] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[1] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[2] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
[3] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
)
The ssm_featured_post_id value corresponds to the value of the array items in the second array.
I want to order the first array items in the same order as the items in the second array
Array
(
[1] => 63
[0] => 70
[3] => 1
[2] => 49
)
so the result after sorting would be
Array
(
[0] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[1] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[2] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
[3] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
)
The simpler way would be to use usort and write a function that uses the second table to compare two values from first table.
You may want to check out array_multisort, particularly the third example given. The idea is that you create arrays based on the "columns" of the multidimensional array, then sort them simultaneously, and put the result back in the original array.
I am working on an ad management system, and recently, we launched on a high traffic website to do some testing. I am storing each impression as a record in the db with a timestamp, ad ID, zone ID, and website ID.
I would select all the data between a date range, and use PHP to organize the records (by days, etc). That was inefficient so I make two queries that
GROUP BY date(`impressions`.timestamp)
First query to retrieve a count of impressions, second query to get a list of associated labels.
My problem is this:
I need to query to get not just the overall impressions, but the impressions by zone AND by day, and I cannot use PHP to organize this array of data because it is millions of points.
SELECT `zones`.name as zoneName, `impressions`.*
from `impressions`, `zones`
WHERE `impressions`.website_id = "14"
AND `zones`.website_id="14"
AND `zones`.zone_id=`impressions`.zone_id
AND `impressions`.timestamp
BETWEEN "2012-10-08 00:00:00" AND "2012-10-15 23:59:59"
ORDER BY `impressions`.timestamp ASC
That may return millions of records. Then I use PHP to sort the returned data into zones.
For example, this is the resulting array after my PHP sorting. This array represents the zones and the impressions they received for the given days. What would be a good query to use to get as close to this as possible to minimize memory overhead when dealing with large arrays?
Array
(
[labels] => Array
(
[Oct 8th] => "Oct 8th"
[Oct 9th] => "Oct 9th"
[Oct 10th] => "Oct 10th"
)
[final] => Array
(
[0] => Array
(
[name] => Blog Skyscraper
[data] => Array
(
[0] => 449
[1] => 499
[2] => 558
)
)
[1] => Array
(
[name] => Latest News Right
[data] => Array
(
[0] => 805
[1] => 809
[2] => 760
)
)
[2] => Array
(
[name] => Photos Right
[data] => Array
(
[0] => 788
[1] => 786
[2] => 743
)
)
[3] => Array
(
[name] => Banner Zone
[data] => Array
(
[0] => 793
[1] => 796
[2] => 747
)
)
[4] => Array
(
[name] => Mini Right Bottom
[data] => Array
(
[0] => 784
[1] => 778
[2] => 742
)
)
[5] => Array
(
[name] => Mini Right Top
[data] => Array
(
[0] => 790
[1] => 787
[2] => 743
)
)
)
)
hello i am learning PHP and came upon this multi-level array after using print_r on $this->root
Array (
[0] => 9
[obj] => 3562
[gen] => 0
[1] => Array (
[0] => 5
[1] => Array (
[/AcroForm] => Array (
[0] => 8
[1] => 3563
[2] => 0
)
[/Metadata] => Array (
[0] => 8
[1] => 3559
[2] => 0
)
[/PageLabels] => Array (
[0] => 8
[1] => 3389
[2] => 0
)
[/Pages] => Array (
[0] => 8
[1] => 3392
[2] => 0
)
[/Type] => Array (
[0] => 2
[1] => /Catalog
)
)
)
) Array (
[0] => 9
[obj] => 8
[gen] => 0
[1] => Array (
[0] => 5
[1] => Array (
[/Type] => Array (
[0] => 2
[1] => /Catalog
)
[/Pages] => Array (
[0] => 8
[1] => 1
[2] => 0
)
[/OpenAction] => Array (
[0] => 6
[1] => Array (
[0] => Array (
[0] => 8
[1] => 3
[2] => 0
)
[1] => Array (
[0] => 2
[1] => /FitH
)
[2] => Array (
[0] => 0
)
)
)
[/PageLayout] => Array (
[0] => 2
[1] => /OneColumn
)
)
)
)
i have an question about the behavior of using multi-level arrays, i want to use this function
$pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
and $this->root[1][1]['/Pages'] which i believe is used to check the array for these keys and if it exists then use as variable for pdf_resolve_object
so my question is 2-fold, one is does $this->root[1][1]['/Pages'] check the array and goes through the keys? if not what is its behavior? and 2 when it checks the array does it go through just the top 4 keys or all of the sub-keys?
If someone can help or link me to some learning material that would be much appreciated, thank you!
1) It does not check for the presence of the array keys -- rather it assumes that those keys already exist and passes the value into the function. If any of the keys did not exist, PHP would issue an E_NOTICE to the effect of Notice: Undefined index: that the key was not found. To check for them would require a call to isset() or array_key_exists() like:
if (isset($this->root[1][1]['/Pages'])) {
$pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
}
2) There is no need for it to iterate through to find the keys. Knowing the array keys already means they can be accessed directly without iteration. In memory, PHP has stored the array keys and the memory locations of the values they point to. Therefore, with the key alone, PHP can return the value without needing to traverse the array.
There is a lot of good information in the PHP manaul on Arrays