I get from my DB data in format like this:
array(12) {
[0]=>
array(4) {
["id"]=>
string(1) "1"
["count"]=>
string(5) "78984"
["month"]=>
string(1) "6"
["hours"]=>
string(10) "10580.0833"
}
[1]=>
array(4) {
["id"]=>
string(1) "2"
["count"]=>
string(5) "64174"
["month"]=>
string(1) "6"
["hours"]=>
string(9) "6866.8333"
}
[2]=>
array(4) {
["id"]=>
string(1) "3"
["count"]=>
string(5) "31032"
["month"]=>
string(1) "6"
["hours"]=>
string(9) "3700.9167"
}
[3]=>
array(4) {
["id"]=>
string(1) "1"
["count"]=>
string(5) "91114"
["month"]=>
string(1) "7"
["hours"]=>
string(10) "11859.6000"
}
...
Each of array inside has a key: "id". It mostly look values from 1 to 3. I would like to create a new array based on this "ids" that would look like this:
array("number of unique ids") {
[0]=> "for id = 0"
array(12) {
int() count/hours
int() count/hours
int() count/hours
...
}
[1]=> "for id = 1 and so on..."
array(12){
....
}
I`ve been trying to do it like this:
$data1 = [];
$data2 = [];
$data3 = [];
foreach($data as $key => $record){
if($record['id'] == 1){
array_push($data1, $record['count']/$record['hours']);
}else if($data['id_zmiana'] == 2){
array_push($data2, $record['count']/$record['hours']);
}else{
array_push($data3, $record['count']/$record['hours']);
}
}
$raport = array_merge($data1, $data2, $data3);
And that would work, but it doesn`t look good in my opinion, beacuse what if the id change to some big number.
Yeah, having separate arrays for each ID is not a good idea. How about:
$raport = [];
foreach ($data as $record) {
$raport[$record['id']][] = $record['count'] / $record['hours']);
}
Simple, and straight to the point.
Haven't tested it. Next time try to use var_export() to export the data you put in your question. That way we can actually use it, without having to rewrite it completely.
Im trying many days now to select data from this db
[1]: https://i.stack.imgur.com/QA34L.jpg
I want to print for example all comments
echo $comment['username']." | "; \ Alex | Alex
My php code so far:
[![$m= new MongoDB\Client ("mongodb://127.0.0.1/");
$db = $m->stores;
$collection = $db->storeinfo;][1]][1]
$storez = $collection->find(array("Products.pTHUMBNAIL" => $pThumb));
$o=1;
$afm=array();
foreach ($storez as $stor) {
$afm[$o] = $stor['AFM'];
$record = $collection->findOne(array(
"AFM" => $afm[$o],"Products.pTHUMBNAIL" => $pThumb));
foreach ($record['Products'] as $pro){
if($pThumb == $pro['pTHUMBNAIL']){
echo $temp = $pro['pID']." ";
foreach($pro as $pro1['pCOMMENTS']) {
foreach($pro1 as $com['Comment']) {
var_dump($com['Comment']);
/*
foreach($com as $comment) {
echo $comment['username'];
}
*/
}
}
}
}
$o += 1;
}
It seems that i just cannot find the correct foreach to loop through my Comment array
var_dump output:
099360111/1 object(MongoDB\BSON\ObjectId)#55 (1) { ["oid"]=> string(24) "6003403a695900000c002649" } string(11) "099360111/1" string(9) "Old Skool" string(2) "75" string(4) "Vans" string(25) "Leather and textile upper" string(2) "44" string(18) "Men/Shoes/Trainers" string(52) "http://127.0.0.1/pricedoc/assets/img/products/p1.jpg" string(1) "7" object(MongoDB\Model\BSONArray)#65 (1) { ["storage":"ArrayObject":private]=> array(1) { [0]=> object(MongoDB\Model\BSONDocument)#10 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#73 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#45 (1) { ["milliseconds"]=> string(13) "1611028053000" } ["text"]=> string(21) "1st comment from user" ["rating"]=> string(1) "4" } } } } } } 099360666/1 object(MongoDB\BSON\ObjectId)#44 (1) { ["oid"]=> string(24) "6006563a3f1c0000c80034a8" } string(11) "099360666/1" string(12) "old school 2" string(2) "50" string(4) "Vans" string(11) "black/white" string(8) "42,43,43" string(18) "Men/Shoes/Trainers" string(52) "http://127.0.0.1/pricedoc/assets/img/products/p1.jpg" string(1) "6" object(MongoDB\Model\BSONArray)#79 (1) { ["storage":"ArrayObject":private]=> array(2) { [0]=> object(MongoDB\Model\BSONDocument)#7 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#39 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#68 (1) { ["milliseconds"]=> string(13) "1611028089000" } ["text"]=> string(21) "1st comment from user" ["rating"]=> string(1) "4" } } } } [1]=> object(MongoDB\Model\BSONDocument)#78 (1) { ["storage":"ArrayObject":private]=> array(1) { ["Comment"]=> object(MongoDB\Model\BSONDocument)#77 (1) { ["storage":"ArrayObject":private]=> array(4) { ["username"]=> string(4) "Alex" ["date"]=> object(MongoDB\BSON\UTCDateTime)#76 (1) { ["milliseconds"]=> string(13) "1611030745000" } ["text"]=> string(8) "good!!!!" ["rating"]=> string(1) "5" } } } } } }
What about this?
After echo $temp = $pro['pID']." ";
foreach($pro['pCOMMENTS'] as $comments) {
foreach ($comments['Comment'] as $comment) {
echo $comment['text'];
}
}
You used foreach() bad: the index should be added to the first parameter, not in the 'as' part.
Also it helps if you use more clear variable names. That doesn't take too much, but makes it more readable and easy to debug.
After echo $temp = $pro['pID']." ";
foreach($pro['pCOMMENTS'] as $comments) {
foreach($comments as $comment) {
echo $comment['text'];
}
}
how can i fetch data in an arrays with gives null value if null
here is my data I var_dump($showStatus); I want to print out . $showStatus[0]['title']
string(0) ""
array(2) {
[0]=>
array(7) {
["id"]=>
string(1) "1"
["container_id"]=>
string(1) "3"
["title"]=>
string(51) "waitting"
}
[1]=>
array(7) {
["id"]=>
string(1) "2"
["container_id"]=>
string(1) "3"
["title"]=>
string(72) "getting"
}
}
array(1) {
[0]=>
array(7) {
["id"]=>
string(1) "4"
["container_id"]=>
string(1) "7"
["title"]=>
string(51) "getting"
}
}
The reason that I've string because in my models I want to print "" or NULL when it don't have data here is my models
public function showStatus($id){
$sql = 'SELECT * FROM status WHERE container_id = '.$id;
if($this->query_rows( $sql )) {
return $this->query_rows($sql);
} else {
return "";
}
}
I try to use
foreach ($getListData as $k) {
}
but it said Warning: Invalid argument supplied for foreach()
Try this:
if(!empty($getListData) )
{
foreach ($getListData as $k) {
print_r($k);
}
}
else {
echo "NULL";
}
Prior to this foreach loop $_POST['encounterName'] contains a predefined name. Inside the loop its value changes to ".json" so the first string in file_put_contents reads ".json.json". As well $_POST['basicTrainingSectionSlide'], $_POST['basicTrainingContentHeader'], and
$_POST['basicTrainingContentText'] lose their values. After the foreach loop, all values go back to normal. What is happening here?
$i = 0;
$j = 0;
foreach($_POST['Sections'] as $order){
if(strcmp($order, "Section") == 0){
file_put_contents($_POST['encounterName'].".json", "\t\t[\n", FILE_APPEND);
file_put_contents($_POST['encounterName'].".json", "\t\t\t\"Section\",\n", FILE_APPEND);
file_put_contents($_POST['encounterName'].".json", "\t\t\t\"".$_POST['basicTrainingSectionSlide'][$i]."\"\n", FILE_APPEND);
file_put_contents($_POST['encounterName'].".json", "\t\t],\n", FILE_APPEND);
$i++;
}
else if(strcmp($order, "Text") == 0){
file_put_contents($_POST['encounterName'].".json", "\t\t[\n", FILE_APPEND);
file_put_contents($_POST['encounterName'].".json", "\t\t\t\"Text\",\n", FILE_APPEND);
file_put_contents($_POST['encounterName'].".json", "\t\t\t\"".$_POST['basicTrainingContentHeader'][$j]."\"\n", FILE_APPEND);
file_put_contents($_POST['encounterName'].".json", "\t\t\t\"".$_POST['basicTrainingContentText'][$j]."\"\n", FILE_APPEND);
file_put_contents($_POST['encounterName'].".json", "\t\t],\n", FILE_APPEND);
$j++;
}
}
This is what the $_post array contains:
array(11) { ["encounterName"]=> string(8) "Violence" ["encounterHint"]=> string(0) "" ["basicTrainingSectionSlide"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "6" } ["basicTrainingContentHeader"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "4" } ["basicTrainingContentText"]=> array(2) { [0]=> string(1) "3" [1]=> string(1) "5" } ["contextText_1"]=> string(0) "" ["contextText_2"]=> string(0) "" ["contextText_3"]=> string(0) "" ["contextText_4"]=> string(0) "" ["contextText_5"]=> string(0) "" ["submit_form"]=> string(6) "Submit" } array(11) { ["encounterName"]=> string(8) "Violence" ["encounterHint"]=> string(0) "" ["basicTrainingSectionSlide"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "6" } ["basicTrainingContentHeader"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "4" } ["basicTrainingContentText"]=> array(2) { [0]=> string(1) "3" [1]=> string(1) "5" } ["submit_form"]=> string(6) "Submit" }
Some added information:
-The form that is filled out sends its post data to a separate php file for processing.
-The $_POST['Sections'] is sent via this function:
function returnValues() {
$.ajax({
type: "POST",
url: "final.php",
data:{ Sections: $sectionOrder },
success: function(data){
console.log(data);
}
})
}
from a .js file onsubmit of the form tag.
Assuming that $_POST['Sections'] contains something like:
array(11) {
["encounterName"]=> string(8) "Violence"
["encounterHint"]=> string(0) ""
["basicTrainingSectionSlide"]=> array(2) {
[0]=> string(1) "1"
[1]=> string(1) "6" }
["basicTrainingContentHeader"]=> array(2) {
[0]=> string(1) "2"
[1]=> string(1) "4" }
["basicTrainingContentText"]=> array(2) {
[0]=> string(1) "3"
[1]=> string(1) "5" }
["contextText_1"]=> string(0) ""
["contextText_2"]=> string(0) ""
["contextText_3"]=> string(0) ""
["contextText_4"]=> string(0) ""
["contextText_5"]=> string(0) ""
["submit_form"]=> string(6) "Submit"
}
array(11) {
["encounterName"]=> string(8) "Violence"
["encounterHint"]=> string(0) ""
["basicTrainingSectionSlide"]=> array(2) {
[0]=> string(1) "1"
[1]=> string(1) "6" }
["basicTrainingContentHeader"]=> array(2) {
[0]=> string(1) "2"
[1]=> string(1) "4" }
["basicTrainingContentText"]=> array(2) {
[0]=> string(1) "3"
[1]=> string(1) "5" }
["submit_form"]=> string(6) "Submit"
}
I would do something like this:
$i = 0;
$j = 0;
foreach($_POST['Sections'] as $order){
$fn = $order['encounterName'] . ".json";
$appText = "";
if(strcmp($order, "Section") == 0){
$appText .= "\t\t[\n";
$appText .= "\t\t\t\"Section\",\n";
$appText .="\t\t\t\"" . $order['basicTrainingSectionSlide'][$i] . "\"\n";
$appText .= "\t\t],\n";
file_put_contents($fn, $appText, FILE_APPEND);
$i++;
} elseif(strcmp($order, "Text") == 0){
$appText .= "\t\t[\n";
$appText .= "\t\t\t\"Text\",\n";
$appText .= "\t\t\t\"" . $order['basicTrainingContentHeader'][$j] . "\"\n";
$appText .= "\t\t\t\"" . $order['basicTrainingContentText'][$j] . "\"\n";
$appText .= "\t\t],\n";
file_put_contents($fn, $appText, FILE_APPEND);
$j++;
}
}
Since you are in a foreach loop and iterating over the array $_POST['Sections'], to call the elements of that array, you need to use $order. That is how you defined the loop. You kept calling $_POST and those indexes shouldn't exist.
If you were using $_POST, the first occurrence of encounterName would be located at $_POST['Sections'][0]['encounterName'] and would contain the string Violence. So, in the loop, $fn = $order['encounterName'].".json"; should be assigned Violence.json as a String.
I am unsure what the if statement is looking for. Since $order is an Array, not a String, strcmp() should return NULL. See notes here. Since both if statements would fail, your file would not be appended in either case. Please comment if you want to clarify that.
I tested the following on http://phpfiddle.org/lite :
<?php
$order = array();
if(strcmp($order, "Section")){
echo "Success";
} else {
echo "Fail";
}
?>
I got the following results:
E_WARNING : type 2 -- strcmp() expects parameter 1 to be string, array given -- at line 5
Fail
This code is untested.
I am trying to upload csv file data using PHP,
CSV File Data (Semicolon separated)
Name;Nom;Tel;Email;Ville;ID
x;DURAND;11;aprasquier+testvia#odm-tech.com;"Paris";1
PHP Code:
$handle = fopen('test.csv', "r");
while (($data = fgetcsv($handle,0, ";")) !== FALSE)
echo var_dump($data);
output:
array(1) {
[0]=> string(53) "x;DURAND;11;aprasquier+testvia#odm-tech.com;"Paris";1"
}
Expected output is ,
array(6) {
[0]=> string(1) "x"
[1]=> string(6) "DURAND"
[2]=> string(2) "11"
[3]=> string(31) "aprasquier+testvia#odm-tech.com"
[4]=> string(5) "Paris"
[5]=> string(1) "1"
}
Please anyone advise on this asap.