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
Log::info('Data received via AJAX:', $request->all());
Data received via AJAX: {"itemId":"157","selectedData":"[\"facebook\",\"youtube\",\"wordpress\",\"instagram\",\"quora\"]"}
Step 2: get single record from database
$socialurl=DB::connection('payments')->table("social_url")->where('id',$itemId)->first();
log::info(json_encode($socialurl));
{"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}"}
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);
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,
)
step4: convert request particular field to associative array to filtering
my request data
Log::info('Data received via AJAX:', $request->all());
Data received via AJAX: {"itemId":"157","selectedData":"[\"facebook\",\"youtube\",\"wordpress\",\"instagram\",\"quora\"]"}
$selectedSocials = json_decode($request->selectedData, true);
Log::info('Selected Social Sites: ' . print_r($selectedSocials, true));
array (
'facebook' => '24',
'youtube' => '78',
'wordpress' => '88',
'instagram' => '78',
'quora' => '55',
)
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));
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];
}
}
output
Log::info($extractedPrices);
Log::info("aayega1");
array (
'facebook' => '24',
'youtube' => '78',
'wordpress' => '88',
'instagram' => '78',
'quora' => '55',
)
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;
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'];
iterate in array
foreach ($socialSites as $socialSite)
remove price from each element of array to to match the key of associative array retrive on step4
$platform = str_replace('_price', '', $socialSite);
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
Top comments (0)