-1

I am trying to loop through each key but i am facing a problem of same value repeating inside for each loop

Here is example of my current code and result (click here)

here is my code so far

<?php
$data2 = array(
        'category_name' => '33287*100*prescription*1,32457*1250*lab*1'
    );
    $result = array('0' => (object)$data2);

    foreach ($result as $key => $category) {
        $category_name = explode(',', $category->category_name);
    }

    $newresults=[];
    foreach ($category_name as $key) {
        $category->category_name = $key;
        $newresults[]=$category;
    }

    $result=$newresults;

    $newresults=[];

        $category->items_count = 0;
        
        foreach ($result as $key => $value) {

            list($sale_key, $sale_value) = explode('*', $value->category_name);
            // $category->items_count += count($sale_value);
            $newresults[]=$category;
        }

    $result=$newresults;

   

i am expect the result should be

Array
(
    [0] => stdClass Object
        (
            [category_name] => 33287*100*prescription*1
            [items_count] => 0
        )

    [1] => stdClass Object
        (
            [category_name] => 32457*1250*lab*1
            [items_count] => 0
        )

)
1
  • added please check the code Commented Jan 24, 2022 at 8:54

2 Answers 2

0

The bug is that you're relying only on the last version of $category after you've finished looping it earlier - you'd have to be using it within the loop where it's assigned, in order to get each value in turn, or you could use $value from your last foreach loop.

But as a general observation, this code has way too many loops etc. just for processing one array in the way you've requested. Here's a much simpler version:

$category_name = '33287*100*prescription*1,32457*1250*lab*1';
$category_name_arr = explode(',', $category_name);

print_r($category_name_arr);

$newresults=[];

foreach ($category_name_arr as $cat) {
    $newresults[] = (object) array("category_name" => $cat, "items_count" => 0);
}

print_r($newresults);

Demo: http://sandbox.onlinephpfunctions.com/code/065a32b40f67e00aa85f0f5b58ecacd510f2f38a


If you still need to be able to support multiple lines of input, you can do it like this, by just merging the exploded arrays before you process them:

$data = array(
    '33287*100*prescription*1,32457*1250*lab*1',
    '33222*900*prescription*3,22233*1200*lab*2',
    );
$category_name_arr = [];

foreach ($data as $category_name)
{
  $category_name_arr = array_merge($category_name_arr, explode(',', $category_name));
}

print_r($category_name_arr);

$newresults=[];

foreach ($category_name_arr as $cat) {
    $newresults[] = (object) array("category_name" => $cat, "items_count" => 0);
}

print_r($newresults);
Sign up to request clarification or add additional context in comments.

8 Comments

this is criteria to keep all kind of loops can you please help me to fix the problem in current code i have this is not to just print this is logical pieces of code i don't care about performance
The performance difference isn't going to be noticeable. I was thinking of the readability and maintainability of your code actually. Why do you "need" to keep all the loops? That doesn't make any sense, when you can write it much more easily like this. The other loops weren't necessary or helpful, they just made it harder to work out what the code was going to do. Also, assigning $newresults, then emptying it and reassigning it all over again was another redundant step
Anyway my first sentence in the answer explains how to fix the current code, if you so wish.
i am agreeing with you and learning, i need to keep on the same code because i need to keep adding new things in future
i am newbie here and can't understand your sentence very well, can you please edit your answer with my code
|
0

adding $category = new stdClass();

foreach ($category_name as $key) {
            

$category = new stdClass();

            $category->category_name = $key;
            $newresults[]=$category;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.