JotForm parse complex array response - php

I am using jotform php api to get all my submitions back.The response is written bellow. I cannot figure a way to parse all the data. How can I parse this complex array?
array(3) {
[0]=>
array(9) {
["id"]=>
string(18) "331530421121325001"
["form_id"]=>
string(14) "53263737388365"
["ip"]=>
string(13) "32.33.33.33"
["created_at"]=>
string(19) "2016-02-17 10:00:07"
["status"]=>
string(6) "ACTIVE"
["new"]=>
string(1) "0"
["flag"]=>
string(1) "0"
["updated_at"]=>
string(19) "2016-02-17 10:00:17"
["answers"]=>
array(1) {
[1]=>
array(4) {
["text"]=>
string(9) "Full Name"
["type"]=>
string(16) "control_fullname"
["answer"]=>
array(2) {
["first"]=>
string(15) "nnnasdadsaasdad"
["last"]=>
string(20) "dsadsadsadsadadassda"
}
["prettyFormat"]=>
string(36) "nnnasdadsaasdad dsadsadsadsadadassda"
}
}
}
}
here is my code but is not that successfull so far.
$responses=$jotformAPI->getSubmissions();
foreach ($responses as $response) {
//print_r($response);
foreach($response as $element=>$value)
{
echo $value;
if(is_array($value))
{
foreach($value as $val)
{
echo $val;
}
}
}
}
please help.

$p[0]= array(
"id"=>"331530421121325001",
"form_id"=> "53263737388365",
"ip"=>"32.33.33.33",
"created_at"=>"2016-02-17 10:00:07",
"status"=>"ACTIVE",
"new"=>"0",
"flag"=>"0",
"updated_at"=> "2016-02-17 10:00:17",
"answers"=> array(
1=>array("text"=> "Full Name",
"type"=>"control_fullname",
"answer"=>array(
"first"=>"nnnasdadsaasdad",
"last"=> "dsadsadsadsadadassda"
),
"prettyFormat"=> "nnnasdadsaasdad dsadsadsadsadadassda",
)));
I solved using a recursive function
ra($p);
function ra($p){
foreach($p as $key=>$value) :
$p=$value;
if(is_array($p)) {
ra($p);
}else{
echo "<br>",$value;
}
endforeach;
}

Related

Search with PHP inside MongoDB

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 to get the index of every array on multidimensional array with unknown depth in php

array(2) { [0]=> array(2) { ["name"]=> string(16) "Daerah Pertanian" ["sub"]=> array(6) { [0]=> array(2) { ["name"]=> string(5) "Sawah" ["value"]=> string(3) "145" } [1]=> array(2) { ["name"]=> string(18) "Sawah Pasang Surut" ["value"]=> string(3) "455" } [2]=> array(2) { ["name"]=> string(6) "Ladang" ["value"]=> string(3) "678" } [3]=> array(2) { ["name"]=> string(10) "Perkebunan" ["value"]=> string(3) "688" } [4]=> array(2) { ["name"]=> string(19) "Perkebunan Campuran" ["value"]=> string(3) "966" } [5]=> array(2) { ["name"]=> string(16) "Tanaman Campuran" ["value"]=> string(3) "565" } } } [1]=> array(2) { ["name"]=> string(22) "Daerah Bukan Pertanian" ["sub"]=> array(2) { [0]=> array(2) { ["name"]=> string(18) "Hutan Lahan Kering" ["sub"]=> array(2) { [0]=> array(2) { ["name"]=> string(25) "Hutan Lahan Kering Primer" ["value"]=> string(3) "566" } [1]=> array(2) { ["name"]=> string(27) "Hutan Lahan Kering Sekunder" ["value"]=> string(3) "255" } } } [1]=> array(2) { ["name"]=> string(17) "Hutan Lahan Basah" ["sub"]=> array(2) { [0]=> array(1) { ["name"]=> string(24) "Hutan Lahan Basah Primer" } [1]=> array(1) { ["name"]=> string(26) "Hutan Lahan Basah Sekunder" } } } } } }
I have an array like I mention above, so I want to print out every "name" key including the index (number) of it's array parent,
for example when I print out "Tanaman Campuran" so all index parent is (0)(5) and when I print "Hutan Lahan Basah Sekunder" the index parent is (1)(1)(1)
how can I achieve it?
here is some recursive function that I've tried
$GLOBALS['all'] = '';
function printout($arr){
foreach ($arr as $ia=>$a){
if(is_array($a)){
foreach ($a as $ib=>$b){
if(is_array($b)){
printout($b);
}
else{
if ($ib == 'name') {
$GLOBALS['all'] .= $ia;
echo '<tr>';
echo '<td>' . $b . ' (' . $ia . ')</td>';
echo '</tr>';
$GLOBALS['all'] = '';
}
}
}
}
}
}
*sorry for my bad explanation, I hope you guys can understand it
You could use the following function:
function search(array $array, $name)
{
foreach ($array as $key => $entry) {
if ($entry['name'] === $name) {
return [$key];
}
if (isset($entry['sub']) && $found_keys = search($entry['sub'], $name)) {
return array_merge([$key], $found_keys);
}
}
return null;
}
It returns:
if the value was directly found, an array of one containing the associated index,
if it wasn't but was found in any descendant item, an array merging its index with the indices of said descendant,
null if it wasn't found in that part of the tree.
Note: if a given name is present several times, it will only find the first occurrence.
Demo: https://3v4l.org/1hGr1

Unshift the array on condition

I have written below lines of code
public function shiftarray($cursor, $arg)
{
$keyarguments = array("first_name","roll_no");
$arrayStudents =array();
foreach ($cursor as $k => $row)
{
foreach ($keyarguments as $key)
{
if (strcasecmp($row[$key], $arg) == 0)
{
array_unshift($arrayStudents, $row);
}
else
{
}
}
}
return $arrayStudents;
}
I have array of students in $cursor like
{ [0]=> array(50) { ["_id"]=> object(MongoId)#23 (1) { ["$id"]=> string(24) "58131c7799fbad4c1d000202" } ["student_id"]=> float(2) ["registration_temp_perm_no"]=> string(1) "1" ["roll_no"]=> float(1) ["admission_date"]=> string(10) "01/07/2016" ["first_name"]=> string(7) "Neil" ["middle_name"]=> string(4) "David" ["last_name"]=> string(6) "Stephan" ["dob"]=> string(10) "12/03/1981" ["gender"]=> string(6) "Female" ["blood_group"]=> string(2) "A+" ["birth_place"]=> string(11) "Sadar Bazar" ["nationality"]=> string(6) "Indian" ["language"]=> string(7) "English" ["religion"]=> string(8) "Agnostic" ["address_line1"]=> string(20) "4148 Hazelcrest Hill" ["address_line2"]=> string(20) "22883 Memorial Place" ["city"]=> string(11) "Sadar Bazar" ["state"]=> string(13) "Uttar Pradesh" ["pincode"]=> string(6) "190010" ["country"]=> string(5) "India" ["phone1"]=> string(10) "9039180419" ["phone2"]=> string(10) "7681559402" ["email"]=> string(24) "educianstudent#gmail.com" ["is_sms_enabled"]=> string(3) "Yes" ["is_active"]=> int(1) ["has_finished"]=> int(0) ["student_category"]=> string(1) "5" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["Biometric_ID"]=> string(1) "1" ["siblings"]=> string(14) "Cynthia Taylor" ["guardian_name"]=> string(14) "Cynthia Taylor" ["guardian_occupation"]=> string(13) "Senior Editor" ["guardian_qualification"]=> string(20) "Research Assistant I" ["guardian_email_id"]=> string(23) "educianparent#gmail.com" ["gaurdain_contact_details"]=> string(10) "9419513603" ["guardian_relationship"]=> string(6) "Father" ["height"]=> string(3) "4.9" ["weight"]=> string(4) "34.9" ["allergies"]=> string(0) "" ["batch"]=> int(2) ["academicyear"]=> string(4) "2015" ["batchhistory"]=> array(1) { [0]=> array(5) { ["batchid"]=> float(2) ["academic_year"]=> string(4) "2015" ["course"]=> string(24) "58131c7099fbad4c1d0001c2" ["sequenceno"]=> int(1) ["courseId"]=> object(MongoId)#24 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } } } ["uploads"]=> array(1) { ["profile_pic"]=> string(39) "58131c7799fbad4c1d000202schoolgirl2.jpg" } ["created_at"]=> NULL ["updated_at"]=> string(0) "" ["routearray"]=> array(2) { [0]=> array(5) { ["routeid"]=> int(2) ["academicyear"]=> string(4) "2016" ["current"]=> int(0) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "09/28/2016" } [1]=> array(5) { ["routeid"]=> int(3) ["academicyear"]=> string(4) "2016" ["current"]=> int(1) ["vehicleno"]=> string(9) "JK01S8764" ["dateofassignment"]=> string(10) "11/17/2016" } } ["HostelAlloted"]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#25 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#26 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } ["HostelAllotmentHistory"]=> array(1) { [0]=> array(7) { ["Food Preferences"]=> string(4) "Both" ["Hostel"]=> object(MongoId)#27 (1) { ["$id"]=> string(24) "58138aee99fbade41e000031" } ["Floor"]=> string(7) "Floor_1" ["RoomNumber"]=> int(11) ["Approved"]=> string(3) "yes" ["Approved On"]=> object(MongoDate)#28 (2) { ["sec"]=> int(1472322600) ["usec"]=> int(0) } ["Academic Year"]=> string(4) "2016" } } ["courseId"]=> object(MongoId)#29 (1) { ["$id"]=> string(24) "58131c7099fbad4c1d0001c2" } }
...
...
I am trying to bring those student on the beginning of array whose first name is brian and rest of students should at the bottom.
Now the above code place the students on the top of the array but the rest of the students are ommited/removed. I don't want them to be removed.
I am trying to tweak what code to write in else condition.
Please help!!!
Fill $arrayStudents before the loop otherwise only matches will be collected and unshifted.
Extend example of php.net/unshift
$add = array('big');
$queue = array(
array("orange"),
array("banana"),
array("apple1"),
array("raspberry")
);
array_unshift($queue, $add);
print_r($queue);
In your case: search for keys you want to unshift, the rest goes to the standard $list = array(/list of studends/):
$list = array();
$bringToTop = array('brian', 'elvis');
foreach($data as $item) {
if (/* in in the list to bring on top*/) {
$memory[] = $item;
} else {
$list[] = $item;
}
}
foreach($memory as $item) {
array_unshift($list, $item);
}
print_r($list);
Kind regards
Add $arrayStudents[] = $row; to your else scope. Here is a simple demo to illustrate it.
When matches add the element to the result from the start of array, unmatches add them from the end of the array.

How to filter and read the multiple array

it's my first question here. I hope I will not be annoying.
I have to read array and put the data to JS script.
This is var_dumped data from array:
array(2406) {
[0]=>
array(2) {
["brand"]=>
string(4) "Mazda"
["model"]=>
string(9) "CX-4"
}
[1]=>
array(2) {
["brand"]=>
string(4) "Mazda"
["model"]=>
string(19) "CX-5"
}
[...]
[120]=>
array(2) {
["brand"]=>
string(12) "Mercedes"
["model"]=>
string(10) "E-class"
}
[121]=>
array(2) {
["brand"]=>
string(12) "Mercedes"
["model"]=>
string(10) "S-class"
}
[122]=>
array(2) {
["brand"]=>
string(9) "Volkswagen"
["model"]=>
string(10) "Golf"
}
[123]=>
array(2) {
["brand"]=>
string(9) "Volkswagen"
["model"]=>
string(10) "Polo"
}
[124]=>
array(2) {
["brand"]=>
string(9) "Volkswagen"
["model"]=>
string(10) "Touareg"
}
}
And I have to return script like this:
"Mazda" : { // the brand
"CX-4",// the list of brand’s models
"CX-5",
},
"Mercedes" : {
"E-class",
"S-class",
},
"Volkswagen" : {
"Golf",
"Polo",
"Touareg",
},
I really need this and I don't have any ideas to solve it. Please help me.
I've tried to use:
foreach($row as $brand => $key){
echo "\"$brand\" : {\n";
print $key[model];
echo "},\n";
}
But unfortunately it returns only
"Mazda" : {
CX-4},
"Mercedes" : {
E-class},
"Volskwagen" : {
Golf},
Only one model for one brand :-/
Maybe this :
foreach($row as $brand => $innerArray){
echo "\"$brand\" : {\n";
foreach($innerArray as $innerRow => $value){
echo $value . "<br/>";
}
}

Access elements inside multidimensional array

I have this multidimensional array in PHP:
array(4) {
["took"]=> int(2)
["timed_out"]=> bool(false)
["_shards"]=> array(3) {
["total"]=> int(5)
["successful"]=> int(5)
["failed"]=> int(0)
}
["hits"]=> array(3) {
["total"]=> int(3)
["max_score"]=> float(2.3578677)
["hits"]=> array(1) {
[0]=> array(5) {
["_index"]=> string(13) "telephonebook"
["_type"]=> string(6) "person"
["_id"]=> string(22) "M5vJJZasTGG2L_RbCQZcKA"
["_score"]=> float(2.3578677)
["_source"]=> array(8) {
["Mob"]=> string(19) "XXX"
["Title"]=> string(13) "Analyst"
["Department"]=> string(8) "Analysis"
["Country"]=> string(6) "Sweden"
["Tlf"]=> string(0) ""
["Name"]=> string(16) "XXX"
["Email"]=> string(29) "XX#retro.com"
["thumbnailPhoto"]=> string(0) ""
}
}
}
}
}
The array has several "hits" inside "hits" and I want to loop trough and print out the stuff inside "_source". I have tried a few different approaches but I cant figure out any way to do this. Please help me.
foreach ($array['hits']['hits'][0]['_source'] as $key => $value) {
//do stuff
}
Try this
foreach ($arr['hits']['hits'] as $val)
{
echo $val['_source']['Mob'];
}
like this
I think this may handle it for you. Replace $the_array_you_provided with your "main" array variable (you've not specified it in the post).
$hits = $the_array_you_provided['hits']['hits'];
foreach ($hits as $hit) {
echo $hit['_source']['Title'];
//print everything in the array
//print_r($hit['_source']);
}
Any help feel free to ask.
Try this:
foreach ($array['hits']['hits'] as $hit) {
foreach ($hit['_source'] as $source) {
echo $source, '<br>';
}
}

Categories