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.
Related
I am trying to parse a Post Array from a multi select dropdown into a comma separated variable so I can insert into a MySQL DB.
The array from the http Post looks like:
array ( [0] => [1] => 1 [2] => 2 [3] => 3 [4] => 5 [5] => 4 );
I want to look like 1,2,3,4,5.
It's driving me nuts. Implode does not seem to work nor does str_replace.
The code below returns a blank screen.
<?php
$array = array ( [0] => [1] => 1 [2] => 2 [3] => 3 [4] => 5 [5] => 4 );
$comma_separated = implode(",", $array);
echo $comma_separated;
?>
Any help is appreciated.
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
)
)
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
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().
Is there a way to foreach() through one array based on a matching value with a different key in another array? In this example, I have a category array ($cat_data) with cat_id as a key and an image array ($img_data) with category_id as a key.
Array (
[0] => Array (
[cat_id] => 1
[cat_name] => Category 1
)
[1] => Array (
[cat_id] => 2
[cat_name] => Category 2
)
)
Array (
[0] => Array (
[img_id] => 2
[img_name] => demo1.jpg
[img_label] => Demo 1
[category_id] => 2
[img_order] => 1
)
[1] => Array (
[img_id] => 3
[img_name] => demo2.jpg
[img_label] => Demo 2
[category_id] => 2
[img_order] => 2
)
[2] => Array (
[img_id] => 4
[img_name] => demo3.jpg
[img_label] => Demo 3
[category_id] => 1
[img_order] => 1
)
)
What I want is to output my display so it looks like the following:
Category 1
demo3.jpg
Category 2
demo1.jpg
demo2.jpg
Since I'm really not great at fully grasping arrays, I thought I'd try Stack, and I haven't been able to find an answer to my question, partially because I'm not sure what to ask for precisely. Any help??
The naïve way:
foreach ($cat_data as $cat) {
echo $cat['cat_name'];
foreach ($img_data as $img) {
if ($img['category_id'] != $cat['cat_id']) {
continue;
}
echo $img['img_name'];
}
}
This is rather inefficient, since it loops through the $imgs array several times, but easy and works.
More efficient:
$images = array();
foreach ($img_data as $img) {
$images[$img['category_id']][] = $img;
}
foreach ($cat_data as $cat) {
echo $cat['cat_name'];
if (isset($images[$cat['cat_id']])) {
foreach ($images[$cat['cat_id']] as $img) {
echo $img['img_name'];
}
}
}
This first groups all images by category into a new array, which you can then loop over directly once.
I would urge you to redesign your array when you fill them with data to instead look something like this.
Array (
[0] => Array (
[cat_id] => 1
[cat_name] => Category 1
[images] = Array(
[0] => Array (
[img_id] => 4
[img_name] => demo3.jpg
[img_label] => Demo 3
[category_id] => 1
[img_order] => 1
)
)
)
[1] => Array (
[cat_id] => 2
[cat_name] => Category 2
[images] = Array(
[0] => Array (
[img_id] => 4
[img_name] => demo3.jpg
[img_label] => Demo 3
[category_id] => 1
[img_order] => 1
)
[1] => Array (
[img_id] => 2
[img_name] => demo1.jpg
[img_label] => Demo 1
[category_id] => 2
[img_order] => 1
)
)
)
)
Then you would have all the relational data connected and would just have to loop through your array of categories and print the images associated with each one in turn. The numbered indexes could even be changed to associative names if the id of the catagory weren't important for example. Then the array could be indexed with the name of the category and just contain the images of that category.
If the images are to be used in other places where you initial layout of those fits better you could still use this layout for your main data graph. Just replace the actual data of the images in the images array under each category with a reference to the actual image object.