Welcome! Log In Create A New Profile

Advanced

[PHP] in_array

Posted by Karl DeSaulniers 
Karl DeSaulniers
[PHP] in_array
April 06, 2017 10:40AM
Hello All,
Hope you are all experiencing some progress with yours.
Looking forward to some on mine. Have a quick question.

When traversing an multidimensional array what is the proper way to detect if a value is in one of the indexes of the array?

EG:

Array1 = array(
0 => array("pUser" => "Customer", "pType" => "Offset", "pProduct" => "Business Cards"),
1 => array("pUser" => "Retailer", "pType" => "Offset", "pProduct" => "Flyers"),
2 => array("pUser" => "InHouse", "pType" => "Offset", "pProduct" => "Business Cards")
);

So I have this array and I want to add let's say a customer -> offset -> business cards,
but let's also say that there is only 1 customer is allowed to offset print at a time.

How would I check for pUser, pType to see if one exists already?

I was looking at in_array(), but my brain twisted when the multidimensional came into play.
Not sure how to apply. Any help would be greatly appreciated.

TIA.

Best,

Karl DeSaulniers
Design Drumm
http://designdrumm.com http://designdrumm.com/
Aziz Saleh
Re: [PHP] in_array
April 06, 2017 03:30PM
On Thu, Apr 6, 2017 at 4:30 AM, Karl DeSaulniers <[email protected]>
wrote:

> Hello All,
> Hope you are all experiencing some progress with yours.
> Looking forward to some on mine. Have a quick question.
>
> When traversing an multidimensional array what is the proper way to detect
> if a value is in one of the indexes of the array?
>
> EG:
>
> Array1 = array(
> 0 => array("pUser" => "Customer", "pType" => "Offset", "pProduct"
> => "Business Cards"),
> 1 => array("pUser" => "Retailer", "pType" => "Offset", "pProduct"
> => "Flyers"),
> 2 => array("pUser" => "InHouse", "pType" => "Offset", "pProduct"
> => "Business Cards")
> );
>
> So I have this array and I want to add let's say a customer -> offset ->
> business cards,
> but let's also say that there is only 1 customer is allowed to offset
> print at a time.
>
> How would I check for pUser, pType to see if one exists already?
>
> I was looking at in_array(), but my brain twisted when the
> multidimensional came into play.
> Not sure how to apply. Any help would be greatly appreciated.
>
> TIA.
>
> Best,
>
> Karl DeSaulniers
> Design Drumm
> http://designdrumm.com http://designdrumm.com/
>
>
>
>
>
A few options I can think of:

- Loop through the array and compare to find if it exists.
- Have the index of the array be the md5 of the unique fields and do an
isset before adding.
- Keep a separate md5 array of the added values and do in_array.

Personally I would prefer the custom keys/isset.
Tolga
Re: [PHP] in_array
April 06, 2017 03:50PM
On 06-04-2017 16:21, Aziz Saleh wrote:
> On Thu, Apr 6, 2017 at 4:30 AM, Karl DeSaulniers <[email protected]>
> wrote:
>
>> Hello All,
>> Hope you are all experiencing some progress with yours.
>> Looking forward to some on mine. Have a quick question.
>>
>> When traversing an multidimensional array what is the proper way to detect
>> if a value is in one of the indexes of the array?
>>
>> EG:
>>
>> Array1 = array(
>> 0 => array("pUser" => "Customer", "pType" => "Offset", "pProduct"
>> => "Business Cards"),
>> 1 => array("pUser" => "Retailer", "pType" => "Offset", "pProduct"
>> => "Flyers"),
>> 2 => array("pUser" => "InHouse", "pType" => "Offset", "pProduct"
>> => "Business Cards")
>> );
>>
>> So I have this array and I want to add let's say a customer -> offset ->
>> business cards,
>> but let's also say that there is only 1 customer is allowed to offset
>> print at a time.
>>
>> How would I check for pUser, pType to see if one exists already?
>>
>> I was looking at in_array(), but my brain twisted when the
>> multidimensional came into play.
>> Not sure how to apply. Any help would be greatly appreciated.
>>
>> TIA.
>>
>> Best,
>>
>> Karl DeSaulniers
>> Design Drumm
>> http://designdrumm.com http://designdrumm.com/
>>
>>
>>
>>
>>
> A few options I can think of:
>
> - Loop through the array and compare to find if it exists.
> - Have the index of the array be the md5 of the unique fields and do an
> isset before adding.
> - Keep a separate md5 array of the added values and do in_array.
>
> Personally I would prefer the custom keys/isset.
>
or rather than md5 fields, make it more multidimentional and check with
isset,

Array1=array(
"Offset"=>array(
"Business Cards"=>array(
"Customer"=>1,
"InHouse"=>1
),
"Flyers"=>array(
"Retailer"=>1
)
)
)

if($Array1["Offset"]["Business Cards"]["Customer"]) or
if(isset($Array1["Offset"]["Business Cards"]["Customer"])) will give you
what you want

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login