I hate Arrays

i’m trying to get my Array to look like

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

[2] => Array
(
[0] => Small
[1] => Medium
[2] => Large
)

[3] => Array
(
[0] => Whatever

)

Below is what i’m getting

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

    [2] => Array
        (
            [1] => Large
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

    [2] => Array
        (
            [1] => Large
        )

    [3] => Array
        (
            [1] => Medium
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

    [2] => Array
        (
            [1] => Large
        )

    [3] => Array
        (
            [1] => Medium
        )

    [4] => Array
        (
            [2] => Green
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

    [2] => Array
        (
            [1] => Large
        )

    [3] => Array
        (
            [1] => Medium
        )

    [4] => Array
        (
            [2] => Green
        )

    [5] => Array
        (
            [1] => Small
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

    [2] => Array
        (
            [1] => Large
        )

    [3] => Array
        (
            [1] => Medium
        )

    [4] => Array
        (
            [2] => Green
        )

    [5] => Array
        (
            [1] => Small
        )

    [6] => Array
        (
            [2] => Red
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

    [2] => Array
        (
            [1] => Large
        )

    [3] => Array
        (
            [1] => Medium
        )

    [4] => Array
        (
            [2] => Green
        )

    [5] => Array
        (
            [1] => Small
        )

    [6] => Array
        (
            [2] => Red
        )

    [7] => Array
        (
            [1] => Small
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

    [2] => Array
        (
            [1] => Large
        )

    [3] => Array
        (
            [1] => Medium
        )

    [4] => Array
        (
            [2] => Green
        )

    [5] => Array
        (
            [1] => Small
        )

    [6] => Array
        (
            [2] => Red
        )

    [7] => Array
        (
            [1] => Small
        )

    [8] => Array
        (
            [1] => Medium
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

    [2] => Array
        (
            [1] => Large
        )

    [3] => Array
        (
            [1] => Medium
        )

    [4] => Array
        (
            [2] => Green
        )

    [5] => Array
        (
            [1] => Small
        )

    [6] => Array
        (
            [2] => Red
        )

    [7] => Array
        (
            [1] => Small
        )

    [8] => Array
        (
            [1] => Medium
        )

    [9] => Array
        (
            [1] => Large
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

    [2] => Array
        (
            [1] => Large
        )

    [3] => Array
        (
            [1] => Medium
        )

    [4] => Array
        (
            [2] => Green
        )

    [5] => Array
        (
            [1] => Small
        )

    [6] => Array
        (
            [2] => Red
        )

    [7] => Array
        (
            [1] => Small
        )

    [8] => Array
        (
            [1] => Medium
        )

    [9] => Array
        (
            [1] => Large
        )

    [10] => Array
        (
            [1] => Extra Large
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

    [2] => Array
        (
            [1] => Large
        )

    [3] => Array
        (
            [1] => Medium
        )

    [4] => Array
        (
            [2] => Green
        )

    [5] => Array
        (
            [1] => Small
        )

    [6] => Array
        (
            [2] => Red
        )

    [7] => Array
        (
            [1] => Small
        )

    [8] => Array
        (
            [1] => Medium
        )

    [9] => Array
        (
            [1] => Large
        )

    [10] => Array
        (
            [1] => Extra Large
        )

    [11] => Array
        (
            [1] => 2X-Large
        )

)

Array
(
    [0] => Array
        (
            [1] => 3X-Large
        )

    [1] => Array
        (
            [1] => Extra Large
        )

    [2] => Array
        (
            [1] => Large
        )

    [3] => Array
        (
            [1] => Medium
        )

    [4] => Array
        (
            [2] => Green
        )

    [5] => Array
        (
            [1] => Small
        )

    [6] => Array
        (
            [2] => Red
        )

    [7] => Array
        (
            [1] => Small
        )

    [8] => Array
        (
            [1] => Medium
        )

    [9] => Array
        (
            [1] => Large
        )

    [10] => Array
        (
            [1] => Extra Large
        )

    [11] => Array
        (
            [1] => 2X-Large
        )

    [12] => Array
        (
            [1] => 2X-Large
        )

)

this is my PHP script



if (count($product->productInfo))
{
	foreach ($product->productInfo as $thisProductInfoID => $thisInfo)
	{
		//	print_array($thisInfo);
		if (count($thisInfo->attributes))
		{
				//print_array($thisInfo->attributes);
			foreach ($thisInfo->attributes as $thisAttributeID => $thisAttributeInfo)
			{				
				
						$AttributeColors[][$thisAttributeID]=$thisAttributeInfo['genValue']; 
						print_array($AttributeColors); 
		
			//print_array($thisAttributeInfo);
				if (!isset($attributesArr[$thisAttributeID]) || !in_array($thisAttributeInfo['genValue'], $attributesArr[$thisAttributeID]))
					$attributesArr[$thisAttributeID][]=$thisAttributeInfo['genValue'];
							//	print_array($attributesArr[$thisAttributeID]);		
			}
		}
	}
}




array_map could also be useful for you.


function sizeInfo($info) {
    return $info[2];
}
function colorInfo($info) {
        return $info[1];
}
$ArSizes = array_map('sizeInfo', $productInfo);
$ArColors = array_map('colorInfo', $productInfo);

If you can give us some sample data to work with, we can help to advise you on the best ways to work with that data.

I think that it’s the part that’s causing you grief.

If you have the following array:

Array
{
    [Foo] => Bar
}

And you use the following to add more:

$array[][‘Baz’] = ‘Bat’;

You will end up with:

Array
{
    [Foo] => Bar,
    [0] => Array
    {
        [Baz] => Bat
    }
}

The part of the notation automatically appends another numeric index on to the array.

What you instead want to do, is to add the info without the part

$array[‘Baz’] = ‘Bat’;

Which will result in the following array instead:

Array
{
    [Foo] => Bar,
    [Baz] => Bat
}

Agreed, if you can post as small example of “before” and “after”, oh, and don’t format them as the output of var_dump but as arrays others can copy and paste and play with, then you’ll get far more answers, so I have noted anyway.

ie not…


Array
(
    [0] => 3X-Large
    [1] => Extra Large
    [2] => Large 


but…


$d = array (
  0 => "3X-Large" 
, 1 => "Extra Large"
,  2 => "Large" 
);

i got it…
putting up my little script maybe it’ll help someone down the road.



$ArSizes = array(); 
$ArColors = array();

foreach($infoAttributes as $AttributeKey => $AttributeValue) [
      
          if($AttributeKey=='2') { 
                       array_push($ArSizes[]="$AttributeValue"); 
               }

          if($AttributeKey=='1') { 
                       array_push($ArColors[]="$AttributeValue"); 
               }


Result



Array
(
    [0] => Green
    [1] => Green
    [2] => Red
    [3] => Red
    [4] => Red
    [5] => Red
    [6] => Red
    [7] => Red
    [8] => Red
)

Array
(
    [0] => 3X-Large
    [1] => Extra Large
    [2] => Large
    [3] => Medium
    [4] => Medium
    [5] => Small
    [6] => Small
    [7] => Small
    [8] => Medium
    [9] => Large
    [10] => Extra Large
    [11] => 2X-Large
    [12] => 2X-Large
)



then just toss in a little array_unique and bam.

yeah i can strip it all the way down to the Keys and Values but …

but i need them in Array… that’s really the part that frustrating me.

so its like strip them… clean it up and put it back into array.

Is this what you’re after?

array[$string] = $otherString;

saying that how do i add the array ids in automatically

doing won’t
array(“$string” => “$otherStrin”);

or trying
array(
run a foreach script
);