Debug School

rakesh kumar
rakesh kumar

Posted on

How to store price value from nested json field array on filtering in laravel

How to convert nested json format of record to associative array
How to filter from nested json field of single record based on dynamic request

How to remove fix word from each element of array using str_replace

How to filter in associative array based on its key element match using array_key_exists

Step 1: Check data format on server side what kind of data is coming to apply filtering records

in table social_url, social_price field are in nested json format

Image description

 Log::info('Data received via AJAX:', $request->all());
Enter fullscreen mode Exit fullscreen mode
Data received via AJAX: {"itemId":"157","selectedData":"[\"facebook\",\"youtube\",\"wordpress\",\"instagram\",\"quora\"]"}
Enter fullscreen mode Exit fullscreen mode

Step 2: get single record from database

$socialurl=DB::connection('payments')->table("social_url")->where('id',$itemId)->first();
 log::info(json_encode($socialurl));
Enter fullscreen mode Exit fullscreen mode
{"id":157,"user_id":"28","user_name":"admins","user_email":"admins@gmail.com","file_pic":"","slug_id":null,"slug":"admins","slugname":null,"country_id":"101","state_id":"4025","city_id":"58078","mobile":"7488127637","digital_marketer":"faceinfluencer","bio":"dxfbfvb","social_price":"{\"facebook\":\"24\",\"twitter\":null,\"youtube\":\"78\",\"wordpress\":\"88\","instagram\":\"78\",\"quora\":\"55\",\"pinterest\":null,"roposo\":null,\"chingari\":null,\"mitron\":null}"}  


Enter fullscreen mode Exit fullscreen mode

step 3:How to convert nested json format of record to associative array

if ($socialurl) {               
                    $socialurlArray = json_decode(json_encode($socialurl), true);            
                    $socialPriceArray = json_decode($socialurlArray['social_price'], true); 
Enter fullscreen mode Exit fullscreen mode

Log::info($socialPriceArray);

array (
  'facebook' => '24',
  'twitter' => NULL,
  'youtube' => '78',
  'wordpress' => '88',
  'tumblr' => NULL,
  'instagram' => '78',
  'quora' => '55',
  'pinterest' => NULL,
  'reddit' => NULL,
  'koo' => NULL,
  'scoopit' => NULL,
  'slashdot' => NULL,
  'telegram' => NULL,
  'fb_grp' => NULL,
  'linkedin_grp' => NULL,
  'linkedin' => NULL,
  'roposo' => NULL,
  'chingari' => NULL,
  'mitron' => NULL,
)  
Enter fullscreen mode Exit fullscreen mode

step4: convert request particular field to associative array to filtering
my request data

 Log::info('Data received via AJAX:', $request->all());
Enter fullscreen mode Exit fullscreen mode
Data received via AJAX: {"itemId":"157","selectedData":"[\"facebook\",\"youtube\",\"wordpress\",\"instagram\",\"quora\"]"}
Enter fullscreen mode Exit fullscreen mode

$selectedSocials = json_decode($request->selectedData, true);
                    Log::info('Selected Social Sites: ' . print_r($selectedSocials, true));
Enter fullscreen mode Exit fullscreen mode
 array (
  'facebook' => '24',
  'youtube' => '78',
  'wordpress' => '88',
  'instagram' => '78',
  'quora' => '55',
)  
Enter fullscreen mode Exit fullscreen mode
if ($socialurl) {               
                    $socialurlArray = json_decode(json_encode($socialurl), true);            
                    $socialPriceArray = json_decode($socialurlArray['social_price'], true); 
                    $selectedSocials = json_decode($request->selectedData, true);
                    Log::info('Selected Social Sites: ' . print_r($selectedSocials, true));
Enter fullscreen mode Exit fullscreen mode

step5:apply filter from nested single associative array after perform in step3 based on dynamic request associative array after perform step 4

How to filter from nested json field of single record based on dynamic request

$extractedPrices = [];
                    foreach ($selectedSocials as $socialSite) {
                        if (isset($socialPriceArray[$socialSite])) {
                            $extractedPrices[$socialSite] = $socialPriceArray[$socialSite];
                        }
                    }  
Enter fullscreen mode Exit fullscreen mode

output

                Log::info($extractedPrices);
                    Log::info("aayega1");
Enter fullscreen mode Exit fullscreen mode
 array (
  'facebook' => '24',
  'youtube' => '78',
  'wordpress' => '88',
  'instagram' => '78',
  'quora' => '55',
) 
Enter fullscreen mode Exit fullscreen mode

step6 store value of associative array in different field of table

How to remove fix word from each element of array using str_replace

How to filter in associative array based on its key element match using array_key_exists

create modal object

$addCart = new addcart;
Enter fullscreen mode Exit fullscreen mode

create array contain social site if only that element found then store in table

       $socialSites = ['face_price','twitter_price','youtube_price', 'wordpress_price', 'tumblr_price','instagram_price','quora_price', 'pintrest_price','reddit_price','koo_price','scoopit_price','slashdot_price', 'telegram_price','linkedin_price'];
Enter fullscreen mode Exit fullscreen mode

iterate in array

foreach ($socialSites as $socialSite)
Enter fullscreen mode Exit fullscreen mode

remove price from each element of array to to match the key of associative array retrive on step4

$platform = str_replace('_price', '', $socialSite);
Enter fullscreen mode Exit fullscreen mode

match the element of array with key value associative array retrive on step4 and store on different field of table`


if (array_key_exists($platform, $selectedSocials)) {
$priceKey = $socialSite;
log::info("Social Site: $socialSite, Price Key: $priceKey, Price Value: {$selectedSocials[$platform]}");
// Now set the value based on the full $socialSite
$addCart->{$priceKey} = $selectedSocials[$platform] ?? 0;
}

Full Code

Image description

Image description

Top comments (0)