Jump to content

Recommended Posts

Hello

 

I am having some trouble with array_keys

 

I have an array for which I want to return all the keys based on value X

 

This is my code:

 


$depth = array_keys(X', $Array);

echo print_r ($depth,1);


 

I have checked the content of my array $Array and it contains at least 1 instance of 'X' but I am getting nothing when I do the print_r

 

Any suggestions would be appreciated

 

Thanks

 

 

 

 

Doh!

 

I thought I checked that!

 

I have another question if you could help...

 

I have these two arrays:

 



Array1

Array
(
    [Arctictrek's Indiana] => Array
        (
            [0] => 6
        )

    [storm Kloud's Chosen to Win] => Array
        (
            [0] => 5
        )

    [storm Kloud's Keep the Win] => Array
        (
            [0] => 11
        )

    [storm Kloud's Hharmony] => Array
        (
            [0] => 12
        )

    [Fire 'N Ice In Conclusion] => Array
        (
            [0] => 29
        )

    [Jacbar Alaskan Black Night] => Array
        (
            [0] => 13
        )

    [Rameslyn Cherokee Squaw] => Array
        (
            [0] => 14
        )

    [Highnoons Kaskinampo of Jacbar] => Array
        (
            [0] => 27
        )

    [Malnorska's Gypsy Lady] => Array
        (
            [0] => 28
        )

    [Highnoons Loucheuse] => Array
        (
            [0] => 30
        )

)


Array 2

Array
(
    [Arctictrek's Indiana] => Array
        (
            [0] => 6
        )

    [storm Kloud's Chosen to Win] => Array
        (
            [0] => 11
        )

    [storm Kloud's Keep the Win] => Array
        (
            [0] => 23
        )

    [storm Kloud's Hharmony] => Array
        (
            [0] => 24
        )

    [Fire 'N Ice In Conclusion] => Array
        (
            [0] => 25
            [1] => 29
        )

    [Jacbar Alaskan Black Night] => Array
        (
            [0] => 13
        )

    [Rameslyn Cherokee Squaw] => Array
        (
            [0] => 14
        )

    [Highnoons Kaskinampo of Jacbar] => Array
        (
            [0] => 27
        )

    [Malnorska's Gypsy Lady] => Array
        (
            [0] => 28
        )

    [Highnoons Loucheuse] => Array
        (
            [0] => 30
        )

)


 

For each named element in Array 1 I want to add the value from Array 2, so for example, adding Highnoons Loucheuse in Array 1 to Highnoons Loucheuse in Array 2 would give me 30+30, equalling 60....

 

But, Fire 'N Ice In Conclusion has one entry in Array 1 and two in Array 2, and when this happens I want to add the value in Array 1 to the first value in Array 2, giving me one answer, and then add the value in Array 1 to the second value in Array 2, giving me an additional answer.....

 

I need to do this both ways round, where Array 1 could also have multiple entries for the same array....

 

Any suggestions?

 

Thanks

 

 

 

 

This is the code for 1 way where array 1 has 1 value and array 2 has more than 1. See if you can amment to do both ways.

 

<?php
$a1 = array('Arctictrek\'s Indiana' => array(6),
		'Storm Kloud\'s Chosen to Win' => array(5),
		'Storm Kloud\'s Keep the Win' => array(11),
		'Storm Kloud\'s Hharmony' => array(12),
		'Fire \'N Ice In Conclusion' => array(29),
		'Jacbar Alaskan Black Night' => array(13),
		'Rameslyn Cherokee Squaw' => array(14),
		'Highnoons Kaskinampo of Jacbar' => array(27),
		'Malnorska\'s Gypsy Lady' => array(28),
		'Highnoons Loucheuse' => array(30));

$a2 = array('Arctictrek\'s Indiana' => array(6),
		'Storm Kloud\'s Chosen to Win' => array(11),
		'Storm Kloud\'s Keep the Win' => array(23),
		'Storm Kloud\'s Hharmony' => array(24),
		'Fire \'N Ice In Conclusion' => array(25,29),
		'Jacbar Alaskan Black Night' => array(13),
		'Rameslyn Cherokee Squaw' => array(14),
		'Highnoons Kaskinampo of Jacbar' => array(27),
		'Malnorska\'s Gypsy Lady' => array(28),
		'Highnoons Loucheuse' => array(30));

// make sure both arrays have the same num elements
if(count($a1) == count($a2)) {
for($x = 0; $x < count($a1); $x++) {
	$key = key($a1);
	print $key."<br />";
	$start_val = $a1[$key][0];
	print "Start Value: ".$start_val."<br />";
	for($i = 0; $i < count($a2[$key]); $i++) {
		print "Sum: ".$start_val."+".$a2[$key][$i]."<br />";
		$equals = $start_val+$a2[$key][$i];
		print "Equals: ".$equals."<br />";
	}
	print "<br />";
	next($a1);
}
}
?>

 

Will produce

 

Arctictrek's Indiana
Start Value: 6
Sum: 6+6
Equals: 12

Storm Kloud's Chosen to Win
Start Value: 5
Sum: 5+11
Equals: 16

Storm Kloud's Keep the Win
Start Value: 11
Sum: 11+23
Equals: 34

Storm Kloud's Hharmony
Start Value: 12
Sum: 12+24
Equals: 36

Fire 'N Ice In Conclusion
Start Value: 29
Sum: 29+25
Equals: 54
Sum: 29+29
Equals: 58

Jacbar Alaskan Black Night
Start Value: 13
Sum: 13+13
Equals: 26

Rameslyn Cherokee Squaw
Start Value: 14
Sum: 14+14
Equals: 28

Highnoons Kaskinampo of Jacbar
Start Value: 27
Sum: 27+27
Equals: 54

Malnorska's Gypsy Lady
Start Value: 28
Sum: 28+28
Equals: 56

Highnoons Loucheuse
Start Value: 30
Sum: 30+30
Equals: 60

OK, I got this working... but am still having an issue....

 

Before the code you posted I do some work on some of the array values, which are then stored back in the array in the same place.. works fine and when I do a print_r of the array it looks identical in format to the original....

 

However, there is a line in the code you posted:

 



 $key = key($a1);


 

 

And this no longer works....

 

If I remove my code before yours, it works fine, if not, it does not return a value..

 

Any ideas why?

 

Here is my code and yours together (plus my change to make it work with multiple values either side)

 

 


// CHANGE ID TO DEPTH DAM


for ($i = 0 ; $i < count ($damdepth) ; $i++){

	$key = key($damdepth);

	$count = count ($damdepth[$key]);

	for ($y = 0 ; $y < $count ; $y++){

	$position =  ($damdepth[$key][$y]);

	$realdepth = getDepth($position);
	$damdepth[$key][$y] = $realdepth;
	}

	//echo $count;
	next($damdepth);
}

// CHANGE ID TO DEPTH SIRE


for ($i = 0 ; $i < count ($siredepth) ; $i++){

	$key = key($siredepth);

	$count = count ($siredepth[$key]);

	for ($y = 0 ; $y < $count ; $y++){

	$position =  ($siredepth[$key][$y]);

	$realdepth = getDepth($position);
	$siredepth[$key][$y] = $realdepth;
	}

	//echo $count;
	next($siredepth);
}





if(count($siredepth) == count($damdepth)) {	


for($x = 0; $x < count($siredepth); $x++) {		

 $key = key($siredepth);		

echo $key;	

for($y = 0; $y < count($siredepth[$key]); $y++) {		


 print $key."<br />";		
 $start_val = $siredepth[$key][$y];		

 print "Start Value: ".$start_val."<br />";		

 for($i = 0; $i < count($damdepth[$key]); $i++) {			

 print "Sum: ".$start_val."+".$damdepth[$key][$i]."<br />";			
 $equals = $start_val+$damdepth[$key][$i];			

 print "Equals: ".$equals."<br />";		

 }		

 print "<br />";		

next ($siredepth[$key]);
}
 next($siredepth);	
 }

 }	


 

Thanks

 

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.