Jump to content

[SOLVED] HELP With an Array and foreach


JSHINER

Recommended Posts

I have the following

 

<?php 
												$APPLIANCES = ARRAY (
												// Breaks up A,C to two separate variables to be used later
												$a => substr($page['details']['APPLIANCES'], 0, 1),
												$b => substr($page['details']['APPLIANCES'], 2, 1)

												);
												// Pulls from a table which defines the Short and Long meanings of AMENITIES
												foreach ($page['references_amenities'] as $z) {

$$z['Short'] = $z['Long'];

												}

//Groups the A,C variables from above with their Long meanings from above. So if $a = C it should display the Long meaning based on the Short being equal to C

foreach ($PPLIANCES as $y) {

												if($y=$z) { echo $z['Long'];}

												} 
												?>

 

 

Where is my error above? It is not working.

 

I first set up JUST the foreach with $z['Short'] and $z['Long'] and it worked - displayed "A - First, "B - Second" and so on.

 

Link to comment
https://forums.phpfreaks.com/topic/49509-solved-help-with-an-array-and-foreach/
Share on other sites

The $APPLIANCES array is taking a value from a database that is stored as "A,B,D,E" or something like that - letters vary.

 

I need to split each letter of that up into its own variable, so I created that ARRAY so $a = A, $b = B, $c = D, etc

 

I then need to cross reference those $a, $b variables with the results of the $page[references_amenities] which each result looks like A - Long Name .... their fields are $z['Short'] - $z['Long']

 

So based on the $APPLIANCES array, I need the $a to match up to the corresponding $z['Short'] and then display the $z['Long'] value for that.

 

I hope that makes sense.

i make this mistake a lot too which is probably why i caught this out :)

 

<?php
if($y=$z) { echo $z['Long'];}
?>

 

 

in the above, you are trying to assign $z to $yz and an assignation will always work...unless you are trying to assign a value to a previously defined constant and therefore, your if condition will ALWAYS be true.

 

you have to use either '==' or '===' depending on your needs.

 

 

what happens in between the if conditions brackets is that the code in there is evaluated (think var_dump()) and the if condition itself receives the boolean result of what is in the brackets.

so, you assigning $z to $y will always return a true to IF since that is a perfectly legal thing to do in PHP.

 

 

 

 

 

btw, you can correct your code by making it like this:

<?php
if($y == $z) { echo $z['Long'];}
?>

Thanks.

 

However, the if($y == $z) echo $z['Long'] is still not working correctly.

 

I am not sure if I am doing this correctly. Here is my current code:

 

<?php 
												$APPLIANCES = ARRAY (

												$a => substr($page['details']['APPLIANCES'], 0, 1),
												$b => substr($page['details']['APPLIANCES'], 2, 1)

												);

												foreach ($page['references_amenities'] as $z) {

												$z['Short'] = $z['Long'];


												}

												foreach ($APPLIANCES as $y) {

												if($y == $z) { echo $z['Long'];}

												} 

												?>

try working backwards then.

 

check the results of $z like this:

<?php
    echo '<pre>' . print_r($z, true) . '</pre>';
?>

 

and if $z does contain what you expect, then go backwards and do the same for $page, etc until you see something that we might find useful to solve this problem and post it here.

 

 

 

 

 

also, you had this earlier:

<?php
$$z['Short'] = $z['Long'];
?>

 

 

but now you have this:

<?php
$z['Short'] = $z['Long'];
?>

 

just pointing it out because your code would have failed earlier anyway.

Using the following:

 

$APPLIANCES = ARRAY (

$a => substr($page['details']['APPLIANCES'], 0, 0),
$b => substr($page['details']['APPLIANCES'], 2, 1)

);

foreach ($APPLIANCES as $y) {

echo '', $y, ' > ';

} 

 

Now when I simply do "echo $page['details']['APPLIANCES']" I get A, C

 

However, when I do that foreach echo '', $y, '>'; It only displays C >

 

What am I missing here?

OK so I changed the ARRAY to:

 

$APPLIANCES = ARRAY (

'$a' => substr($page['details']['APPLIANCES'], 0, 0),
'$b' => substr($page['details']['APPLIANCES'], 2, 1)

);

foreach ($APPLIANCES as $y) {

echo '', $y, ' > ';

} 

 

put ' ' around $a and $b

 

 

I have a function that pulls the values for Short and Long from a database. So for example a Short would be A, Long would be Tiger

 

So I want that if the $a is A, it would display Tiger instead of A

 

I'm thinking of using a foreach to set the values for Short and Long. I tried:

 

foreach ($page['references_amenities'] as $z) {

$z['Short'] = $z['Long'];

}

 

But that does not work.

Example.

 

<?php

  // connect to db
  $sql = "SELECT short,long FROM tbl";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $values = array();
      while($row = mysql_fetch_assoc($result)) {
        $values[$row['short']] = $row['long'];
      }
    }
  }

?>

This is what I currently use to bring the array of Long and Short in. It pulls from the database. Is there anyway I can create a relationship between the two here?

 

foreach ($page['references_amenities'] as $z) {

$z['Short'] = $z['Long'];

}

 

 

Then - how do I make it so if the $a from the $APPLIANCES array = Short, it will display Long ?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.