Hey Peeps,
this has puzzled me to some degree and I wish to seek advice from those higher up.
Lets say I have a foreach loop running on a 2d array ie:
$a[0]["cat"];
$a[0]["name"];
$a[1]["cat"];
$a[1]["name"];
and so on...
how would I go about making one of the array variables(for example: cat) only echo when it was diffrent from the previous one echoed?I know how to do this in ASP but I've been unsuccessful in doing it in PHP (there was a thread a while back,they were doing an A - Z list and only wanted to echo the letter if it was diffrent).
hopefully I explained well enough,would love some help with this.
Any thoughts appriciated....Alright, here goes...
It's very hard for me to know in what context you use this, what your code looks like, etcetera, so I've interpreted it in my own way.. It's probably not exactly what you need, but according to your description, which I've read very throughly, I think the following code will at least give you some ideas about how to solve your specific problem.
<?php
$a[0]['cat'] = 'Mitsy';
$a[0]['owner'] = 'Arnold Smith';
$a[1]['cat'] = 'Mitsy';
$a[1]['owner'] = 'Herbert Love';
$a[2]['cat'] = 'Fritz';
$a[2]['owner'] = 'Herbert Love';
$a[3]['cat'] = 'Mitsy';
$a[3]['owner'] = 'Herbert Love';
$a[4]['cat'] = 'Mitsy';
$a[4]['owner'] = 'Rupert Hesse';
function echoDifferentFromLastEcho($a, $selectedKey) {
// Loop through the first dimension of the array (NUM ASSOC)
for ($i = 0; $i < count($a); $i++) {
$b = $a[$i];
// loop through the second dimension of the array
foreach ($b as $key => $value) {
// If the current $key is the selected one, and it's not equal
// to the previously echoed one, echo it and save its value
if ($key == $selectedKey && @ $lastEcho != $value) {
echo "Array[$i]['$selectedKey'] = $value<br />\n";
$lastEcho = $value;
}
}
}
}
echoDifferentFromLastEcho($a, 'cat');
?>
This function allows you to send an array and the key you're interested in (for example 'cat'), as arguments, and it will only echo the value of 'cat', if it's different from the previous one echoed (if any).
I don't work an awful lot with arrays like this, but this is my take on it. I'm sure there are many ways to do this, and probably better ways too good job Rydberg but what is the @ sign doing in here
if ($key == $selectedKey && @ $lastEcho != $value) {
but the main point Will is that you need to copy the variable to another variable to compare to. if they are different then you do what you need to do.The '@' in there will suppress the notice that PHP -might- send about the variable not being defined, the first time the loop is ran. I am actually uncertain if the '@' should be used like that in statements, but I have done it a few times, and it works perfectly.although it didn't error out I don't think it works if it is not with the variable.
@ $lastEcho
should be
@$lastEcho
but I can't tell as I turn off notices.
but why surpress them if you want the notices on anyway? Oh it works just fine like I have it.. Normally it will suppress any errors on an entire line, but that does not work on control structures like 'if', so instead it will apply on statement by statement inside the if condition.
Well, notices help to write better code, but in this case I couldn't quite figure out how to get rid of it in a nice way, so I took the easy way out Normally it's done by using isset($var) before checking the value of $var.
Anyway, it can also be solved by adding
$lastEcho = NULL;
As the first line of the function. Another way is a rather silly construct with isset():
foreach ($b as $key => $value) {
// If the current $key is the selected one, and it's not equal
// to the previously echoed one, echo it and save its value
if ($key == $selectedKey) {
if (isset($lastEcho)) {
if ($lastEcho != $value) {
echo "Array[$i]['$selectedKey'] = $value<br />\n";
$lastEcho = $value;
}
} else {
echo "Array[$i]['$selectedKey'] = $value<br />\n";
$lastEcho = $value;
}
}
}
LOL.. Well, enough already, I say, just keep the '@' there Having notices enabled and trying to avoid them is just a principle of mine.. But no need to exaggerate now, since it wasn't even the topic of this thread ^^ thanks for the ideas guys.I had a quick look at this thread this morning but I was in keyboarding class before lunch so I had a total of 3 seconds to read it so I just finished reading it fully.Yeah,you guys kicked my brain into gear I believe I can work with it to do exactly as I need...I'll be sure to keep you guys posted.
Thanks Again ^^
this has puzzled me to some degree and I wish to seek advice from those higher up.
Lets say I have a foreach loop running on a 2d array ie:
$a[0]["cat"];
$a[0]["name"];
$a[1]["cat"];
$a[1]["name"];
and so on...
how would I go about making one of the array variables(for example: cat) only echo when it was diffrent from the previous one echoed?I know how to do this in ASP but I've been unsuccessful in doing it in PHP (there was a thread a while back,they were doing an A - Z list and only wanted to echo the letter if it was diffrent).
hopefully I explained well enough,would love some help with this.
Any thoughts appriciated....Alright, here goes...
It's very hard for me to know in what context you use this, what your code looks like, etcetera, so I've interpreted it in my own way.. It's probably not exactly what you need, but according to your description, which I've read very throughly, I think the following code will at least give you some ideas about how to solve your specific problem.
<?php
$a[0]['cat'] = 'Mitsy';
$a[0]['owner'] = 'Arnold Smith';
$a[1]['cat'] = 'Mitsy';
$a[1]['owner'] = 'Herbert Love';
$a[2]['cat'] = 'Fritz';
$a[2]['owner'] = 'Herbert Love';
$a[3]['cat'] = 'Mitsy';
$a[3]['owner'] = 'Herbert Love';
$a[4]['cat'] = 'Mitsy';
$a[4]['owner'] = 'Rupert Hesse';
function echoDifferentFromLastEcho($a, $selectedKey) {
// Loop through the first dimension of the array (NUM ASSOC)
for ($i = 0; $i < count($a); $i++) {
$b = $a[$i];
// loop through the second dimension of the array
foreach ($b as $key => $value) {
// If the current $key is the selected one, and it's not equal
// to the previously echoed one, echo it and save its value
if ($key == $selectedKey && @ $lastEcho != $value) {
echo "Array[$i]['$selectedKey'] = $value<br />\n";
$lastEcho = $value;
}
}
}
}
echoDifferentFromLastEcho($a, 'cat');
?>
This function allows you to send an array and the key you're interested in (for example 'cat'), as arguments, and it will only echo the value of 'cat', if it's different from the previous one echoed (if any).
I don't work an awful lot with arrays like this, but this is my take on it. I'm sure there are many ways to do this, and probably better ways too good job Rydberg but what is the @ sign doing in here
if ($key == $selectedKey && @ $lastEcho != $value) {
but the main point Will is that you need to copy the variable to another variable to compare to. if they are different then you do what you need to do.The '@' in there will suppress the notice that PHP -might- send about the variable not being defined, the first time the loop is ran. I am actually uncertain if the '@' should be used like that in statements, but I have done it a few times, and it works perfectly.although it didn't error out I don't think it works if it is not with the variable.
@ $lastEcho
should be
@$lastEcho
but I can't tell as I turn off notices.
but why surpress them if you want the notices on anyway? Oh it works just fine like I have it.. Normally it will suppress any errors on an entire line, but that does not work on control structures like 'if', so instead it will apply on statement by statement inside the if condition.
Well, notices help to write better code, but in this case I couldn't quite figure out how to get rid of it in a nice way, so I took the easy way out Normally it's done by using isset($var) before checking the value of $var.
Anyway, it can also be solved by adding
$lastEcho = NULL;
As the first line of the function. Another way is a rather silly construct with isset():
foreach ($b as $key => $value) {
// If the current $key is the selected one, and it's not equal
// to the previously echoed one, echo it and save its value
if ($key == $selectedKey) {
if (isset($lastEcho)) {
if ($lastEcho != $value) {
echo "Array[$i]['$selectedKey'] = $value<br />\n";
$lastEcho = $value;
}
} else {
echo "Array[$i]['$selectedKey'] = $value<br />\n";
$lastEcho = $value;
}
}
}
LOL.. Well, enough already, I say, just keep the '@' there Having notices enabled and trying to avoid them is just a principle of mine.. But no need to exaggerate now, since it wasn't even the topic of this thread ^^ thanks for the ideas guys.I had a quick look at this thread this morning but I was in keyboarding class before lunch so I had a total of 3 seconds to read it so I just finished reading it fully.Yeah,you guys kicked my brain into gear I believe I can work with it to do exactly as I need...I'll be sure to keep you guys posted.
Thanks Again ^^