Jump to content

Array H*ll


Serenitee

Recommended Posts

I know someday I will hit my lightbulb moment and it will all make sense - until then I'm hoping for pointers either in how to, or links or proper search terms:

 

I have 2 arrays, as example:

Array1
(
    [0] => Array
        (
            [0] => Five Dollars
            [1] => Five Dollars
            [2] => Ten Dollars
        )

    [1] => Array
        (
            [0] => Blue
            [1] => Purple
            [2] => Red
        )

    [2] => Array
        (
            [0] => Socks
            [1] => Gloves
            [2] => Scarf

        )

)
1

 

and

 

Array2
(
    [0] => Array
        (
            [0] => Gloves
            [1] => Socks
            [2] => Scarf
            [3] => Shawl
         )

    [1] => Array
        (
            [0] => Merino
            [1] => Cotton
            [2] => 
            [3] => Alpaca
        )

    [2] => Array
        (
            [0] => Aunt Sue
            [1] => Katie
            [2] => Gramma
            [3] => Sarah
        )

    [3] => Array
        (
            [0] => Fingerless
            [1] => Striped
            [2] => Cabled
            [3] => Lace

        )

)
1

 

I would like to compare these arrays, array1[2] and array2[0].  With these 2 arrays, I would like to create a 3rd.  When a match is found, I would like the new array to have the comparison feature as '0' and grab each line of the other arrays as well.  If no match is found - just add the info from the array it is found in.

 

Confusing?  I'd like the end output to look like this:

 

Array3
(
    [0] => Array
        (
            [0] => Gloves
            [1] => Socks
            [2] => Scarf
            [3] => Shawl
         )

    [1] => Array
        (
            [0] => Merino
            [1] => Cotton
            [2] => 
            [3] => Alpaca
        )

    [2] => Array
        (
            [0] => Aunt Sue
            [1] => Katie
            [2] => Gramma
            [3] => Sarah
        )

    [3] => Array
        (
            [0] => Fingerless
            [1] => Striped
            [2] => Cabled
            [3] => Lace

        )
    [4] => Array
        (
            [0] => Five Dollars
            [1] => Five Dollars
            [2] => Ten Dollars
            [3] => 
        )

    [5] => Array
        (
            [0] => Blue
            [1] => Purple
            [2] => Red
            [3] => 
        )


)
1

 

Noting that the order wouldn't always match - but the values are correct (ie:  Fingerless Gloves for aunt sue cost $5 using blue merino - the colors/type (fingerless), etc didn't get mixed up).

 

Any that can light the way?  I have looked through various array functions on php.net ( array_intersect_assoc, array_diff, array_diff_assoc, array_merge_recursive, the list goes on) to no avail they either seem 'not quite right' or 'definitely not right'.

 

tia!

Link to comment
Share on other sites

try to explain more, do u want to compare those arrays on what condition ? values in the database or what

 

coz the socks as a string has nothing to do with five dollars, if you want to merge them, you can do it with a loop

make an empty array and foreach through both arrays so it inserts key and value into the new array

Link to comment
Share on other sites

Array1 would read:

Cost to make, color of item, Item

 

Array2 would read:

Item made of material for whom with what feature

 

The only thing they have the same would be the Item - in this example, gloves would always be blue fingerless gloves - I am just hoping to come up with array3 that puts all of the information from both arrays together when there is a match - and when there is no match, still add the item to the array but leave the other attributes blank.

 

So, they'd be compared based on item against each other.  Make more sense or am I still having clarity issues?

 

tia!

 

Link to comment
Share on other sites

array_intersect does not look to be what I need as it ignores any that do not match (any that do not match I would also like added to the 3rd array).  But on this page:

 

http://php.net/array_intersect

 

Under 'user contributed' is where I see a hobbled together 'union' which looks to perhaps be on the right path for my needs - though perhaps not 'quite' right.

 

Yes, I am struggling here lol :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.