return specific values from array using php - php

I have an array that is produced from a mysql query:
Array ( [0] => AA [alleles] => AA [1] => 6 [total] => 6 [2] => 25.00 [percentage] => 25.00 )
Array ( [0] => AG [alleles] => AG [1] => 11 [total] => 11 [2] => 45.83 [percentage] => 45.83 )
Array ( [0] => GG [alleles] => GG [1] => 7 [total] => 7 [2] => 29.17 [percentage] => 29.17 )
How do I parse this data with php to show:
AA 25%
AG 45.83%
GG 29.17%

Looks like you want to return two elements per array : elements 0 and 2
Depending on what language you are using ? it would be something like :
answer_string = Array_1[0] + " " + Array_1[2] + "%"
PS. in the future always tag your question with appropriate language to get more helpful answers (it wouldn't hurt to edit your question as such)

Related

Get value multidimensional array and combine product group

I have created a multidimensional array using a csv file.
I get the following array:
Array
(
[0] => Array
(
[0] => Product
[1] => Price
[2] => Quantity
[3] => Date
)
[1] => Array
(
[0] => Teddybear
[1] => 3.789,33
[2] => 2
[3] => 2018-08-14
)
[2] => Array
(
[0] => Teddybear
[1] => 2.702,17
[2] => 1
[3] => 2018-02-17
)
[3] => Array
(
[0] => Table
[1] => 2.932,29
[2] => 5
[3] => 2018-10-09
)
[4] => Array
(
[0] => Lamp
[1] => 671,70
[2] => 3
[3] => 2018-12-11
)
[5] => Array
(
[0] => Teddybear
[1] => 481,21
[2] => 3
[3] => 2018-03-24
)
[6] => Array
(
[0] => Table
[1] => 456,52
[2] => 3
[3] => 2018-05-14
)
[7] => Array
(
[0] => Chair
[1] => 2.960,20
[2] => 2
[3] => 2018-06-20
)
[8] =>
)
I need to create a json file, but before that, I need to count al products and turnover per month
I'm a novice / junior PHP developer and I'm more specialized in WordPress, so forgive me for my bad code and/or bad practices. I'm here to learn.
I really don't know how to select for example all the Teddybears. I know I could do $csv[1][1]; to select turnover for the first Tedybear.
ok here there should be everything you need, I did 3 functions that you can chain together to get what you need, like in the example
$data=[
["Product","Price","Quantity","Date"], // <-- if you want to remove the header remove all the lines with this symbol *
["Teddybear",3789.33,2,"2018-08-14"],
["Teddybear",2702.17,1,"2018-02-17"],
["Table",2932.29,5,"2018-10-09"],
["Lamp",671.70,3,"2018-12-11"],
["Teddybear",481.21,3,"2018-03-24"],
["Table",45.52,3,"2018-05-14"],
["Chair",2960.20,2,"2018-06-20"]
];
function WhereFromColum($data,$colum,$equal){
$ret=[];
array_push($ret,$data[0]); // *
$colum = is_numeric($colum)?$colum:array_search($colum,$data[0]);
for($i=1/* 0 if you remove the header to data*/;$i!=count($data);$i++)
if ($data[$i][$colum]==$equal)
array_push($ret,$data[$i]);
return $ret;
}
function CountFromColum($data,$colum){
$ret=0;
$colum = is_numeric($colum)?$colum:array_search($colum,$data[0]);
for($i=1/* 0 if you remove the header to data*/;$i!=count($data);$i++)
$ret+=$data[$i][$colum];
return $ret;
}
function WhereFromColumData($data,$colum,$min,$max){
$contractDateBegin = new DateTime($min);
$contractDateEnd = new DateTime($max);
$ret=[];
array_push($ret,$data[0]); // *
$colum = is_numeric($colum)?$colum:array_search($colum,$data[0]);
for($i=1/* 0 if you remove the header to data*/;$i!=count($data);$i++){
$paymentDate = new DateTime($data[$i][$colum]);
if ($paymentDate->getTimestamp() > $contractDateBegin->getTimestamp() &&
$paymentDate->getTimestamp() < $contractDateEnd->getTimestamp())
array_push($ret,$data[$i]);
}
return $ret;
}
var_dump(WhereFromColum($data,"Product","Teddybear")); // get all Teddybear's Product
var_dump(CountFromColum($data,"Quantity")); // get all Quantity
var_dump(CountFromColum(WhereFromColum($data,"Product","Teddybear"),"Quantity")); // get all Quantity of Teddybear's Product
var_dump(WhereFromColumData($data,"Date","2018-08-01","2018-12-01")); // get all between 2018-08-01 and 2018-12-01
var_dump(WhereFromColumData(WhereFromColum($data,"Product","Teddybear"),"Date","2018-08-01","2018-12-01")); // get all Teddybear's Product between 2018-08-01 and 2018-12-01
usually I don't usually write code but explain how to do it but in your case it was too complex so please kindly read the three functions carefully and try to understand how they work, ps if you want to improve your skils in php I suggest you play a with these functions and modify them to fit what you need best. when you understand how they work you can also create another one with the same logic of nesting that allows you to make much more advanced filters.

How to insert 100 records in mysql procedure and loop in MySQL procedure not in PHP?

I have a record of 100 or 200 questions when user submit the test all 100 or 200 questions from caches need to save to MySQL. Right now I have to loop in PHP and every time a single query has executed, any idea how I send an array in MySQL procedure and the loop of 100 or 200 should be done in MySQL procedure. I can convert the array to JSON as JSON is supported in MySQL.
array which needs to be insert looks like this
Array
(
[0] => Array
(
[0] => 19287
[1] => 1
[2] => 1
[3] => 101
[4] => 224
[5] => 0
)
[1] => Array
(
[0] => 19285
[1] => 0
[2] => 1
[3] => 101
[4] => 2
[5] => 0
)
[2] => Array
(
[0] => 19289
[1] => 1
[2] => 1
[3] => 72
[4] => 23
[5] => 0
)
[3] => Array
(
[0] => 19290
[1] =>
[2] => 1
[3] => 106
[4] => 3
[5] => 0
)
[4] => Array
(
[0] => 19291
[1] => 0
[2] => 1
[3] => 230
[4] => 76
[5] => 0
)
)
You can build a multi-row INSERT in your PHP code. Assuming your data is in $arr:
$sql = "INSERT INTO yourtable (...column list...) VALUES ";
$vsets = array();
foreach ($arr as $values) {
$vsets[] = str_replace(array(',,', ',,'), ',NULL,', "(" . implode(',', $values) . ")");
}
$sql .= implode(',', $vsets);
echo $sql;
Output:
INSERT INTO yourtable (...column list...)
VALUES (19287,1,1,101,224,0),(19285,0,1,101,2,0),
(19289,1,1,72,23,0),(19290,NULL,1,106,3,0),
(19291,0,1,230,76,0)
Make sure you filter, validate or cast your data, to prevent any non-integers (or any SQL) from being injected into your queries.

Accessing values from inner array [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
Array ( [err] => [num] => 1 [result] => Array ( [0] => Array ( [0] => 85 [proj_id] => 85 [1] => Automatic Mobile Alert System For Lethal Gas Detection [proj_item_title] => Automatic Mobile Alert System For Lethal Gas Detection [2] => GSM PROJECTS [proj_category] => GSM PROJECTS [3] => 5000 [actual_price] => 5000 [4] => 4000 [discount_price] => 4000 [5] => 5 [rating] => 5 [6] => automatic-mobile-alert-system-for-lethal-gas-detection [friendly_url] => automatic-mobile-alert-system-for-lethal-gas-detection [7] => gsnrtmccfoqqkgb8qs2ni5hud36c032j [session_id] => gsnrtmccfoqqkgb8qs2ni5hud36c032j [8] => 85 [9] => 2017-01-20 [date_added] => 2017-01-20 [10] => 1 [qty] => 1 ) ) )
I need to access elements from the array "result". How can I access them. Please help me.
echo $array['result'][0][0]
Will return 85
$result = $array['result'][0]
Will give you a nice way to save the Result array into another Array.
So you can do
$result[0] //Returns 85 again
Your $data['result'] is an array so you can simply loop over the elements
foreach($data['result'] as $result) {
var_dump($result);
echo $result[0];
echo $result['proj_item_title'];
echo $result['proj_id']; //etc
}

how to get particular key value from an object array in php

I have an array like below :
Array ( [3] =>
stdClass Object (
[Course_ID] => php01
[Course_Type] => E
[Course_Name] => PHP
[Service] => 3L
[Valid_Start_Date] => 2015-11-05
[Valid_End_Date] => 2016-01-31
[Duration] => 3
[Re_cert_Years] => 0
[LMS_Course_ID] => 123
[id] => 3
)
[21] =>
stdClass Object (
[Course_ID] => php01
[Course_Type] => E
[Course_Name] => PHP
[Service] => 3L
[Valid_Start_Date] => 2015-11-05
[Valid_End_Date] => 2016-01-31
[Duration] => 3
[Re_cert_Years] => 0
[LMS_Course_ID] => 123
[id] => 21
)
)
i want to get the value of Course_ID.
How to get the value from this array ?
For accessing Object key in PHP use -> operator Have you tried like following:
foreach($ret as $index=>$obj){
echo $obj->Course_ID;
}
It will print 2 courseID because your array of object contains 2 object. if you want to print any particular index then use this:
echo $ret[3]->Course_ID;
echo $ret[21]->Course_ID; //where 3 or 21 is index of array

Sorting Multidimensional Array by Specific Key

EDIT: For anyone who might come across this post with a similar problem, It was solved by taking konforce's supplied answer and tweaking around a bit with the custom sorting function:
function cmp($a, $b) {
if ($a[5] == $b[5]) {
return ($a[3] < $b[3]) ? -1 :1;
}
return ($a[5] > $b[5]) ? -1 : 1;
}
Notice $a[5] == $b[5] does not return zero. It was changed to check who has the most losses and then sort it in ASC order. I'm sure you can even keep going and add another if-statement in there in-case they have the same losses.
Lastly, all you do is usort($ARRAY, "cmp"); and finito!!!
Original Post
My apologies for coming up with yet another MD Array sorting question but I'm just not getting it. I've searched aplenty
for a solution and although many sites have provided what seemed like a logical answer I still have not been able to figure it out.
My problem is since I'm still learning its been rather difficult for me to grasp the concept of using usort with a custom comparing
function. Atleast, thats what I have seen the most when others have tried to sort MD Arrays.
I'm working on a small project to sharpen up on my php skills. Its a very basic tournament standings script that holds a team's information within an array. I would like to sort the array by most points($array[X][X][5]).
So the array looks something like this:
Array (
[0] => Array (
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
)
[1] => Array (
[0] => Array (
[0] => strenx
[1] => 9
[2] => 5
[3] => 1
[4] => 3
[5] => 18
)
)
[2] => Array (
[0] => Array (
[0] => rapha
[1] => 10
[2] => 8
[3] => 1
[4] => 1
[5] => 25
)
) [3] => Array (
[0] => Array (
[0] => ronald reagan
[1] => 5
[2] => 4
[3] => 0
[4] => 1
[5] => 13
)
)
)
I would like to sort it by most points(cell #5), so it would look like this after sorting:
Array (
[0] => Array (
[0] => Array (
[0] => rapha
[1] => 10
[2] => 8
[3] => 1
[4] => 1
[5] => 25
)
)
[1] => Array (
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
)
[2] => Array (
[0] => Array (
[0] => strenx
[1] => 9
[2] => 5
[3] => 1
[4] => 3
[5] => 18
)
)
[3] => Array (
[0] => Array (
[0] => ronald reagan
[1] => 5
[2] => 4
[3] => 0
[4] => 1
[5] => 13
)
)
)
The player with 25 points would be at the top, followed by 18, 18, and lastly 13. Sorry for my earlier post, was having difficulty wording my question correctly. Thanks in advanced!
I think you want something like this:
usort($standings, function($a, $b) { return $b[0][5] - $a[0][5]; });
Or prior to PHP 5.3:
function cmp($a, $b) { return $b[0][5] - $a[0][5]; }
usort($standings, 'cmp');
When using usort, the $a and $b parameters will be one "layer" into the supplied array. So in your case, an example of $a or $b will be:
[0] => Array (
[0] => cooller
[1] => 6
[2] => 6
[3] => 0
[4] => 0
[5] => 18
)
I'm not sure why you have an extra containing array there, but as you can see, you want to sort based on the [0][5] position.
usort($standings[0][0][5], 'cmp') won't work because the first parameter isn't an array to sort, it's just a single number, the points.

Categories