0

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

Please be noted we should keep on same code structure multiple foreach it's requirement i already posted this question here but didn't get solution instead of solution entire new code as answer imposed on me and nobody is actually taking care of it

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 getting the wrong results like this

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

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

)
15
  • 1
    What do you want to do ? Commented Jan 24, 2022 at 12:10
  • i am looking to get result like this 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 ) ) Commented Jan 24, 2022 at 12:43
  • @uzthegeek first of all use self explanatory variable names. It will definitely simplify the debugging. You make a $result then build $newresults and overwrite the original $result then clear the $newresults to build new set of data and assign it back to $result meanwhile playing with $category. This messing up with variable names make the code difficult for reading. Commented Jan 24, 2022 at 12:54
  • As ADyson said in your other question, you cannot use $category outside of the loop (unless you full understand the consequences). Commented Jan 24, 2022 at 13:01
  • 1
    Including what you commented out, it seems like this is roughly what you are looking for: 3v4l.org/7nkgb Commented Jan 24, 2022 at 13:08

1 Answer 1

1

As noted, because you are reusing variable names, and also using them when their scope might not be correct or accepted, you are causing some confusion.

The code below brings the bottom loop inside of the top loop, because that's where the context really lives. Creating a temporary loop only adds to the potential confusion. If that doesn't work with the additional logic, more changes will be needed. I also changed a bunch of the variable names to hopefully make things more obvious. See the comments in the code for more details.

$reporting_data = array(
    'category_name' => '33287*100*prescription*1,32457*1250*lab*1,32459*1500*lab*1,32460*400*lab*1,32461*600*lab*1,32468*950*lab*1,32470*950*lab*1,33291*2500*lab*1,33292*2500*lab*1,47516*2000*lab*1,49209*0*lab*1,56835*2400*lab*1,56836*2400*lab*1',
    'patient' => '28370',
    'date' => 1643030497,
    'ref' => '371',
);

// Create array of objects
$reporting_data_as_objects[] = (object)$reporting_data;

$results = [];
foreach ($reporting_data_as_objects as &$obj) {

    // Setup base data that is shared across all items
    $obj->reception_data_sum = 0;
    $obj->references_data_sum = 0;
    $obj->actual_price = 0;

    $category_names = explode(',', $obj->category_name);

    // Loop over the comma-delimited parts of category_name
    foreach ($category_names as $category_name) {

        // Clone our template object
        $tmp = clone $obj;

        // The second item of the asterisk-delimted field is the price
        // We used $_ to indicate that we aren't interested in the first item.
        list($_, $sale_value) = explode('*', $category_name);

        // Set object-specific fields on our clone
        $tmp->category_name = $category_name;
        $tmp->actual_price = (int)$sale_value;

        // Add the clone to the array
        $results[] = $tmp;
    }
}

// Always unset by-ref variables of a foreach
unset($obj);

print_r($results);

Demo here: https://3v4l.org/95KAQ

Sign up to request clarification or add additional context in comments.

3 Comments

hass it's working thanks
hass i am still getting an error the $sale_key cannot be sacrificed with $_ because i am calling model function s with $sale_key i should keep on same structure
Okay, you can just use it like you had. This was just a sample.

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.