I have some sorting problem to this i want it to compare properly but the array result is incrementing
$query=mysqli_query($conn,"SELECT * FROM questions WHERE MATCH (question) AGAINST ('$msg*' IN BOOLEAN MODE) limit $limit ");
}
while($row = mysqli_fetch_assoc($query)){
$id = $row ["id"];
$question = $row ["question"];
$answer = $row ["answer"];
$words2 = explode(" ", cleanWords($question));
foreach($words2 as $word)
{
$stem = PorterStemmer::Stem($word);
if(!in_array($stem, $stop_words)) {
$stem_words[] = $stem;
}
}
print_r($stem_words);
echo "<br>";
}
}
Now my result set is incrementing like
Array ( [0] => What [1] => IT [2] => STAFF [3] => )
Array ( [0] => What [1] => IT [2] => STAFF [3] => [4] => what [5] => it [6] => )
Array ( [0] => What [1] => CEIT [2] => UCC [3] => [4] => what [5] => it [6] => [7] => Where [8] => it [9] => )
**I want to get the result like this
Array ( [0] => What [1] => IT [2] => STAFF [3] => )
Array ( [0] => what [1] => it [2] => )
Array ( [0] => Where [1] => it [2] => )
The problem is that you are just adding each word to the $stem_words array. Instead for each record, build a list of words for this record ($newList in this code) and add this record to the $stem_words array...
$newList = [];
foreach($words2 as $word)
{
$stem = PorterStemmer::Stem($word);
if(!in_array($stem, $stop_words)) {
$newList[] = $stem;
}
}
$stem_words[] = $newList;
Related
I have an array in PHP loop output like this
this is my php code :
$sql = "SELECT * FROM `acc` ";
$stm = $conn->prepare($sql);
$stm->execute();
$array= [];
while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
$array[]=$row['h_id'];
}
print_r($array);
and output is :
Array
( [0] => 11
[1] => 12
[2] => 13
[3] => 1101
[4] => 1102
[5] => 110101
[6] => 110102
[7] => 1201
[8] => 1202
[9] => 1301
[10] => 1302
[11] => 1303
[12] => 130201
[13] => 130202
[14] => 130301
[15] => 130302
)
and I want to sort and rearrange array to multi-dimension for parent and child with php Loop Like this :
Array
(
[11] => Array
(
[1101] => Array
(
[0] => 110101
[1] => 110102
)
)
[12] => Array
(
[0] => 1201
[1] => 1202
)
[13] => Array
(
[0] => 1301
[1302] => Array
(
[0] => 130201
[1] => 130202
)
[1303] => Array
(
[0] => 130301
[1] => 130302
)
)
how to implemention and push array to another with php.
thanks!
I absolutely don't understand what you're doing, but I think I've grasped how your input may be mapped to desired output. Here is the code:
<?php
$flat = [
11,
12,
13,
1101,
1102,
110101,
110102,
1201,
1202,
1301,
1302,
1303,
130201,
130202,
130301,
130302,
];
function expand($flat)
{
$expanded = [];
foreach ($flat as $element) {
$sections = str_split((string)$element, 2);
$tmp = &$expanded;
$path = '';
foreach ($sections as $section) {
$path .= $section;
if (!isset($tmp[$path])) {
$tmp[$path] = [];
}
$tmp =& $tmp[$path];
}
unset($tmp);
}
return $expanded;
}
function fold($expanded) {
$folded = [];
foreach ($expanded as $key => $value) {
if ($value === []) {
$folded[] = $key;
} else {
$folded[$key] = fold($value);
}
}
return $folded;
};
print_r(fold(expand($flat)));
And here is its output (which is suspiciously close to yours):
Array
(
[11] => Array
(
[1101] => Array
(
[0] => 110101
[1] => 110102
)
[1102] => 1102
)
[12] => Array
(
[0] => 1201
[1] => 1202
)
[13] => Array
(
[0] => 1301
[1302] => Array
(
[0] => 130201
[1] => 130202
)
[1303] => Array
(
[0] => 130301
[1] => 130302
)
)
)
There are some differences, though, e.g. there is element 1102 in your input, but in your sample output it is absent, and I'm not sure if you forgot it or if you have filtered it out for some reason.
$routes array:
[admin/login] => Array
(
[0] => POST|AJAX
[1] => admin/login
[2] => admin/login
)
[admin/logout] => Array
(
[0] => POST
[1] => admin/logout
[2] => admin/logout
)
[admin/dashboard] => Array
(
[0] => GET
[1] => admin/dashboard
[2] => admin/dashboard
)
[admin/products] => Array
(
[0] => GET
[1] => admin/products/[:pag]
[2] => admin/products/all
)
[admin/products/add] => Array
(
[0] => GET
[1] => admin/product/add
[2] => admin/products/add
)
[admin/products/insert] => Array
(
[0] => POST
[1] => admin/product/insert
[2] => admin/products/insert
)
[admin/products/show] => Array
(
[0] => GET
[1] => admin/product/[i:id]
[2] => admin/products/show
)
[admin/products/edit] => Array
(
[0] => GET
[1] => admin/product/[i:id]/edit
[2] => admin/products/edit
)
[admin/products/update] => Array
(
[0] => POST
[1] => admin/product/update
[2] => admin/products/update
)
[admin/products/delete] => Array
(
[0] => POST
[1] => admin/product/delete
[2] => admin/products/delete
)
[admin/categories] => Array
(
[0] => GET
[1] => admin/categories
[2] => admin/categories/all
)
[admin/categories/add] => Array
(
[0] => GET
[1] => admin/category/add
[2] => admin/categories/add
)
[admin/categories/insert] => Array
(
[0] => POST
[1] => admin/category/insert
[2] => admin/categories/insert
)
[admin/categories/show] => Array
(
[0] => GET
[1] => admin/category/[i:id]
[2] => admin/categories/show
)
[admin/categories/edit] => Array
(
[0] => GET
[1] => admin/category/[i:id]/edit
[2] => admin/categories/edit
)
[admin/categories/update] => Array
(
[0] => POST
[1] => admin/category/update
[2] => admin/categories/update
)
[admin/categories/delete] => Array
(
[0] => POST
[1] => admin/category/delete
[2] => admin/categories/delete
)
[admin/api] => Array
(
[0] => GET
[1] => admin/api
[2] => admin/api/all
)
[admin/filter/sessions] => Array
(
[0] => POST|AJAX
[1] => admin/filter/sessions
[2] => admin/filters/sessions
)
This is $admin_routes array (build from $routes):
[login] => Array
(
[0] => admin/login
)
[logout] => Array
(
[0] => admin/logout
)
[dashboard] => Array
(
[0] => admin/dashboard
)
[products] => Array
(
[0] => admin/products
[1] => admin/products/add
[2] => admin/products/insert
[3] => admin/products/show
[4] => admin/products/edit
[5] => admin/products/update
[6] => admin/products/delete
)
[api] => Array
(
[0] => admin/api
)
[filter] => Array
(
[0] => admin/filter/sessions
)
[categories] => Array
(
[0] => admin/categories
[1] => admin/categories/add
[2] => admin/categories/insert
[3] => admin/categories/show
[4] => admin/categories/edit
[5] => admin/categories/update
[6] => admin/categories/delete
)
I need some help from you guys about $admin_routes.
I want to combine all sub-arrays from $admin_routes, from same area, which has only one element, in a new sub-array with key name formed from their key names.
Also i want the new sub-array to have same 'position' in line.
My imperfect solution:
// creating $admin_routes
$admin_routes = [];
foreach ($routes as $name => $route) {
if (preg_match('/^admin\/(.*)$/', $name))
$admin_routes[explode('/', $name)[1]][] = $name;
}
// imperfect solution for what i want
$single = false;
$waiting = [];
foreach ($admin_routes as $section => $routes) {
if (count($routes) == 1) { // remember all sub-arrays, with one single element, from same area
$single = true;
$waiting[$section] = $routes;
}
else if ($single) { // if found all sub-arrays, with one single element, from same area
$name = '';
$subarray = [];
foreach ($waiting as $section => $routes) {
// creating only one sub-array
$name .= ($section . ' ');
$subarray = array_merge($subarray, $routes);
unset($admin_routes[$section]);
}
// the problem is, sub-array it's added at the end of $admin_routes
// (not where sub-array's elements were before)
$admin_routes[$name] = $subarray;
$single = false;
$waiting = [];
}
}
The problem is, the new sub-arrays are placed at the end.
If you have any idea, please help me. Thx.
Using your actual result $admin_routes, you could loop through them and rebuild the desired array:
$real_routes = []; // The new array
$singles_key = ''; // The key for combined sub-arrays
$singles_routes = []; // The routes for combined sub-arrays
foreach ($admin_routes as $name => $r) {
/* If this route array has a single entry,
add its name and value to our temp vars */
if (count($r) === 1) {
$singles_key .= $name . ' ';
$singles_routes = array_merge($singles_routes, $r);
} else {
/* If then a route array has multiple value,
save the combined singles and then save this one */
if (!empty($singles_key)) {
$real_routes[trim($singles_key)] = $singles_routes;
$singles_key = '';
$singles_routes = [];
}
$real_routes[$name] = $r;
}
}
/* If the loop ends but the temp vars aren't empty,
save the lasts combined singles */
if (!empty($singles_key)) {
$real_routes[trim($singles_key)] = $singles_routes;
}
// Then to output:
echo '<pre>' . print_r($real_routes, true) . '</pre>';
My if ($arr[$key][0] == 'th') // is not working
I've been trying to convert my array value to string, but still my if statement is not true, please help!!!
Code:
$preparedstring = "
pb,List information,ca;
'th',Name,ca;
th,Surname,ca;
th,ID,ca;
th,Gender,ca;
th,Gender,ca;
pb,personal details,ca;
fl,Name,la;
fl,Surname,la;
fl,ID,la;
fl,Gender,la
";
$columncount = 0;
$firstindex = 0;
$lastindex = 0;
$functionvalue = "";
$outerARR = explode(";", $preparedstring);
$arr = array();
foreach ($outerARR as $arrvalue){
$innerarr = explode(",", $arrvalue);
$arr[] = $innerarr;
}
print_r($arr);
unset($arrvalue);
//Read the array
foreach ($arr as $key => $arrvalue1){
$functionvalue = $arr[$key][0];
echo $functionvalue;
//Get column key functions
if ($arr[$key][0] == 'th'){
$firstindex++;
echo "<p>".$a."</p><br/>";
}
elseif ( $firstindex > 0 )
{
$lastindex = $key - 1;
}
foreach ($arrvalue1 as $arrvalue2){
}
$a++;
}
echo "<p>".$arr[0][0].".</p><br/> First Index : ".$firstindex. " Last Index : ".$lastindex. "---".$functionvalue;
Output:
Array ( [0] => Array ( [0] => pb [1] => List information [2] => ca ) [1] => Array ( [0] => 'th' [1] => Name [2] => ca ) [2] => Array ( [0] => th [1] => Surname [2] => ca ) [3] => Array ( [0] => th [1] => ID [2] => ca ) [4] => Array ( [0] => th [1] => Gender [2] => ca ) [5] => Array ( [0] => th [1] => Gender [2] => ca ) [6] => Array ( [0] => pb [1] => personal details [2] => ca ) [7] => Array ( [0] => fl [1] => Name [2] => la ) [8] => Array ( [0] => fl [1] => Surname [2] => la ) [9] => Array ( [0] => fl [1] => ID [2] => la ) [10] => Array ( [0] => fl [1] => Gender [2] => la ) )
pb 'th' th th th th pb fl fl fl fl
pb.
First Index : 0 Last Index : 0--- fl
This is the output, as you can see my if statement is false
You need to remove all newlines from the string and then perform the operations:
$outerARR = explode(";", preg_replace('~[\r\n]+~', '', $preparedstring));
what I'm doing wrong here.
I'm trying to send the json_encode to a dataTables, using a MWS Themeforest Theme.
Here is my ajax code:
<?php
include("../inc/connect.inc.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
$sql = "SELECT vv.id, vv.licenseplate, vv.emergency, vv.alert, vt.name FROM vehicle_vehicle AS vv
INNER JOIN client_user_vehicle_map AS cv ON cv.id_vehicle = vv.id
INNER JOIN vehicle_type AS vt ON vt.id = vv.id_type
WHERE cv.id_user = ".$_GET["idUser"];
$rst = mysql_query($sql);
$val = "";
while($lst = mysql_fetch_array($rst)){
$sql2 = "SELECT id, ignition, date FROM vehicle_tracking WHERE id_vehicle = ".$lst["id"]." ORDER BY date DESC LIMIT 1";
$rst2 = mysql_query($sql2);
while($lst2 = mysql_fetch_assoc($rst2)){
$date = date_create($lst2["date"]);
$comm = date_format($date, 'd-m-Y H:i:s');
if ($lst2["ignition"] == "1"){
$val = "<center><i class='icol32-bullet-green'></i></center>";
}else if ($lst2["ignition"] == "0"){
$val = "<center><i class='icol32-bullet-black'></i></center>";
}
$licenseplate = $lst["licenseplate"];
$data = array($lst["name"], "1", $comm, "1");
$arr[] = array("name" => $licenseplate, "data" => $data);
}
}
$output['aaData'][] = $arr;
echo "<pre>";
print_r($output);
echo "</pre>";
echo json_encode($arr); ?>
The result of my print_r is this array:
Array(
[aaData] => Array
(
[0] => Array
(
[0] => Array
(
[name] => ITU 2198
[data] => Array
(
[0] => Automóvel
[1] => 1
[2] => 09-05-2014 19:17:55
[3] => 1
)
)
[1] => Array
(
[name] => BANCADA
[data] => Array
(
[0] => Automóvel
[1] => 1
[2] => 09-04-2014 11:10:51
[3] => 1
)
)
[2] => Array
(
[name] => IHC 8764
[data] => Array
(
[0] => Automóvel
[1] => 1
[2] => 09-05-2014 19:22:14
[3] => 1
)
)
[3] => Array
(
[name] => IFT 0003
[data] => Array
(
[0] => ISCA
[1] => 1
[2] => 07-05-2014 15:30:13
[3] => 1
)
)
[4] => Array
(
[name] => IFT 0004
[data] => Array
(
[0] => ISCA
[1] => 1
[2] => 07-05-2014 13:20:37
[3] => 1
)
)
)
))
But my json_encode is blank.
Can you help me with this?
Thanks a lot.
Add this line after while(....
$lst2 = array_map('utf8_encode', $lst2);
This will convert all the elements into utf8.
I need to create a multidimensional array with a foreach loop and a while loop.
The first array contains this:
Array
(
[0] => 13-10-14
[1] => 13-10-15
[2] => 13-10-16
[3] => 13-10-17
[4] => 13-10-18
[5] => 13-10-19
)
I need to make it look like this:
Array
(
[0] => Array
(
[date] => 13-10-14
[id] => Array
(
[0] => 012643
[1] => 012667
[2] => 013362
[3] => 016169
[4] => 016839
[5] => 035288
[6] => 035369
[7] => 037664
[8] => 038979
[9] => 039014
[10] => 039036
[11] => 039505
)
)
)
The first array I do a foreach loop with the second I need to make while as it is a sql query.
Here is the code:
foreach ($rs as $results) {
$rowT = $db->query("SELECT id FROM users WHERE LIMIT 10");
while ($rsT = $db->fetch_assoc($rowT)) {
$results['id'][] = $rsT;
}
$l_array[] = $results;
}## Heading ##
print_r($l_array);
Is returning the error:
Fatal error: Can not use string offset to an array
If your $rs is this:
array([0] => 13-10-14
[1] => 13-10-15
[2] => 13-10-16
[3] => 13-10-17
[4] => 13-10-18
[5] => 13-10-19)
Your foreach() would set $results to: 13-10-15, 13-10-16, etc. I think that's your error, since those are strings, not arrays. How about this:
$final_array = array();
foreach ($rs as $results) {
$tmp_array = array('date'=>$results);
$rowT = $db->query("SELECT id FROM users WHERE LIMIT 10");
while ($rsT = $db->fetch_assoc($rowT)) {
$tmp_array['id'][] = $rsT;
}
$final_array[] = $tmp_array;
}## Heading ##
print_r($final_array);