简体   繁体   中英

Foreach Loop and if statement using PHP

I want to edit a JSON object, so made method called alterJSON the function find the specific JSON array from the database and then change the key-values based on the parameters was given so my question is as you can see in this foreach loop there is an (if statement)

foreach( $decoded as $index => $product ){
                    print_r('<pre>');
                    if( $product->$key == $item) {
                        echo ('found');
                        $decoded[ $index ]->$key = $value;
                        $json = json_encode( $decoded );
                        
                        $stmt->execute([
                            ':json' =>  $json,
                            ':id'   =>  $bid
                        ]);
                    }                   
                }

the loop will print found and make some changes on the $JSON by MySQL query and that's perfectly working, but it will only find the JSON object or item even if there are more then one item in the same key-value so, I want to make another condition in the statement that checks a unique key-value called id so how can I accomplish that? I made some attempts and failed.

what I have tried

<?php
public function AlterJSON( $bid=false, $item=false, $key=false, $Product, $id, $value=false ){
        try{
            if( $bid && $item && $key && $value && $Product && $this->connected === true ){
                
                $stmt=$this->connection->prepare('select `items` from `bills` where `id`=:id');
                $stmt->execute( [ ':id' => $bid ] );
                
                $fetched = $stmt->fetchColumn();
                $decoded = json_decode( $fetched );
                
                $stmt=$this->connection->prepare('update `bills` set `items`=:json where id=:id');
                
                
                foreach( $decoded as $index => $product ){
                    print_r('<pre>');
                    if( $product->$key == $item  && $product->$id == $Product) {
                        echo ('found');
                        $decoded[ $index ]->$key = $value;
                        $json = json_encode( $decoded );
                        
                        $stmt->execute([
                            ':json' =>  $json,
                            ':id'   =>  $bid
                        ]);
                    }                   
                }
                return true;
            }
            return false;
        } catch( PDOException $e ){
            return $this->errors === true ? $this->error( $e->getMessage() ) : false;
        }
    }
?>

function call:-

    $call =  $dbConnection->AlterJSON( $BillID, $CurrentPrice,'price', $ProductID,'id', $ProductPrice );

Have you tried to simply put the $stmt=$this->connection->prepare into the foreach loop?

Just be sure, the function AlterJSON have a variable $Product but when you call this AlterJSON you use $ProductID , can you use $ProductID for both two variables? The PHP is case sensitive for variables. So it's good, but I for the readable it's better.

And other trick in if( $product->$key if $key not exist in $product will produce an error. Yeah, you have a try catch, but if the replacement is in N loop you will lose the possibility of this update. I would install protection before the $product->$key to just be sure the key exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM