tags:

views:

76

answers:

3

Hi,

I am not sure how to do this, but if it can be done can anyone please help me in this. Basically I have 3 columns in my table:

ID Set   Result Case  
1  Set1  PASS   101  
2  Set2  FAIL   102  
3  Set2  FAIL   101  
4  Set1  FAIL   101  
5  Set1  PASS   104  

$set =  $row['Set'];

What I am trying to achieve is , foreach of these Set, store its associated Result and Case in an array.

+2  A: 
$arr = array();

foreach ($records as $key => $value)
{
    $arr[$key]['Result'] = $value['Result'];
    $arr[$key]['Case'] = $value['Case'];
}

echo '<pre>';
print_r($arr);
echo '</pre>';

In light of your comment:

foreach ($records as $key => $value)
{
    $arr[$key][$value['Result']] = $value['Case'];
}

In light of your most recent comment:

foreach ($records as $key => $value)
{
    $arr[$value['Set']][$value['Result']] = $value['Case'];
}
Alix Axel
But is it a 2 dimentional aray, I thought I would require a 3 dimentional array.[Set][Result][Case] ?
JPro
@JPro: I've edited it, check it again.
Alix Axel
This has nothing to do with his table, and $arr is almost identical to $set in the end. If this _is_ what he's looking for I think it would be a little clearer to instead unset() those array elements he doesn't want ('Set' and 'ID')
LeguRi
What is "required" is the wrong way to think about it. We don't know the hierarchy of your data, we only see it in it's relational form. *YOU* choose what it's going to look like when you structure it into an array. Want it in a 2D array? Do that! Want it in a 3D array? Do that!
Peter Bailey
@Richard JP Le Guen: It's not identical, check his comment.
Alix Axel
Why the down-vote? The question is a little ambiguous but it seems to do what the asker is asking.
Alix Axel
I am looking for an array that looks like this : `Set1 PASS 101``Set1 FAIL 101``Set2 FAIL 102``Set2 fail 101`
JPro
@JPro: Check again, you would benefit from being explicit about your question in the first time.
Alix Axel
@Alix Axel I didn't down vote but would if I had the points, as your third answer will cause him/her to lose data (see my answer).
sequoia mcdowell
@sequoia mcdowell: Potentially, but I just provided the solution he wanted, I'm not making assumptions about his data - that's up to him to decide.
Alix Axel
+2  A: 

After reading comments I thought I'd take a crack at it.

First of all: What you asking for will not work unless you are going to check for duplicate {result} keys in $array[Set2][{result}] and lowercase them if there are duplicates as in your comment, which I don't know why you'd do. It would be confusing and strikes me as nonsensical. To wit:

$arr[Set2][FAIL] vs. $arr[Set2][fail]

If you do it as shown above [in Alix Axel's third code block], you'll do:

$arr[Set2][FAIL] = 102 then overwrite that array index value with $arr[Set2][FAIL] = 101, causing you to lose data.

To put it another way, you are using a combination of the "set" and "result" as a "combined key" so to speak, which you CANNOT DO as the combinations are not unique (Set2 FAIL, Set2 FAIL). I know it's an annoying answer, but you should take a look at what you are doing and why, as I have a hunch you are going about it the wrong way. You probably want an array like:

Array
(  
    [Set1] => Array
    (  
            [101] => 'FAIL'  
            [102] => 'PASS'  
    )

    [Set2] => Array
    (  
            [101] => 'FAIL'  
            [102] => 'FAIL'  
    )
)

or something, but even then it won't work as you have some Set/Case pairs both passing and failing. Because of this, the only thing you can do here is use the "id" as an index:

Array
(  
    [1] => Array
    (  
            [Set] => 'Set1'  
            [Result] => 'PASS'  
            [Case] => '101'  
    )
    [2] => Array
    (  
            [Set] => 'Set1'  
            [Result] => 'FAIL'  
            [Case] => '101'  
    )
)

But I can't even tell you how to do that, cuz you haven't told us how your query results array is structured in the first place!! So step 1) Please print_r or var_dump the query results.

sequoia mcdowell
+1 for the alternative approach. Hope you don't downvote me now! :P
Alix Axel
+1  A: 
// assuming
$myArray = array();
$result = mysql_query("SELECT * FROM table ORDER BY Set ASC");
while ($rows = mysql_fetch_assoc($result)) {
  for ($i = 0; $i <= count($rows); $i++) {
    $myArray[$rows['ID']]['Set'] = $rows['Set'];
    $myArray[$rows['ID']]['Result'] = $rows['Result'];
    $myArray[$rows['ID']]['Case'] = $rows['Case'];
}

// output
Array
(
    [1] => Array
    (
        [Set] => 'Set1'
        [Result] => 'PASS'
        [Case] => '101'
    )
    [2] => Array
    (
        [Set] => 'Set1'
        [Result] => 'FAIL'
        [Case] => '101'
    )
    [3] => Array
    (
        [Set] => 'Set1'
        [Result] => 'PASS'
        [Case] => '104'
    )
    [4] => Array
    (
        [Set] => 'Set2'
        [Result] => 'FAIL'
        [Case] => '102'
    )
    [5] => Array
    (
        [Set] => 'Set2'
        [Result] => 'FAIL'
        [Case] => '101'
    )
)
Spyder