i have products that belong to categories and i need to get such categories and output them as a single array. This is my this code:
$act_prod = array(0=>1,1=>10);
$active_cat = array();
foreach ($act_prod as $act) {
$cat = $this->getDi()->productTable->load($act);
$active_cat[$act] = $cat->getCategories();
}
print_r($active_cat);
Which will output:
Array ( [1] => Array ( [0] => 1 ) [10] => Array ( [0] => 2 ) )
This means product 1 belongs to category 1 and product 10 to category 2 but i dont need all that. I only need the categories like this: Array (1, 2) or Array (0=>1, 1=>2).
What should i use so i get the correct output?
Thank you.
Modified your code to build up just the list you want.
$act_prod = array(0=>1,1=>10);
$active_cat = array(); // will be a flat list of categories
foreach ($act_prod as $act) {
$cat = $this->getDi()->productTable->load($act);
foreach($cat->getCategories as $category) {
// if we have not seen this category on any previous category, push it
if(!in_array($cat->getCategories(), $active_cat)) {
array_push($active_cat, $cat->getCategories());
}
}
}
// if desired, sort array first
print_r($active_cat);
foreach ($act_prod as $act) {
$cat = $this->getDi()->productTable->load($act);
$cats = $cat->getCategories();
foreach($cats as $cat)
{
$active_cat[] = $cat['cat_id'];
}
}
Assuming cat_id is your category id
You need to flatten the $active_cat array, like this:
// ...
foreach ($cat->getCategories() as $category) {
$active_cat[] = $category;
}
// ...
Afterwards, make sure there are no duplicates:
$active_cat = array_unique($active_cat);
Related
I am creating following array that contains all products and all their categories:
$result = $wpdb->get_results("SELECT product_nr, category FROM erp_product_categories",ARRAY_A);
$product_categories = array();
foreach($result as $row){
$product_categories[$row["product_nr"]][] = $row["category"];
}
(product_nr is an integer and category is a string)
Then i want to check if one of the categories of a product matches with an other variable and return true if thats the case:
foreach($product_categories[$ean] as $product_categorie) {
$manages_post = in_array( $product_categorie, $this->term_link_conditions );
if($manages_post == true){
break;
}
}
return $manages_post;
But I am getting the error
Invalid argument supplied for foreach()
is it not possible to loop only through elements of an array with a specific key?
Edit:
The array looks like this
Array
(
[10001] => Array //product_nr
(
[0] => 1 //category
[1] => 4 //category
)
[10002] => Array
(
[0] => 1
[1] => 20
)
//...
)
You should check that what you are passing to foreach is an array by using the is_array function
If you are not sure it's going to be an array you can always check using the following PHP example code:
if (is_array($product_categories[$ean])) {
foreach ($product_categories[$ean] as $product_categorie) {
//do something
}
}
Check out all your foreach statements, and look if the thing before the as, to make sure it is actually an array. Usevar_dump to dump it.
Try this :
if(is_array($product_categories) && sizeof($product_categories) > 0) {
foreach($product_categories as $key => $product_categorie) {
if($manages_post = in_array($key, $this->term_link_conditions)){
return $manages_post;
}
}
}
I figured out a way to do this
$product_category = $product_categories[$ean];
if (is_array($product_category)) {
$matches = array_intersect($product_category, $this->term_link_conditions);
if(sizeof($matches) > 0){
$manages_post = true;
}
}
I am scraping an ecommerce website and need to get some data from products, like product name, price, ...
For that, I have:
...
// library includes...
$html = file_get_html($link);
foreach($html->find('.productBoxClass') as $element){
foreach($element->find('.productTitle') as $product) {
$product = $product->plaintext;
}
foreach($element->find('.price') as $price) {
$price = $price->outertext;
}
// and so on...
}
I wanna save this data in a database. So, I want to save all the data in an array for after verify each product if I have to insert or just update. I am intending to populate an multi-dimensional array with this data:
Each position of the array with another array containing the information about one product... To make it easier to save in the database after...
Any help?
This seems like an abnormal data structure or you should be looping through it differently. But if it is an abnormal structure and the product and price aren't grouped together, they are just listed in the same order, then this should work:
$products = [];
$i = 0;
foreach($element->find('.productTitle') as $product) {
$products[$i++]['product'] = $product->plaintext;
}
$i = 0;
foreach($element->find('.price') as $price) {
$products[$i++]['price'] = $price->outertext;
}
Note the $i++ as the key which will increment $i each loop.
If the product and pricing are grouped together in an element, then you should be looping on that element and there should be no need for a foreach for product and price.
Please check the below code, let me know your thoughts...
<?php
// library includes...
$html = file_get_html($link);
$productArr = array();
foreach($html->find('.productBoxClass') as $element){
$tempArr = array('title' => '','price' => 0,'other' => ''); // declare temp array for stroing each product nodes
foreach($element->find('.productTitle') as $product) {
$tempArr['title'] = $product->plaintext; // To do check for empty text here
}
foreach($element->find('.price') as $price) {
$tempArr['price'] = $price->outertext; // To do validate the price
}
foreach($element->find('.other-features') as $price) {
$tempArr['other'] = $price->outertext; // To do validate the price
}
// and so on... with $tempArr['key']
// then assign
$productArr[] = $tempArr; // save temp array in global product array
}
// Product array
echo '<pre>';print_r($productArr);die;
Use in first foreach the count item:
...
// library includes...
$html = file_get_html($link);
// Array declaration
$products = array();
foreach($html->find('.productBoxClass') as $i => $element){
foreach($element->find('.productTitle') as $product) {
$products[$i]['name'] = $product->plaintext;
}
foreach($element->find('.price') as $price) {
$products[$i]['price'] = $price->outertext;
}
// and so on...
}
And will result:
Array
(
[0] => Array
(
[name] => Product 1
[price] => 1.00
)
[1] => Array
(
[name] => Product 1
[price] => 1.00
),
...
)
I have an array of posts that I want to sort - but before I do, I want to find the id of the post with the highest number of likes.
I loop through the array using a foreach. Although it seems like a waste to do two foreach loops for this - I don't know if there's an alternative when trying to find the highest value beforehand?
Array
(
[0] => Array
(
[id] => 162
[like_count] => 2
etc.
)
[1] => Array
(
[id] => 165
[like_count] => 23
etc.
)
)
So the second post has the highest amount of likes, so I need the ID of 165 - then when I loop through I can do something like
foreach ($posts as $post){
if($most_liked_id == $post["id"]){
// this post is the most liked!
}
}
Any help would be much appreciated - thanks!
$highest = 0;
$highest_id = 0;
foreach($array as $a) {
if($a['like_count'] > $highest) {
$highest = $a['like_count'];
$highest_id = $a['id'];
}
}
Hope I understood you correctly :)
This looks like data retrieved from a database. If so, use the ORDER BY like_count DESC clause in the SQL.
The ID of the post with the most likes will then be at $posts[0]['id'] before you sort by your other method.
very easy task, you loop through your posts.
function get_max_like_post($posts) {
$max_like = 0;
$max_like_post = array();
foreach ($posts as $post) {
if ($post['like_count'] > $max_like) {
$max_like = $post['like_count'];
$max_like_post = $post;
}
}
return $max_like_post['id']
}
You could use usort.
$posts = array(
array('id' => 161, 'like_count' => 0),
array('id' => 162, 'like_count' => 6),
array('id' => 4, 'like_count' => 2),
);
function like_sort($a, $b) {
if ($a['like_count'] == $b['like_count']) {
return 0;
}
return ($a['like_count'] > $b['like_count']) ? -1 : 1;
}
usort($posts, 'like_sort');
// The first element in the array is now the one with the highest like_count.
echo $posts[0]['id']; // 162
Try this:
usort($posts, function($item) { return -$item['like_count']; });
$id = empty($posts) ? null : $posts[0]['id'];
Where $posts is the input array.
Explanation:
first you sort the posts by like count in decreasing fashion
then you get the top post id if there any posts, null otherwise.
What's good about this solution is that you can also select first n posts.
$highest_post_likes = 0;
$highest_post_id = 0;
for($i = 0; $i < sizeof($posts); $i++) {
if ( $posts[$i][like_count] > $highest_post_likes ) {
$highest_post_likes = $posts[$i][like_count];
$highest_post_id = $i;
}
}
// now you can use $posts[$highest_post_id]
Is those datas came from a database like MySQL? If is it, the simpliest solution is to put an "ORDER BY".
You can also make seperate the 'like_count' array keeping the same key than your first array and doing a asort (http://www.php.net/manual/fr/function.asort.php). You will have the key of the highest like count.
You can sort the array in descending order based on like_count and then pick up the id for the first array element.
You can use max function to get the highest value of likes:
foreach ($posts as $post){
$max[]=$post['like_count'];
}
echo max($max['id']);
I'm actually working on ZF. I have a category table with which, I want to create a tree in order to get display the data as below :
Category
--Sub cat 1
--Sub cat 2
----Su sub cat 1
Another Category
--Sub cat 1
//...etc...
I'm using the fetchAll method to get all my data. Everyting works fine. But then I'm now trying to create my tree into a double foreach loop as below :
$tree = array();
foreach($data as $parent){
$tree[$parent->name] = array();
foreach($data as $child){
if($child->parent_id == $parent->id){
$tree[$parent->name][] = $child->name;
}
}
}
The problem is that the loop stop after the main loop first iteration so I'm just getting the first parent and it's sub category but it does not continue to the second parent.
My database table as the following fields :
id, name, parent_id
Any idea?
EDIT
Thanks to you Thibault, it did work using the good old for loop :
for($i=0;$i<count($data);$i++){
$tree[$data[$i]->name] = array();
for($j=0;$j<count($data);$j++){
if($data[$j]->parent_id == $data[$i]->id){
$tree[$data[$i]->name][] = $data[$j]->name;
}
}
}
You may have a conflict between the cursor of both $data variables.
You should use a copy of $data for the second foreach loop.
Or use for loops with $i and $j index, and call them via $data[$i] and $data[$j] to access the array, so the loops don't get messed up.
EDIT
Happy i could help, but after some research, i created this piece of code :
<?
class o {
public $id;
public $name;
public $parent_id;
function __construct($_id,$_name,$_parent) {
$this->id = $_id;
$this->name = $_name;
$this->parent_id = $_parent;
}
}
$data = array(
new o(1,'toto',0),
new o(2,'truc',1),
new o(3,'machin',1),
new o(4,'bidule',2),
new o(5,'titi',3),
new o(6,'tutu',3),
);
$tree = array();
foreach($data as $parent){
$tree[$parent->name] = array();
foreach($data as $child){
if($child->parent_id == $parent->id){
$tree[$parent->name][] = $child->name;
}
}
}
print_r($tree);
And your code works just fine :
(something must be wrong somewhere else ...)
Array
(
[toto] => Array
(
[0] => truc
[1] => machin
)
[truc] => Array
(
[0] => bidule
)
[machin] => Array
(
[0] => titi
[1] => tutu
)
[bidule] => Array
(
)
[titi] => Array
(
)
[tutu] => Array
(
)
)
I'm trying to arrange a group of pages in to an array and place them depending on their parent id number. If the parent id is 0 I would like it to be placed in the array as an array like so...
$get_pages = 'DATABASE QUERY'
$sorted = array()
foreach($get_pages as $k => $obj) {
if(!$obj->parent_id) {
$sorted[$obj->parent_id] = array();
}
}
But if the parent id is set I'd like to place it in to the relevant array, again as an array like so...
$get_pages = 'DATABASE QUERY'
$sorted = array()
foreach($get_pages as $k => $obj) {
if(!$obj->parent_id) {
$sorted[$obj->id] = array();
} else if($obj->parent_id) {
$sorted[$obj->parent_id][$obj->id] = array();
}
}
This is where I begin to have a problem. If I have a 3rd element that needs to be inserted to the 2nd dimension of an array, or even a 4th element that needs inserting in the 3rd dimension I have no way of checking if that array key exists. So what I can't figure out is how to detect if an array key exists after the 1st dimension and if it does where it is so I can place the new element.
Here is an example of my Database Table
id page_name parent_id
1 Products 0
2 Chairs 1
3 Tables 1
4 Green Chairs 2
5 Large Green Chair 4
6 About Us 0
Here is an example of the output I'd like to get, if there is a better way to do this I'm open for suggestions.
Array([1]=>Array([2] => Array([4] => Array([5] => Array())), [3] => Array()), 6 => Array())
Thanks in advanced!
Well, essentially you are building a tree so one of the ways to go is with recursion:
// This function takes an array for a certain level and inserts all of the
// child nodes into it (then going to build each child node as a parent for
// its respective children):
function addChildren( &$get_pages, &$parentArr, $parentId = 0 )
{
foreach ( $get_pages as $page )
{
// Is the current node a child of the parent we are currently populating?
if ( $page->parent_id == $parentId )
{
// Is there an array for the current parent?
if ( !isset( $parentArr[ $page->id ] ) )
{
// Nop, create one so the current parent's children can
// be inserted into it.
$parentArr[ $page->id ] = array();
}
// Call the function from within itself to populate the next level
// in the array:
addChildren( $get_pages, $parentArr[ $page->id ], $page->id );
}
}
}
$result = array();
addChildren( $get_pages, $result );
print_r($result);
This is not the most efficient way to go but for a small number of pages & hierarchies you should be fine.