Jump to content

[SOLVED] Arrays are making me cry :(


Serenitee

Recommended Posts

I keep waiting for the lightbulb (sledgehammer) moment, and it hasn't occurred no matter how many tutorials I read - so once again turning here.

 

I have this array:

 

Array
(
    [Arms] => Array
        (
            [97] => 2
            [96] => 3
            [98] => 2
            [99] => 1
        )

    [Pants] => Array
        (
            [97] => 1
            [99] => 1
        )

    [Gloves] => Array
        (
            [97] => 1
            [96] => 1
            [99] => 1
        )

    [boots] => Array
        (
            [99] => 1
        )

)

 

And would like to transform it into this:

Array
(
    [Arms] => 8
    [Pants] => 2
    [Gloves] => 3
    [boots] => 1
)

If my reading has garnered me any understanding, I am hoping to turn a multidimensional array into an associative (I'm still trying to learn what everything is called, so confirmation or correction would be grand), as well as add the values.  I'm quite sure this is a very easy matter that I'm just not "getting" and would appreciate any assistance or pointers to clear tutorials.

 

tia!

-Ser

Link to comment
Share on other sites

<?php

function sum_array ($array)
{
$result = array();

foreach($array as $key => $val) //Sum each sub-array and put it into $result
{
	//Do the sum
	$sum = 0;
	foreach($val as $num)
		$sum += $num;
	//Enter the sum into $result
	$result[$key] = $sum;
}

return $result;
}

?>

 

 

And you were right about the terms ;)

 

Orio.

Link to comment
Share on other sites

The only thing that defines an associative array is one in which the key has some meaning -- that is, it is is some way associated with the value. You would therefore wish to maintain this association throughout all array operations (sorting etc).

 

This is as opposed to an array in which the keys have no specific meaning beyond being used to access the values.

 

A multidimensional array is one in which one or more values of the array are arrays themsleves.

 

So yeah, in essence, you were correct. But I thought i'd provide some clarification.

Link to comment
Share on other sites

Here's a shorter solution:

<?php
$start = Array ( 'Arms' => Array ( 97 => 2, 96 => 3, 98 => 2, 99 => 1), 'Pants' => Array ( 97 => 1, 99 => 1),
    'Gloves' => Array ( 97 => 1, 96 => 1, 99 => 1), 'Boots' => Array ( 99 => 1));
$end = array();
foreach ($start as $k => $v)
        $end[$k] = array_sum($v);
print_r($end);
?>

 

Ken

Link to comment
Share on other sites

Thank you both - it's nice to know the reading I have done has sunk in some :)

 

The coding is not providing intended results, but I'm quite sure it's an edit I've done so will post the changes I made in hopes of a point to my newb error.

 

$finCountArr = $QuaItem;//Copied actual array so I don't mess up other tasks
//checked copy - all was well
//echo '<pre>';
//echo print_r($finCountArr);
//echo '</pre>';

function sum_array ($finCountArr)
{
$result = array();

foreach($finCountArr as $key => $val) //Sum each sub-array and put it into $result
{
	//Do the sum
	$sum = 0;
	foreach($val as $num)
		$sum += $num;
	//Enter the sum into $result
	$result[$key] = $sum;
}

return $result;
}

 

All I've edited is "$array" to "$finCountArr" in both instances, providing its 'real' name, which seems logical - but perhaps that has created an issue.  When I print_r($result) it returns:

 

1

 

tia,

-Ser

 

Edit:  while I was posting, Ken added in some coding as well - which worked like a charm, thank you!  I am still curious how I newbed up Orio's code though if any see what I may have killed it with.

 

As usual, ya'all rock, thank you for taking the time :)

 

Link to comment
Share on other sites

I think you misread Orio -- you're code wasn't working, but only because the function was never called. Ken's code worked (it wasn't a function).

 

You need to call the the function like so:

 

function sum_array ($finCountArr)
{
$result = array();

foreach($finCountArr as $key => $val) //Sum each sub-array and put it into $result
{
	//Do the sum
	$sum = 0;
	foreach($val as $num)
		$sum += $num;
	//Enter the sum into $result
	$result[$key] = $sum;
}

return $result;
}
sum_array($finCountArr);

 

The reason why print_r() was echoing a 1 was just that -- you were echoing it, but it already does that for you in the function. You can either do this:

 

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

 

Or, you can return the output of print_r by setting the second paramenter to true, and then echo it:

 

echo '<pre>'.print_r($finCountArr,1).'</pre>';

Link to comment
Share on other sites

Do I understand properly that I was missing the final "sum_array($finCountArr);" and that's how I broke it?

 

If I do not understand properly.. I seem to be missing what you were drawing my attention to GingerRobot.

 

Now I have a lovely new error, that when the coding is by itself works perfectly, but when put in the rest of my script is returning errors - still researching, but will likely be posting it later as all I'm finding is the actual error in my searches, not what might be causing it.  That, however, is for a new post if I can not find the answer :)

 

tia,

-Ser

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.