Save the value from another function and check PHP - php

I call again THIS function so I get some new $key is this and i want to add it to same array so I can check if it is same so unique it. Some thing like global array.
function getNodesInfo($node)
{
foreach ($result as $key => $value)
{
$items[]=$key;
}
echo ("-----."\n");
print_r($items."\n");
getNodesInfo($subNode);
}
It is my output
-------
Array
(
[0] => author
[1] => title
[2] => genre
[3] => price
[4] => publish_date
[5] => description
)
-------
Array
(
[0] => author
[1] => title
[2] => genre
[3] => price
[4] => publish_date
[5] => description
)
-------
Array
(
[0] => book
)
and I want my output looklike
Array
(
[0] => author
[1] => title
[2] => genre
[3] => price
[4] => publish_date
[5] => description
)
-------
Array
(
[0] => book
)

Use the below Algorithm for this.
Step 1 : Create a Blank array.
Step 2 : Each time when you get an array with data, Check whether that array exists in the Blank array or not.
Step 3 : If it does not exist, Add that array(with data) to Blank array. If it exists Do not add that array just discard it.
At the end you will get the unique values inside Blank array which you have created. It is a simple process

Related

PHP Array delete an entire array by one element value

So this is how my Array ($dataArray) looks oks like:
Array
(
[0] => Array
(
[0] => Date
[1] => Time
[2] => Duration
[3] => Info
[4] => Client
)
[1] => Array
(
[0] => 2021-12-01
[1] => 10:45:43
[2] => 237
[3] => Some text from
[4] => Client 1
)
[2] => Array
(
[0] => 2021-12-01
[1] => 11:29:13
[2] => 77
[3] => Nothing important
[4] => Client 2
)
[3] => Array
(
[0] => 2021-12-01
[1] => 11:53:03
[2] => 44
[3] => anonymous
[4] => Client 1
)
I need to Loop trough it to search the Client Names, and if i find the matching name in the Element 4 then delete the entire Array.
$ExportKDname = "Client 1"
foreach($dataArray as $key => $sub_array) {
if($sub_array[4] == $ExportKDname) {
unset($dataArray[$key]);
break;
}
}
print_r($dataArray);
But with this code none of the arrays will be deleted. And I just can not find the right way to do it.
The Final array what I need to look like if we find the "Client 1" in the array would be like this:
Array
(
[0] => Array
(
[0] => Date
[1] => Time
[2] => Duration
[3] => Info
[4] => Client
)
[1] => Array
(
[0] => 2021-12-01
[1] => 11:29:13
[2] => 77
[3] => Nothing important
[4] => Client 2
)
In the if condition you are saying "if u match with $sub_arr[4] == $ExportKDname unset it and stop the loop". the machine doing that. when it matched first time it removes and stoping. If u wanna delete all match do not write break; let it continue. So delete or make it comment break; line.
You can array_filter your variable and check if value is in_array.
With PHP 7.4+ syntax it should look like this:
$result = array_filter($dataArray, fn ($innerArray) => !in_array('Client 1', $innerArray));

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.

Storing array values in two database tables

I have an array, I just print it as print_r($data) which looks like-
Array
(
[0] => Array
(
[0] => Title
[1] => Featured Image
[2] => Catagories
[3] => Tags
[4] => Content
)
[1] => Array
(
[0] => title 1
[1] => img1.jpg
[2] => cat 1
[3] => tag 1
[4] => post 1 content
)
[2] => Array
(
[0] => title 2
[1] => img2.jpg
[2] => cat2
[3] => tag 2
[4] => post 2 content
)
[3] => Array
(
[0] => title 3
[1] => img3.jpg
[2] => cat3
[3] => tag3
[4] => post 3 content
)
}
I have two tables-
1) sa_posts
2) sa_terms
In sa_posts table I want to store title, feature image, content and in sa_terms table I have to store categories and tags.
How is this possible using foreach or for loop?
Get array values by keys, and write insert query for below values
<?php
foreach($data as $d)
{
//sa_posts values title=$d[0], feature image=$d[1], content = $d['4']
//sa_terms values categories =$d[2], tags=$d[3]
}
?>
Foreach is your friend with arrays.
foreach($array as $element) {
//sql insert to sa_posts for $element[0], $element[1], $element[2]
//sql insert to sa_terms for $element[3], $element[4]
}
Personally, I've not seen an array whose first element describes the contents of the rest, though. If this isn't common practice, I'd consider using an associative array instead otherwise you'll be forever skipping the first element of your array and if you forget, you're going to end up with some weird data.

grab category value from mediawiki markup in php

I am working on retriving category values from a wiki markup text in loop, could not grab category values from the markup using regex match in php
The Markup Text Contains the category values as
$input_wiki_markup = "
[[Category:Google]]
[[Category:Tricks]]
[[Category:Google Search]]
[[Category:Filters]]
[[Category:Search]]
[[Category:Tips]]";
Here's what I have tried so far
$matches = array();
if(preg_match("/\[\[(Category):(.+)*\]\]/i", $input_wiki_markup, $matches)){
print_r($matches);
}
This is the output
Array
(
[0] => [[Category:Google]][[Category:Tricks]][[Category:Google Search]][[Category:Filters]][[Category:Search]][[Category:Tips]]
[1] => Category
[2] => Google]][[Category:Tricks]][[Category:Google Search]][[Category:Filters]][[Category:Search]][[Category:Tips
)
But I'm trying to get output array with only category values after colon , i.e.
Array
(
[0] => Google
[1] => Tricks
[2] => Google Searcg
)
And so on.
What changes should i make to my regex to get only category values filled up in the $mathces array
Or should i use oter php function instead of preg_match ?
Kindly note that, the $input_wiki_markup also containes other text around the [[Categpry:xyz]] tags
all you need was an all
$input_wiki_markup="
[[Category:Google]]
[[Category:Tricks]]
[[Category:Google Search]]
[[Category:Filters]]
[[Category:Search]]
[[Category:Tips]]
";
$matches = array();
if(preg_match_all("/\[\[(Category):(.+)*\]\]/i", $input_wiki_markup, $matches)){
print_r($matches);
}
OUTPUT:
Array
(
[0] => Array
(
[0] => [[Category:Google]]
[1] => [[Category:Tricks]]
[2] => [[Category:Google Search]]
[3] => [[Category:Filters]]
[4] => [[Category:Search]]
[5] => [[Category:Tips]]
)
[1] => Array
(
[0] => Category
[1] => Category
[2] => Category
[3] => Category
[4] => Category
[5] => Category
)
[2] => Array
(
[0] => Google
[1] => Tricks
[2] => Google Search
[3] => Filters
[4] => Search
[5] => Tips
)
)

displaying and sorting a multidimensional array

i have an array.i need to get the item id check in the db and get the manufacturer.
after that I need to write every item to a HTML table where the tables are created for all manufacurers in the array.
example
Items in these 3 arrays are made by two seperate manufacturers.
Now I need help with the code to dinamicly create table for each manufacturer and add the corresponding item in the table
[0] => Array
(
[0] => 3 //item id
[1] => 1 //quantity
[2] => efg //some text
[3] => 50 //price
)
[1] => Array
(
[0] => 1
[1] => 1
[2] => bla bla bla
[3] => 10
)
[2] => Array
(
[0] => 5
[1] => 1
[2] => abe
[3] => 15
)
Sort your data in the SQL query. [best]
usort(), uasort(), uksort().

Categories