How i can Convert an array in PHP to Use AnyChart - php

I want to create some graphs stuff,but i'm having some problems to convert an array Key => Value to X = Key,Value=>Value.
AnyChart needs this:
x: "A", value: 637166
and i Have this:
x => Value
I Trying something like that:
$dadosproc=array();
$a=0;
foreach($dados as $key => $value){
array_push($dadosproc,$dadosproc[$a]["x"]= $key,$dadosproc[$a++]["value"]=$value);
};

My guess is that you might want an output array maybe similar to,
$dadosproc = array();
$a = 0;
$dados = ["x1" => 637166, "x2" => 637168];
foreach ($dados as $key => $value) {
$dadosproc[$a]["key"] = $key;
$dadosproc[$a]["value"] = $value;
$a++;
}
var_dump($dadosproc);
Output
array(2) {
[0]=>
array(2) {
["key"]=>
string(2) "x1"
["value"]=>
int(637166)
}
[1]=>
array(2) {
["key"]=>
string(2) "x2"
["value"]=>
int(637168)
}
}

Related

Combining array values for the same associative key

I've got this array, and I want to loop through it and add up the values prices that are on the same OrderDate. The other values like the Discount code I want to add as a sub-array.
array(3) {
[0]=>
array(4) {
["OrderDate"]=>
string(10) "2018-01-01"
["DiscountCode"]=>
NULL
["TotalRevenue"]=>
string(9) "147618.76"
["Discount_Revenue"]=>
string(8) "13453.77"
}
[1]=>
array(4) {
["OrderDate"]=>
string(10) "2018-01-01"
["DiscountCode"]=>
string(6) "SALE38"
["TotalRevenue"]=>
string(8) "364.92"
["Discount_Revenue"]=>
string(8) "4083.64"
}
[2]=>
array(4) {
["OrderDate"]=>
string(10) "2018-01-01"
["DiscountCode"]=>
string(9) "WELCOME20"
["TotalRevenue"]=>
string(6) "113.83"
["Discount_Revenue"]=>
string(6) "113.83"
}
}
So it should then look like:
array(3) {
[0]=>
array(4) {
["OrderDate"]=>
string(10) "2018-01-01"
["DiscountCodes"]=> array {
[0] => "DISCOUNT"
[1] => "SALE38"
[2] => "WELCOME20"
)
["TotalRevenue"]=>
string(9) "147618.76"
["Discount_Revenue"]=>
string(8) "13453.77"
}
}
I believe I have fixed it using this loop adding to the array if the key exists. Not sure if this is the most efficient way to do it though?
foreach ($results as $k => $result){
if( array_key_exists($result['OrderDate'], $arr)){
$arr[$result['OrderDate']]['price'] += $result['TotalRevenue'];
$arr[$result['OrderDate']]['new'] = false;
} else {
$arr[$result['OrderDate']] = array(
'price' => $result['TotalRevenue'],
'new' => true
);
}
}
I've come to my own solution if anyone else needs it.
$arr = array();
foreach ($results as $k => $result){
if( array_key_exists($result['OrderDate'], $arr)){
$arr[$result['OrderDate']]['Total_Revenue'] += $result['TotalRevenue'];
$arr[$result['OrderDate']]['Discount_Revenue'] += $result['Discount_Revenue'];
isset($result['DiscountCode']) ? $arr[$result['OrderDate']]['Discount_Code'][] = $result['DiscountCode'] : '';
$arr[$result['OrderDate']]['new'] = false;
} else {
$arr[$result['OrderDate']] = array(
'Total_Revenue' => $result['TotalRevenue'],
'Discount_Revenue' => $result['Discount_Revenue'],
'new' => true
);
isset($result['DiscountCode']) ? $arr[$result['OrderDate']]['Discount_Code'][] = $result['DiscountCode'] : '';
}
}

How can I flatten a Multidimensional array into a string?

Ive researched this but im coming up blank, Im generating an array of tests from a database like so:
$descriptions = array();
foreach ($tests as $value) {
array_push($descriptions, ['name' => $value['name']]);
}
I'm getting the desired out put but i'm getting a Multidimensional array of an array with '[64]' arrays inside '$descriptions', I need to convert this array so I get the following output:
'name' => $value1, 'name' => $value2, etc etc for all results,
I've tried implode, array_merge etc but the closest I've got is a flat array with only my last test: [name] => Zika can anyone point me in the right direction? cheers
You can't have duplicate array keys. But you can pass an array in like so:
<?php
$descriptions = array();
$tests = array(
'Zika', 'SARS', 'AIDS', 'Mad Cow Disease', 'Bird Flu', 'Zombie Infection',
);
foreach ($tests as $value) {
$descriptions[] = array('name' => $value);
}
var_dump($descriptions);
Which gives you :
array(6) { [0]=> array(1) { ["name"]=> string(4) "Zika" } [1]=> array(1) { ["name"]=> string(4) "SARS" } [2]=> array(1) { ["name"]=> string(4) "AIDS" } [3]=> array(1) { ["name"]=> string(15) "Mad Cow Disease" } [4]=> array(1) { ["name"]=> string(8) "Bird Flu" } [5]=> array(1) { ["name"]=> string(16) "Zombie Infection" } }
So you could foreach ($descriptions as $desc) and echo $desc['name']';
Have a look here: https://3v4l.org/pWSC6
If you just want a string, try this:
<?php
$descriptions = '';
$tests = array(
'Zika', 'SARS', 'AIDS', 'Mad Cow Disease', 'Bird Flu', 'Zombie Infection',
);
foreach ($tests as $value) {
$descriptions .= 'name => '.$value.', ';
}
$descriptions = substr($descriptions, 0, -2); // lose the last comma
echo $descriptions;
Which will output:
name => Zika, name => SARS, name => AIDS, name => Mad Cow Disease, name => Bird Flu, name => Zombie Infection
See it here https://3v4l.org/OFGF4

Combine multiple form array into 1 array

["trnx_date"]=>
array(2) {
[0]=>
string(10) "2017-01-10"
[1]=>
string(10) "2017-01-10"
}
["curr_from"]=>
array(2) {
[0]=>
string(3) "USD"
[1]=>
string(3) "PHP"
}
["curr_from_amt"]=>
array(2) {
[0]=>
string(8) "4,000.00"
[1]=>
string(8) "3,000.00"
}
["curr_to"]=>
array(2) {
[0]=>
string(3) "GBP"
[1]=>
string(3) "SAR"
}
["curr_to_amt"]=>
array(2) {
[0]=>
string(8) "3,000.00"
[1]=>
string(8) "2,000.00"
}
["amount"]=>
array(2) {
[0]=>
string(8) "7,000.00"
[1]=>
string(8) "5,000.00"
}
I have the above array which was being submitted. This input was in sets of multiple field which was generated by dynamic table row. How can I group this into 1 (one) array so that I could save in the database? Like this:
[cust_row] => array(
'tranx_date' => "2017-01-10",
'curr_from' => "USD",
'curr_from_amt' => "4,000.00",
'curr_to' => "GBP",
'curr_to_amt' => "3,000.00",
'amount' => "7,000.00"
),
[cust_row] => array(
'tranx_date' => "2017-01-10",
'curr_from' => "PHP",
'curr_from_amt' => "3,000.00",
'curr_to' => "SAR",
'curr_to_amt' => "2,000.00",
'amount' => "5,000.00"
),
All of the above we being populated like this:
$trnx_date = $this->input->post('trnx_date');
$curr_from = $this->input->post('curr_from');
$curr_from_amt = $this->input->post('curr_from_amt');
$curr_to = $this->input->post('curr_to');
$curr_to_amt = $this->input->post('curr_to_amt');
$amount = $this->input->post('amount');
Assuming all the sub-arrays have the same length, you can use a simple for loop that iterates over the index of one of them, and use that to access each of the sub-arrays at that index. Then combine all of them into the associative array for each customer.
$result = array();
$keys = array_keys($array);
$len = count($array[$keys[0]]); // Get the length of one of the sub-arrays
for ($i = 0; $i < $len; $i++) {
$new = array();
foreach ($keys as $k) {
$new[$k] = $array[$k][$i];
}
$result[] = $new;
}
$arr = array(
'trnx_date' => array('2017-01-10', '2017-01-10'),
'curr_from' => array('USD', 'PHP'),
'curr_from_amt' => array('4,000.00', '3,000.00'),
'curr_to' => array('GBP', 'SAR'),
'curr_to_amt' => array('3,000.00', '2,000.00'),
'amount' => array('7,000.00', '5,000.00')
);
$arr_out = array();
$arr_keys = array_keys($arr);
for($i = 0; $i < count($arr[$arr_keys[0]]); $i++) {
$new_arr = array();
foreach($arr_keys as $key => $value) {
$new_arr[$value] = $arr[$value][$i];
}
$arr_out[] = $new_arr;
}
var_dump($arr_out);
Hope it helps!
How about this?
// assume $arr is your original data array
$keys = array_keys($arr);
$result = array();
foreach($keys as $key) {
$vals = $arr[$key];
foreach($vals as $i =>$val) {
if (!is_array($result[$i]) {
$result[$i] = array();
}
$result[$i][$key] = $val;
}
}
var_dump($result);
EDIT: added array check for $result[$i]

php How to concatenate 2 arrays in one array [duplicate]

This question already has answers here:
How to get all combinations from multiple arrays?
(2 answers)
Closed 5 months ago.
I have the following array with 2 elements:
$attribute_metric = array(2)
{
[0]=>
array(2) {
[0]=>
string(5) "white"
[1]=>
string(6) " Black"
}
[1]=>
array(3) {
[0]=>
string(1) "S"
[1]=>
string(2) " L"
[2]=>
string(2) " M"
}
}
and I want to concatenate its elements in a way where I get one array that has 6 elements
array(6) {
[0]=>
string(8) "white, S"
[1]=>
string(8) "white, L"
[2]=>
string(8) "white, M"
[3]=>
string(8) "Black, S"
[4]=>
string(8) "Black, L"
[5]=>
string(8) "Black, M"
}
I have tried the following but it's not working:
$size = 1;
foreach ($attribute_metric as $key => $value) {
$size = $size * sizeof($value);
}
foreach ($attribute_metric as $key => $value) {
if($key > 0){
foreach ($attribute_metric[0] as $subkey => $subvalue) {
array_push($subvalue,$value);
}
}
}
$array1= $attribute_metric[0]; //['white','black']
$array2= $attribute_metric[1]; // ['S', 'L', 'M']
$resultArray = [];
foreach ($array1 as $color){
foreach ($array2 as $size){
$resultArray[] = $color . ', ' . $size;
}
}
print_r($resultArray);
results
Array
(
[0] => white, S
[1] => white, L
[2] => white, M
[3] => black, S
[4] => black, L
[5] => black, M
)
If the original arrays are not too large then using nested loops would allow you to generate the desired output.
$colours=array('white','black');
$sizes=array('s','m','l');
$out=array();
foreach( $colours as $colour ){
foreach( $sizes as $size ){
$out[]=$colour.', '.$size;
}
}
print_r( $out );
Try this
<?php
$list = array(array('white','Black'), array('S','L','M'));
$result = array();
foreach ($list[0] as $k) {
foreach ($list[1] as $t) {
$result[] = $k.','.$t;
}
}
var_dump($result);
?>
Just for completeness sake and if you want to do it just using array functions:
$output = array_reduce(array_map(function($a) use($attribute_metric) {
return array_map(function($b) use($a) { return $a . ', ' . $b; }, $attribute_metric[1]);
}, $attribute_metric[0]), 'array_merge', []);

Create array with keys coming from multiple arrays

I have an array that looks like this:
array(5) {
[0]=>
array(2) {
["id"]=>
string(2) "23"
["my_value"]=>
NULL
}
[1]=>
array(2) {
["id"]=>
string(2) "62"
["my_value"]=>
NULL
}
...
I would like to have an array that as keys have the value of the key "id" in each array and as value have the value of "my_value". However if "my_value" is NULL I want to set a value of 100.
So, the result array would be like this:
array(5) {
[23] => 100
[62] => 100
...
How can I do that cleanly? I have been iterating over with a foreach but I believe it can be done cleaner...
You can use array_map() for populate my_value
$newData = array_map(function($row) {
if ( $row['my_value'] === null ) $row['my_value'] = 100;
return $row;
}, $data);
But you already need a foreach loop because of formatting. So try this:
$newData = array();
foreach ($data as $row) {
$newData[$row['id']] = ($row['my_value'] === null) ? 100 : $row['my_value'];
}
You could do like this:
$arrayUsers = array();//declare array
$arrUser = array("id"=>"23","myvalue"=>NULL); //User 23
$arrayUsers[$arrUser["id"]] = $arrUser; //add
$arrUser = array("id"=>"62","myvalue"=>NULL); //User 62
$arrayUsers[$arrUser["id"]] = $arrUser; //add
var_dump($arrayUsers);
the result is this:
array(2)
{
[23]=> array(2)
{
["id"]=> string(2) "23"
["myvalue"]=> NULL
}
[62]=> array(2)
{
["id"]=> string(2) "62"
["myvalue"]=> NULL
}
}
[EDIT]
$valueArray = array();
foreach($arrayUsers as $id=>$value)
{
$val = ($value["myvalue"]===NULL?100:$value["myvalue"]);
$valueArray[$id] = $val;
}
var_dump($valueArray);
This should behave like you want
$arr = array(5 => array('id'=>23, 'myvalue'=>null),
1 => array('id'=>62, 'myvalue'=>null));
$callback = function($v) {
$id = $v['id'];
$myv = !is_null($v['myvalue']) ? $v['myvalue'] : 100;
return array($id=>$myv);
}
$newarr = array_map($callback, $arr);
you should do it:
foreach($old_array as $value)
$new_array[$value['id']]= ($value['my_value']!==NULL) ? $value['my_value'] : 100 ;
the result will be
array(5) {
[23] => 100
[62] => 100
...
running code example at Codepad.org

Categories