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));
Related
I have two associative arrays like
Array
(
[0] => Array
(
[0] => 2022-01-19
[1] => 6
)
[1] => Array
(
[0] => 2022-01-20
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-22
[1] => 2
)
)
and
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-23
[1] => 2
)
)
I need to merge them with the date and want a result array-like below
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 0
[2] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 0
[2] => 1
)
[2] => Array
(
[0] => 2022-01-19
[1] => 6
[2] => 0
)
[3] => Array
(
[0] => 2022-01-20
[1] => 1
[2] => 0
)
[4] => Array
(
[0] => 2022-01-21
[1] => 1
[2] => 1
)
[5] => Array
(
[0] => 2022-01-22
[1] => 2
[2] => 0
)
[6] => Array
(
[0] => 2022-01-23
[1] => 0
[2] => 2
)
)
I tried with the below code but not any success.
$final_array = [];
foreach($openTicket as $val){
$closeTicketNo = 0;
foreach($closeTicket as $value){
if($val[0] == $value[0]){
$closeTicketNo = $value[1];
}
}
$final_array[] = [$val[0],$val[1],$closeTicketNo];
}
I get all the elements from the $openTicket but not get all the elements from a $closeTicket to my result array $final_array
This code first finds all of the unique dates (using array_unique) from the first values in each array (array_column fetches the values and array_merge puts them into 1 array).
Then it indexes each array by the dates (using array_column again).
Finally looping through the unique dates and adding a new element to the output with the values (using ?? 0 so that if no value is present the array is still filled properly)...
$dates = array_unique(array_merge(array_column($openTicket, 0), array_column($closedTicket, 0)));
$open = array_column($openTicket, 1, 0);
$closed = array_column($closedTicket, 1, 0);
$finalArray = [];
foreach ($dates as $date) {
$finalArray[] = [$date, $open[$date] ?? 0, $closed[$date] ?? 0];
}
You can try like this
$array1 = [
['2022-01-19',6],
['2022-01-20',1],
['2022-01-21',0]
];
$array2 = [
['2022-01-17',6],
['2022-01-20',2],
['2022-01-21',1]
];
function mergeMultiple($array1,$array2){
foreach($array1 as $item1){
$mergedArray[$item1[0]] = $item1;
}
foreach($array2 as $item2){
if(isset($mergedArray[$item2[0]])){
array_push($mergedArray[$item2[0]],$item2[1]);
$mergedArray[$item2[0]] = $mergedArray[$item2[0]];
}else{
$mergedArray[$item2[0]] = $item2;
}
}
return array_values($mergedArray);
}
$mergedArray = mergeMultiple($array1,$array2);
ksort($mergedArray);
print_r($mergedArray);
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;
Array:
Array
(
[0] => Array
(
[date] => 2018-05-23
[content] => Array
(
[0] => Array
(
[0] => AAAAAA
[1] => BBBBBB
[2] => CCCCCC
[3] => DDDDDD
[4] => EEEEEE
[5] => FFFFFF
[6] => GGGGGG
[7] => HHHHHH
[8] => IIIIII
[9] => JJJJJJ
)
)
)
[1] => Array
(
[date] => 2018-05-22
[content] => Array
(
[0] => Array
(
[0] => KKKKKK
[1] => LLLLLL
[2] => MMMMMM
[3] => NNNNNN
...
I need to be able to create a foreach loop that will create a database record from 3 variables.
foreach #1:
$date = $array['content']['date']; //2018-05-23
$headline = $array['content'][0][$key]; // AAAAAA
$content = $array['content'][0][$key]; // BBBBBB
foreach #2:
$date = $array['content']['date']; //2018-05-23
$headline = $array['content'][0][$key]; // CCCCCC
$content = $array['content'][0][$key]; // DDDDDD
Until it finishes with the first sub-array and then goes to the second array:
foreach #6:
$date = $array['content']['date']; //2018-05-22
$headline = $array['content'][0][$key]; // KKKKKK
$content = $array['content'][0][$key]; // LLLLLL
I've been trying to group the arrays with array_chunk with no success and then I tried to write a small fix to order the array properly with this:
if ($x <= 10) {
if ( $a < 2 ) {
$a++;
} else {
$x++;
$a = 1;
}
$res[$i]['content'][$x][] = ltrim($text);
} else {
$res[$i]['content'][$x][] = ltrim($text);
$x = 0;
}
Result:
[date] => 2018-05-23
[content] => Array
(
[0] => Array
(
[0] => AAAAAA
[1] => BBBBBB
)
[1] => Array
(
[0] => CCCCCC
[1] => DDDDDD
)
[2] => Array
(
[0] => EEEEEE
[1] => FFFFFF
)
[3] => Array
(
[0] => GGGGGG
[1] => HHHHHH
)
Which worked for the first array but all the other arrays lost order and were not categorized properly.
Any ideas how this can be created?
EDIT: content will always have 24 records (0-23), so if divided into chunks we should get 12 array chunks for every content sub-array.
I'm not sure how your DB structure looks like because you didn't mention, but you can do something like that:
<?php
$array = [
[
"date" => "date",
"content" => [["A", "B", "C"]]
],
[
"date" => "date",
"content" => [["E", "F", "G", "H"]]
],
];
$sql = "INSERT INTO table (content, date) VALUES ";
foreach ($array as $key => $item) {
$content = $item["content"][0];
for ($i = 0; $i < count($content); $i += 2) {
$letters = $content[$i];
if (isset($content[$i + 1])) {
$letters .= $content[$i + 1];
}
$sql .= "('$letters', '$item[date]'),";
}
}
$sql = rtrim($sql, ",") . ";";
echo $sql;
Will output:
INSERT INTO table (content, date) VALUES ('AB', 'date'),('C', 'date'),('EF', 'date'),('GH', 'date');
<?php
$collection =
array
(
array
(
'date' => '2018-05-23',
'content' => array
(
array
(
'AAAAAA',
'BBBBBB',
'CCCCCC',
'DDDDDD',
'EEEEEE',
'FFFFFF',
'GGGGGG',
'HHHHHH',
'IIIIII',
'JJJJJJ'
)
)
),
array
(
'date' => '2018-05-22',
'content' => array
(
array
(
'KKKKKK',
'LLLLLL',
'MMMMMM',
'NNNNNN'
)
)
)
);
function getChunks($data){
$result = array();
$length = count($data);
for($i=0;$i<$length;$i += 2){
$result[] = array($data[$i],$data[$i+1]);
}
return $result;
}
function groupContentByDate($collection){
$result = array();
foreach($collection as $each_data){
$result[$each_data['date']] = array('content' => array());
foreach($each_data['content'] as $each_content){
$result[$each_data['date']]['content'] = array_merge($result[$each_data['date']]['content'],getChunks($each_content));
}
}
return $result;
}
echo "<pre>";
print_r(groupContentByDate($collection));
OUTPUT
Array
(
[2018-05-23] => Array
(
[content] => Array
(
[0] => Array
(
[0] => AAAAAA
[1] => BBBBBB
)
[1] => Array
(
[0] => CCCCCC
[1] => DDDDDD
)
[2] => Array
(
[0] => EEEEEE
[1] => FFFFFF
)
[3] => Array
(
[0] => GGGGGG
[1] => HHHHHH
)
[4] => Array
(
[0] => IIIIII
[1] => JJJJJJ
)
)
)
[2018-05-22] => Array
(
[content] => Array
(
[0] => Array
(
[0] => KKKKKK
[1] => LLLLLL
)
[1] => Array
(
[0] => MMMMMM
[1] => NNNNNN
)
)
)
)
Array
(
[content_type] => Array
(
[0] => story
[1] => delhi
[2] => tez
)
[type] => Array
(
[0] => video_id
[1] => subcategory
[2] => story_id
)
[fetch_id] => Array
(
[0] => 32
[1] => 32
[2] => 2
)
[order] => Array
(
[0] => 6
[1] => 4
[2] => 5
)
[label] => Array
(
[0] => dsfs fdsf dsf sdf
[1] => dfsdfs
[2] => sdfsdfsd
)
[link] => Array
(
[0] => fsd fsdf sdf
[1] => fsdfsdfdsf
[2] => fsdfdsfds
)
[record] => Array
(
[0] => 10
[1] => 8
[2] => 12
)
)
Above is the array I have to sort this array in the basis of order field and it should shorted all the fields accordingly like below example.
$arr['order'][0] = 4;
$arr['order'][1] = 5;
$arr['order'][2] = 6;
$arr['type'][0] = 'subcategory';
$arr['type'][1] = 'story_id';
$arr['type'][2] = 'video_id';
and so on.....
You can try this -
$new = array();
// Extract and get the keys as values
$order = array_flip($array['order']);
// sort them according to keys
ksort($order);
// loop through main array
foreach($array as $key => $sub_array) {
// loop through order
foreach ($order as $o) {
// store the new value according to order
$new[$key][] = $sub_array[$o];
}
}
Demo
Some lesser solution:
asort($array['order']);
foreach ($array as $key => $subArray) {
$array[$key] = array_replace($array['order'], $subArray);
}
For reset a key sequence you may just to use array_values().
Is it possible to connect the key genres, country, in one value
eg [genres] => value1, value2, value3
because there is a problem such that the Values from previous loop are added to the next loop
Here is printscreen http://s12.postimg.org/sv53nakej/Bez_tytu_u.png
1./////////////////////////////////////////////////////////////////////
stdClass Object
(
[title] => Marsjanin
[origTitle] => The Martian
[genres] => Array
(
[0] => Array
(
[key] => 28
[value] => akcja
)
[1] => Array
(
[key] => 33
[value] => sci-fi
)
)
[country] => Array
(
[0] => Array
(
[key] => 53
[value] => USA
)
[1] => Array
(
[key] => 53
[value] => Australia
)
)
)
2./////////////////////////////////////////////////////////////////////
stdClass Object
(
[title] => Pięćdziesiąt twarzy Greya
[origTitle] => Fifty Shades of Grey
[genres] => Array
(
[0] => Array
(
[key] => 16
[value] => melodramat
)
[1] => Array
(
[key] => 33
[value] => sci-fi
)
)
[country] => Array
(
[0] => Array
(
[key] => 53
[value] => USA
)
)
)
This is my code:
foreach( $urll as $urle){
$results = My_Parser::getMovie($urle);
foreach($results as $itemz => $valuez) {
if($itemz=='genres'){
foreach($valuez as $val) {
$genr[] = $val['value'];
}}
}
$genre = implode(', ', $genr);
echo '<tr><th>'.$genre.'</th><th>Importuj</th></tr>';
}
Values from previous loop are added to the next
ex.
A
B
C
D
Result:
A = A
A+B = B
A+B+C = C
A+B+C+D = D
Output [genres]
Array
(
[0] => akcja
[1] => sci-fi
)
Array
(
[0] => akcja / Incorect value added from first array
[1] => sci-fi / Incorect value added from first array
[2] => melodramat
[3] => sci-fi
)
Your $genr is being appended to constantly. Reset it before entering the nested loop:
foreach( $urll as $urle){
...
$genr = array(); // Add this!
foreach($results as $itemz => $valuez) {
...
}
...
$genre = implode(', ', $genr); // Now you're good.
...
}
You need to initialize the array before the foreach of foreach($valuez as $val)
foreach( $urll as $urle){
$results = My_Parser::getMovie($urle);
foreach($results as $itemz => $valuez) {
if($itemz=='genres'){
$genr=array(); //initialization
foreach($valuez as $val) {
$genr[] = $val['value'];
}
$genre = implode(', ', $genr); // use the variable -- As per as your requirement.
echo '<tr><th>'.$genre.'</th><th>Importuj</th></tr>';
}
}
}