Rebuild array multidimensional - php

For a project i am trying to rebuild a array that the database returned. This data is needed to populate a graph.
array
0 =>
array
'payment_method_stats_id' => string '1' (length=1)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-01-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'iDeal' (length=5)
'month' => string 'January' (length=7)
1 =>
array
'payment_method_stats_id' => string '49' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-02-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'iDeal' (length=5)
'month' => string 'February' (length=8)
2 =>
array
'payment_method_stats_id' => string '97' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-03-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'iDeal' (length=5)
'month' => string 'March' (length=5)
3 =>
array
'payment_method_stats_id' => string '97' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-03-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'Paypal' (length=5)
'month' => string 'January' (length=5)
4 =>
array
'payment_method_stats_id' => string '97' (length=2)
'payment_method_id' => string '92' (length=2)
'country_id' => string '1' (length=1)
'total' => string '2' (length=5)
'timestamp' => string '2014-03-17 13:39:41' (length=19)
'payment_methods_id' => string '92' (length=2)
'definition' => string 'Visa' (length=5)
'month' => string 'January' (length=5)
The database returns a array with payment methods and with the month. so ideal is returned 12 times only with a different month.
now i need this data for a graph and i want to rebuild this array into this
'Januari'
=>payment
=> values, values, values, values values,
=>payment
=> values, values, values, values, values
how could i accomplish this

Related

Getting array value by string key

I have array and i nees access to key:
var_dump($load);
array (size=18)
'4' =>
array (size=3)
0 => string '12.3%' (length=5)
1 => string '17.8%' (length=5)
2 => string '11.8%' (length=5)
'9' =>
array (size=3)
0 => string '5.0%' (length=4)
1 => string '8.2%' (length=4)
2 => string '8.9%' (length=4)
'24' =>
array (size=3)
0 => string '53.1%' (length=5)
1 => string '86.1%' (length=5)
2 => string '53.6%' (length=5)
'7' =>
array (size=3)
0 => string '95.1%' (length=5)
1 => string '90.1%' (length=5)
2 => string '80.7%' (length=5)
Hove a can get data on key?
var_dump($load["7"]);
var_dump($load["'7'"]);
var_dump($load['"7"']);
var_dump($load[7]);
All var_dump return null;
Var dump array keys, Keys match.
$keys=array_keys($load);
var_dump($keys);
array (size=18)
0 => string '14' (length=2)
1 => string '60' (length=2)
2 => string '19' (length=2)
3 => string '7' (length=1)
4 => string '15' (length=2)
5 => string '24' (length=2)
6 => string '1' (length=1)
7 => string '16' (length=2)
8 => string '5' (length=1)
9 => string '11' (length=2)
10 => string '17' (length=2)
11 => string '18' (length=2)
12 => string '3' (length=1)
13 => string '6' (length=1)
14 => string '2' (length=1)
15 => string '10' (length=2)
16 => string '9' (length=1)
17 => string '4' (length=1)

error data in loop foreach does'nt display good syntax

I don't understand why in my foreach loop I can't use
$products_option_value['products_option_value_id'] for example. I must use
$option['products_option_value']['products_option_value_id'] to display the good result else I have just a value
Below the elements
var_dump($options_array);
array (size=7)
0 =>
array (size=7)
'products_option_id' => string '213' (length=3)
'products_option_value' =>
array (size=13)
'products_option_value_id' => string '171' (length=3)
'option_value_id' => string '179' (length=3)
'name' => string 'S' (length=1)
'image' => null
'quantity' => string '100' (length=3)
'subtract' => string '0' (length=1)
'price' => string '1.0000' (length=6)
'price_prefix' => string '+' (length=1)
'weight' => string '0.00' (length=4)
'weight_prefix' => string '+' (length=1)
'customers_group_id' => string '99' (length=2)
'products_option_model' => string '99' (length=2)
'option_tax_class_id' => string '99' (length=2)
'option_id' => string '40' (length=2)
'name' => string 'Taille' (length=6)
'type' => string 'select' (length=6)
'value' => null
'required' => null
etc
foreach ($options_array as $option) {
foreach ($option['products_option_value'] as $products_option_value) {
var_dump($products_option_value);
}
}
the result is :
var_dump($option); result
array (size=7)
'products_option_id' => string '213' (length=3)
'products_option_value' =>
array (size=13)
'products_option_value_id' => string '171' (length=3)
'option_value_id' => string '179' (length=3)
'name' => string 'S' (length=1)
'image' => null
'quantity' => string '100' (length=3)
'subtract' => string '0' (length=1)
'price' => string '1.0000' (length=6)
'price_prefix' => string '+' (length=1)
'weight' => string '0.00' (length=4)
'weight_prefix' => string '+' (length=1)
'customers_group_id' => string '99' (length=2)
'products_option_model' => string '99' (length=2)
'option_tax_class_id' => string '99' (length=2)
'option_id' => string '40' (length=2)
'name' => string 'Taille' (length=6)
'type' => string 'select' (length=6)
'value' => null
'required' => null
var_dump($products_option_value) result
products_info_options_new.php:84:string '171' (length=3)
products_info_options_new.php:84:string '179' (length=3)
products_info_options_new.php:84:string 'S' (length=1
etc
http://php.net/manual/en/control-structures.foreach.php
You should use foreach($array as $key => $value) when iterating trough arrays that have keys. (I mean arrays such as this: array("key" => $value), and this is also your array)

push all the records from DB into one hash in PHP

I am trying to push all the records from DB into one hash (associative array) on the basis of uid (as key) below.
Sample dump of Records from DB:
array (size=4)
'id' => string '18' (length=2)
'job_title' => string 'Rubyyyy' (length=4)
'job_skills' => string 'Rubyyyy' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '19' (length=2)
'job_title' => string 'Ruby on Rails' (length=13)
'job_skills' => string 'Ruby on Rails' (length=13)
'uid' => string '2' (length=1)
array (size=4)
'id' => string '20' (length=2)
'job_title' => string 'Ruby' (length=4)
'job_skills' => string 'Ruby' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '21' (length=2)
'job_title' => string 'Rubyon' (length=13)
'job_skills' => string 'Rubyon' (length=13)
'uid' => string '2' (length=1)
Expected Output:
array (size=1)
1 =>
array (size=4)
'id' => string '20' (length=2)
'job_title' => string 'Ruby' (length=4)
'job_skills' => string 'Ruby' (length=4)
'uid' => string '1' (length=1)
array (size=4)
'id' => string '18' (length=2)
'job_title' => string 'Rubyyyy' (length=4)
'job_skills' => string 'Rubyyyy' (length=4)
'uid' => string '1' (length=1)
2 =>
array (size=4)
'id' => string '21' (length=2)
'job_title' => string 'Rubyon' (length=13)
'job_skills' => string 'Rubyon' (length=13)
'uid' => string '2' (length=1)
array (size=4)
'id' => string '19' (length=2)
'job_title' => string 'Ruby on Rails' (length=13)
'job_skills' => string 'Ruby on Rails' (length=13)
'uid' => string '2' (length=1)

fill missing dates for specific key and value

I have an array:
array (size=3)
0 =>
array (size=3)
'quantity' => string '20' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '2' (length=1)
1 =>
array (size=3)
'quantity' => string '10' (length=2)
'date' => string '2016-03-01' (length=10)
'eID' => string '2' (length=1)
2 =>
array (size=3)
'quantity' => string '15' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '1' (length=1)
how can i fill missing date for each 'eID'? so the output would be:
array (
0 =>
array (
'quantity' => string '20' (length=2)
'date' => string '2016-01-01' (length=10)
'eID' => string '2' (length=1)
1 =>
array (
'quantity' => null
'date' => string '2016-02-01' (length=10)
'eID' => string '2' (length=1)
2 =>
array (
'quantity' => string '10' (length=2)
'date' => string '2016-03-01' (length=10)
'eID' => string '2' (length=1)
...until 2016-12-01. 12 arrays for each 'eID' key with same value

php display multilevel treenode menu recursive

I have a post regarding this which was answered though I now have a different problem related to the same topic. You may want to reference here for the original question. php display multilevel treenode menu
Now that the array is displaying by tree-node, it seems that it is not fully recursive. I notice that when the first element is a child node, it is not displaying as a child.
I tried doing a krsort then ksort.
function getChildren(&$rows, $p = 0) {
$r = array();
krsort($rows);
foreach($rows as $row_id => $row) {
if ($row['parent_node']==$p) {
$r[$row['product_category_code']] = getChildren($rows, $row['product_category_code']);
unset($rows[$row_id]);
}
}
ksort($rows);
return $r;
}
Here's the structure of the array:
array
0 =>
array
'product_category_code' => string 'akamia' (length=6)
'product_category_desc' => string 'Akamia' (length=6)
'parent_node' => string 'summer-dress' (length=12)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-04-01 10:03:42' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-04-01 10:03:47' (length=19)
1 =>
array
'product_category_code' => string 'bracelets' (length=9)
'product_category_desc' => string 'Bracelets' (length=9)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:04:08' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-14 22:09:05' (length=19)
2 =>
array
'product_category_code' => string 'floral-dress' (length=12)
'product_category_desc' => string 'Floral Dress' (length=12)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:49' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-04-01 10:03:30' (length=19)
3 =>
array
'product_category_code' => string 'flowery-bracelets' (length=17)
'product_category_desc' => string 'Flowery Bracelets' (length=17)
'parent_node' => string 'bracelets' (length=9)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:16' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:08:44' (length=19)
4 =>
array
'product_category_code' => string 'small-flowery-bracelets' (length=23)
'product_category_desc' => string 'Small Flowery Bracelets' (length=23)
'parent_node' => string 'flowery-bracelets' (length=17)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:08:35' (length=19)
'modified_by' => string '1' (length=1)
'modified_date' => string '2014-03-30 19:09:44' (length=19)
5 =>
array
'product_category_code' => string 'summer-dress' (length=12)
'product_category_desc' => string 'Summer Dress' (length=12)
'parent_node' => string '' (length=0)
'inactive' => string '0' (length=1)
'sort' => string '0' (length=1)
'created_by' => string '1' (length=1)
'created_date' => string '2014-03-14 22:09:29' (length=19)
'modified_by' => string '0' (length=1)
'modified_date' => null
The output (without the krsort / ksort) is showing this, which is wrong:
Akamai
Bracelets
Flowery Bracelets
Small Flowery Bracelets
Floral Dress
Summer Dress
This should be the output:
Bracelets
Flowery Bracelets
Small Flowery Bracelets
Floral Dress
Summer Dress
Akamai
Never mind, I got the answer. The $p on function getChildren(&$rows, $p = 0) should be $p = '' instead of a zero (0) since the parent node on the array is empty. Silly me. XD

Categories