I got this code to fetch data from my Database to store in an Array in PHP:
<?php
$keyemail = "testmail#testmailer.com";
$sql = mysqli_query($con, "SELECT * FROM crypto_data WHERE email = '$keyemail'");
while($row = mysqli_fetch_array($sql)){
$myCoins[] = array($row['currency'], $row['amount'], $row['price_when_bought']);
}
var_dump($myCoins);
?>
The Array I get looks like this:
array(2) {
[0] => array(3) {
[0] => string(3) "BTC"[1] => string(1) "2"[2] => string(2) "23"
}
[1] => array(3) {
[0] => string(3) "IOT"[1] => string(1) "6"[2] => string(2) "74"
}
}
How can I change my Code to get the Array listed as below?
So that the $row['currency'] is the array Title of each Array.
array(2) {
["BTC"] => array(2) {
["balance"] => int(2) ["boughtprice"] => int(23)
}
["ETH"] => array(2) {
["balance"] => int(6) ["boughtprice"] => int(74)
}
}
Just change
$myCoins[] = array($row['currency'], $row['amount'], $row['price_when_bought']);
to
$myCoins[$row['currency']] = array('balance' => $row['amount'],
'boughtprice' => $row['price_when_bought']);
Related
I need to store an array in this format
array(2)
{
[1002] => array(2)
{
[0] => string(1) "2"
[1] => string(1) "1"
}
}
So far i only have this:
function permissions($conn){
$sql="SELECT PROCESS,PERMISSION FROM table WHERE PROCESS=1002";
$result = $conn->executeSQL($sql);
$return = array();
foreach($result as $value){
array_push($return,$value['PROCESS_ID'],$value['PERMISSION_ID']);
}
which brings:
I donĀ“t know how to make this array multidimensional, i don't want the "1002" to repeat
array(2)
{
[0] => string(4) "1002"
[1] => string(1) "2"
[2] => string(4) "1002"
[3] => string(1) "1"
}
Hi try something like this:
function permissions($conn){
$sql="SELECT PROCESS,PERMISSION FROM table WHERE PROCESS=1002";
$result = $conn->executeSQL($sql);
$return = array();
foreach($result as $value){
if(!isset($return[$value['PROCESS_ID']])){
$return[$value['PROCESS_ID']] = [];
}
$return[$value['PROCESS_ID']][] = $value['PERMISSION_ID'];
}
I have an array like this :
array(3) {
["data"]=> array(1) {
["mesq_G3SC"]=> array(1) {
["data"]=> object(stdClass)#30 (19) {
["cart_id"]=> string(13) "mesq_G3SC"
["qty"]=> string(4) "2.00"
["price"]=> string(5) "11400"
["product_id"]=> string(1) "6"
["member_id"]=> string(1) "5"
["session_id"]=> string(32) "4dedde2a2eb12b25e940b7051a5f65a1"
["date_added"]=> string(19) "2016-09-28 10:39:06"
["date_modified"]=> string(19) "2016-09-28 10:39:06"
["status"]=> string(7) "pending"
["category_id"]=> string(1) "2"
["location"]=> string(9) "England"
["square"]=> string(3) "300"
["stock"]=> string(3) "260"
["folder_name"]=> string(23) "assets/uploads/products"
["photo1"]=> string(11) "photo1.jpg"
["photo2"]=> string(11) "pc.jpg"
["category_name"]=> string(6) "PC"
["is_published"]=> string(1) "1"
["modified_by"]=> NULL
}
}
}
["total_item"]=> int(1)
["total_price"]=> string(8) "11400.00"
}
I would to get each value of that array with foreach but I got error
Trying to get property of non-object...
I developed this with Codeigniter and this is my foreach:
foreach ($data as $row) {
echo $row->product_id;
}
This is what I get if I do print_r($row) :
Array ( [mesq_G3SC] => Array ( [data] => stdClass Object ( [cart_id] => mesq_G3SC [qty] => 2.00 [price] => 11400 [product_id] => 6 [member_id] => 5 [session_id] => 4dedde2a2eb12b25e940b7051a5f65a1 [date_added] => 2016-09-28 10:39:06 [date_modified] => 2016-09-28 10:39:06 [status] => pending [category_id] => 2 [lokasi_lahan] => England [square] => 300 [stock] => 260 [folder_name] => assets/uploads/products [photo1] => photo1.jpg [photo2] => photo2.jpg [category_name] => PC [is_published] => 1 [modified_by] => ) ) ) 1 11400.00
Anyone knows how to get each value of that array?
I re-created your multidimensional array, if you want to get the deepest values, here it is:
$deepest_values = array();
foreach($arr_ as $first_level_key => $first_level_value) {
if(is_array($first_level_value)) {
foreach($first_level_value as $second_level_key => $second_level_value) {
if(is_array($second_level_value)) {
foreach($second_level_value as $third_level_key => $third_level_value) {
if(is_array($third_level_value)) {
foreach($third_level_value as $fourth_level_key => $fourth_level_value) {
$deepest_values[$fourth_level_key] = $fourth_level_value;
}
}
}
}
}
}
}
echo '<pre>';
print_r($deepest_values);
echo '</pre>';
if mesq_G3SC is the key you want to iterate over - i suggest you do the following
$obj = new stdClass();
$obj->cart_id = "mesq_G3SC";
$obj->qty = "2.00";
$obj->price = "11400";
$obj->product_id = "6";
$obj->member_id = "5";
$obj->session_id = "4dedde2a2eb12b25e940b7051a5f65a1";
$obj->date_added = "2016-09-28 10:39:06";
$obj->date_modified = "2016-09-28 10:39:06";
$obj->status = "pending";
$obj->category_id = "2";
$obj->location = "England";
$obj->square = "300";
$obj->stock = "260";
$obj->folder_name = "assets/uploads/products";
$obj->photo1 = "photo1.jpg";
$obj->photo2 = "pc.jpg";
$obj->category_name = "PC";
$obj->is_published = "1";
$obj->modified_by = NULL;
$data = array(
"mesq_G3SC"=> array(
"data"=> $obj
),
"total_item" => 1,
"total_price" => "11400.00"
);
foreach($data['mesq_G3SC'] AS $key => $obj)
{
echo $key;
print_r($obj);
}
Update: i simulated your array - and it works like a charm - there is something wrong with the keys - instead of var_dump try to print_r your data array and show us the output pls
Having the following array:
array(4) {
[0]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(37) "some data"
}
[1]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(68) "some other data"
}
[2]=>
array(2) {
[0]=>
string(3) "144"
[1]=>
string(38) "some other data"
}
[3]=>
array(2) {
[0]=>
string(3) "233"
[1]=>
string(42) "some other data"
}
}
I want to replace the values 233 and 144 (the key 0 from the inner array) by some random HEX color. The ones with the same keys (233) for example, has to have the same HEX color (FFF000 for example in the desired solution above).
This is the function I use to generate random HEX colors:
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
My desired output should be:
array(4) {
[0]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(37) "some data"
}
[1]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(68) "some other data"
}
[2]=>
array(2) {
[0]=>
string(6) "111333"
[1]=>
string(38) "some other data"
}
[3]=>
array(2) {
[0]=>
string(6) "FFF000"
[1]=>
string(42) "some other data"
}
}
How can I archieve this?
Thanks in advance.
foreach ($array as &$item) {
if (!isset($temp[$item[0]]) {
$temp[$item[0]] = randHEXcolor();
}
$item[0] = $temp[$item[0]];
}
If you want all values to be translated to the same random color, you'll have to save those colors:
$colors_translation = array();
foreach ($array as &$item) {
$color = $item[ 0 ];
$translate = $colors_translation[ $color ];
if (empty($translate)) {
$colors_translations[ $color ] = $translate = randHEXcolor();
}
$item[ 0 ] = $translate;
}
The solution using in_array and isset functions:
$keys = [];
foreach ($arr as &$v) { // $arr is your initial array
if (in_array($v[0], ['233', '144'])) {
if (!isset($keys[$v[0]])) $keys[$v[0]] = sprintf('%06X', mt_rand(0, 0xFFFFFF));
$v[0] = $keys[$v[0]];
}
}
print_r($arr);
The output:
Array
(
[0] => Array
(
[0] => 65A4BB
[1] => some data
)
[1] => Array
(
[0] => 65A4BB
[1] => some data
)
[2] => Array
(
[0] => DDB588
[1] => some data
)
[3] => Array
(
[0] => 65A4BB
[1] => some data
)
)
This code will create a color map as the array is traversed. Pre-populate $colorMap if you want pre-defined color translations.
<?php
$array = array(
0 => array(
0 => "233",
1 => "some data"
),
1 => array(
0 => "233",
1 => "some data"
),
2 => array(
0 => "144",
1 => "some data"
),
3 => array(
0 => "233",
1 => "some data"
),
);
$colorMap = array();
foreach ($array as &$inner) {
if (!array_key_exists($inner[0],$colorMap)) {
$newColor = randHEXcolor();
$colorMap[$inner[0]] = $newColor;
$inner[0] = $newColor;
} else {
$inner[0] = $colorMap[$inner[0]];
}
}
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
print_r($array);
print_r($colorMap);
Array
(
[0] => Array
(
[0] => F1519A
[1] => some data
)
[1] => Array
(
[0] => F1519A
[1] => some data
)
[2] => Array
(
[0] => 2F7D00
[1] => some data
)
[3] => Array
(
[0] => F1519A
[1] => some data
)
)
Array
(
[233] => F1519A
[144] => 2F7D00
)
Try:
<?php
$array = array(
0 => array(
0 => "233",
1 => "some data"
),
1 => array(
0 => "233",
1 => "some data"
),
2 => array(
0 => "144",
1 => "some data"
),
3 => array(
0 => "233",
1 => "some data"
),
);
function randHEXcolor() {
return sprintf('%06X', mt_rand(0, 0xFFFFFF));
}
$firstHex = randHEXcolor();
$secondHex = randHEXcolor();
foreach($array as $arrayIndex => &$arrayValue){
if($arrayValue[0] == "144"){
$arrayValue[0] = $firstHex;
}
if($arrayValue[0] == "233"){
$arrayValue[0] = $secondHex;
}
}
output:
array(4) {
[0]=>
array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
[1]=>
array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
[2]=>
array(2) {
[0]=>
string(6) "22AF8B"
[1]=>
string(9) "some data"
}
[3]=>
&array(2) {
[0]=>
string(6) "AB8248"
[1]=>
string(9) "some data"
}
}
I need to create array using loop, How to do it
Here is my array
$data = array(
value1 => 1,
value2 => 32,
value3 => 25
);
for (i=o,i<2,i++) {
}
If i value is 2 my array should be like
$arrays = (array(data,data));
If i value is 3my array should be like
$arrays=(array(data,data,data));
Help me to create array like this
If i i value is 2 means ouput should be like
result =(array(value1 => 1,value2 => 32,value3 => 25),(value1 => 1,value2 => 32,value3 => 25));
);
Do you mean this:
<?php
$data = array(
'value1' => 1,
'value2' => 32,
'value3' => 25
);
$finalArr = [];
for ($i=0;$i<2;$i++) {
$finalArr[] = $data;
}
print_r($finalArr);
Output:
Array
(
[0] => Array
(
[value1] => 1
[value2] => 32
[value3] => 25
)
[1] => Array
(
[value1] => 1
[value2] => 32
[value3] => 25
)
)
Your Eval sample
below code give output as
<?php
$data = array(
'value1' => 1,
'value2' => 32,
'value3' => 25
);
$finalArr = [];
for ($i=1;$i<=3;$i++) {
$finalArr['value'.$i] = $data['value'.$i];
}
print_r($finalArr);
?>
Array
(
[value1] => 1
[value2] => 32
[value3] => 25
)
if you dont need value1 in side array ie [value1]=>1
then remove 'value'.$i from $finalArr['value'.$i]
if you can change the value of $ according to the number of your array
So from what I understand, you want an array with a number of values...
$size = 5; # Size of the array
$array = array(); # The empty array to begin with
$value = array('value1' => '1', 'value2' => '32', 'value3' => '25');
// Create our array with a for loop
for($i=1; $i<=$size; $i++)
array_push($array, $value);
The var_dump of the array would be:
array(5) {
[0]=>
array(3) {
["value1"]=>
string(1) "1"
["value2"]=>
string(2) "32"
["value3"]=>
string(2) "25"
}
[1]=>
array(3) {
["value1"]=>
string(1) "1"
["value2"]=>
string(2) "32"
["value3"]=>
string(2) "25"
}
[2]=>
array(3) {
["value1"]=>
string(1) "1"
["value2"]=>
string(2) "32"
["value3"]=>
string(2) "25"
}
[3]=>
array(3) {
["value1"]=>
string(1) "1"
["value2"]=>
string(2) "32"
["value3"]=>
string(2) "25"
}
[4]=>
array(3) {
["value1"]=>
string(1) "1"
["value2"]=>
string(2) "32"
["value3"]=>
string(2) "25"
}
}
I have an array
dump($data);
*************************************
array(10) {
["12-male"] => string(1) "2"
["11-male"] => string(1) "2"
["10-female"] => string(1) "2"
["16-female"] => string(1) "2"
["9-male"] => string(1) "2"
["17-male"] => string(1) "4"
["14-male"] => string(1) "4"
["15-female"] => string(1) "4"
["13-female"] => string(1) "5"
["18-female"] => string(1) "6"
}
******************************************
I am DYNAMICALLY getting like sub arrays out of the array above
$rooms = array();
foreach ($data as $key => $value) {
$rooms['room'.$value][] = $key;
$rooms['room'.$value]['count'] = sizeof($rooms['room'.$value]);
}
dump($rooms);
******************************************
I get this result
Dump => array(4) {
["room2"] => array(6) { //array size=6
[0] => string(7) "12-male"
["count"] => int(6) //count of array size=6
[1] => string(7) "11-male"
[2] => string(9) "10-female"
[3] => string(9) "16-female"
[4] => string(6) "9-male"
}
["room4"] => array(4) { //array size=4
[0] => string(7) "17-male"
["count"] => int(4) //count of array size=4
[1] => string(7) "14-male"
[2] => string(9) "15-female"
}
["room5"] => array(2) { //array size=2
[0] => string(9) "13-female"
["count"] => int(1) //count of array size=1 (the problem here)
}
["room6"] => array(2) { //array size=2
[0] => string(9) "18-female"
["count"] => int(1) //count of array size=1 (the problem here)
}
}
My issue is that, the count is returned correctly after first 2 iterations, after that the count is always showing 1, no matter the size of array.
I tried count() as well but the result is the same.
You could do like below:
$rooms = array();
foreach ($data as $key => $value) {
if (!isset($rooms['room'.$value])) {
$rooms['room'.$value] = array('count' => 0);
}
$rooms['room'.$value][] = $key;
$rooms['room'.$value]['count']++;
}
But you don't need to add the count into your array.
The reason the count is doing that is that from room2 and room4 you are inserting 'count' on the first iteration, then on subsequent iterations 'count' is included in the sizeof() request. For room 5 and room6 as they are iterated only once sizeof() is only called once, before 'count' is inserted into the array, so it's not the index of 'count' not included in the result of sizeof for those items.