check single value present in array
Apply in_array after split string using explode
The in_array function in PHP is used to check if a given value exists in an array. It returns true if the value is found in the array, and false otherwise. Here's a simple explanation along with an example:
Syntax:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
$needle: The value to search for.
$haystack: The array to search in.
$strict (optional): If true, the function will also check the types of the values.
// Sample array
$fruits = ['apple', 'banana', 'orange', 'grape'];
// Check if 'banana' is in the array
if (in_array('banana', $fruits)) {
echo 'Found!';
} else {
echo 'Not found!';
}
In this example:
- The $fruits array contains several fruit names.
- The in_array function is used to check if the value 'banana' is present in the array.
- Since 'banana' is in the array, the condition is true, and it will echo 'Found!
You can also use the optional third parameter $strict to enforce strict type checking. If set to true, the function will also check the types of the values. For example:
// Sample array with mixed types
$mixedArray = [1, 'apple', 3.14, 'banana'];
// Check if 3 is in the array (without strict type checking)
if (in_array(3, $mixedArray)) {
echo 'Found!';
} else {
echo 'Not found!';
}
// Check if 3 is in the array (with strict type checking)
if (in_array(3, $mixedArray, true)) {
echo 'Found!';
} else {
echo 'Not found!';
}
In this case, without strict type checking, the first check returns true because the value 3 is loosely equal to the string '3.14'. With strict type checking, the second check returns false because it considers the type as well.
Practical Example
Data received via AJAX:
{"_token":"uJFyM5MacvkbIQOn8Jiic8QOztkm0oKctzHhyW0Z","admin_id":"28","slug":"Amit","influencer_email":"abc@gmail.com","admin_email":"rakeshdev.cotocus@gmail.com","user_name":"rakesh","item":"24","dataitem_id":"24","selected_socials":"face_price,youtube_price"}
Apply in_array after split string using explode
we need two different array as parameter for in_array
convert dynamic request to array using explode
take another array as hardcode or pluck-->toarray from db
then apply in_array
$selectedSocials = explode(',', $request->selected_socials);
$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'];
foreach ($socialSites as $socialSite) {
// Check if the social site is in $selectedSocials
if (in_array($socialSite, $selectedSocials)) {
$addCart = new addcart;
$addCart->admin_id = $admins_id;
$addCart->cart_id = $cartID;
$addCart->influencer_admin_id = $addprofile->user_id;
$addCart->admin_email = $admins_email;
$addCart->user_name = $user_name;
$addCart->slug = $slug;
$addCart->influencer_email = $influencer_email;
$addCart->{$socialSite} = $payments->{$socialSite} ?? 0;
$addCart->save();
}
}
Practical Example
Wap to display button text based on other table field presense
$addcartdata = addcart::pluck('influencer_admin_id')->toarray();
$influencerIds = empty($addcartdata) ? [] : $addcartdata;
log::info($influencerIds);
return view('pages.influencersd',compact('paginator','previousPageUrl','influencerIds','nextPageUrl','filteredData','currentURL'));
In blade file
@if ($valueAfterColon == 'yes' && auth()->user()->email == $parts[0] && in_array($item->user_id, $influencerIds))
<button type="submit"
class="btn btn-sm btn-primary" style="position: relative;float:right">Added</button>
@else
<button id="callmyAjaxButton" class="btn btn-sm btn-primary mr-2" data-id="{{ $item->id }}" style="position: relative;float:right" onclick="mySocialClick(this, event)">Add cart</button>
@endif
<button id="callAjaxButton" class="btn btn-sm btn-primary mr-2" data-id="{{ $item->id }}" style="position: relative;float:right" onclick="myButtonClick(this, event)">View Price</button>
Top comments (0)