Array's

From what I've read the following code should work.

It's blowing up on the line that's in bold.  I'm sure it's something stupid.

----------------------------------------------------------------------------------------------

$last_bldg_seq = "";
                        
                        foreach ($even_array as $display_array)
                        {
                              
                        // ****************************************************
                        // * If new building change color background          *
                        // * and save building number.                        *
                        // ****************************************************
                              if ($last_bldg_seq == "")
                              {
                                    $last_bldg_seq = $display_array['BNMB'];
                              }
                              if ($last_bldg_seq != $display_array['BNMB'])
                                    {
                                          $count++;
                                          $current_store_count = 0;
                                          if ($bgcolor == "F6F7D7")
                                          {
                                                $bgcolor = "DCF7DC";
                                          }
                                          else
                                          {
                                                $bgcolor = "F6F7D7";
                                          }
                                    }
                              $last_bldg_seq = $display_array['BNMB'];

---------------------------------------------------------------------------------------------
Thanks
Glenn
breeze351Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
breeze351Connect With a Mentor Author Commented:
Ok, check the web site again.  This time look at the "Odds"  All the data should be there.

Sorry that I haven't got to this sooner.  My mother-inlaw has been in out of the hospital for the last 5 days.  I've running around like a nut.  There's nothing you can do because it's your wife's mom.  So if the wife says "You have to do this!" , there's no answer but "Yes dear".

Glenn
0
 
haloexpertsexchangeCommented:
is BNMB exactly what the key is for what you are trying to retrieve?
If it is something like bnmb or Bnmb it would not be found by $display_array['BNMB'].
0
 
Marco GasiFreelancerCommented:
What does exactly mean 'blowing up'?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
breeze351Author Commented:
"BNMB" is the key
Here's the array.
------------------------------------
$even_array = array(
                  "BNMB" => "",
                  "STRT" => "",
                  "STOREKEY" => "",
                  "SEQ" => "",
                  "FRONTAGE" => ""
                  );
-------------------------------------

By blowing up I mean here's the error message:

----------------------------------------------
Notice: Uninitialized string offset: 0 in /home/langsyst/public_html/Walk_Map_Display.php on line 350
-------------------------------------------------

 Line 350 is:
--------------------------------------------------
$last_bldg_seq = $display_array['BNMB'];
------------------------------------------------------
0
 
haloexpertsexchangeCommented:
you are iterating through $even_array  here foreach ($even_array as $display_array)
which means that $display_array will equal what ever is in each array location and will have nothing in $display_array['BNMB'] because in the array you showed us no array location contains another array.
If you want to know what is in the location keyed with BNMB try $even_array['BNMB'] instead or if you still need to iterate through the array try below.

foreach ($even_array as $key=> $display_array)
{
if($key == "BNMB")
{
 if ($last_bldg_seq == "")
                              {
                                    $last_bldg_seq = $display_array;
                              }
}
}

Open in new window

0
 
Marco GasiFreelancerCommented:
IMHO your loop doesn't make sense. Try to replace it with the following

$last_bldg_seq = "";

foreach ($even_array as $key=>$value)
{
	  
// ****************************************************
// * If new building change color background          *
// * and save building number.                        *
// ****************************************************
	if ($key == "BNMB")
	{ 
	  if ($last_bldg_seq == "")
	  {
			$last_bldg_seq = $value;
	  }
	}
	if ($last_bldg_seq != $value)
	{
		$count++;
		$current_store_count = 0;
		if ($bgcolor == "F6F7D7")
		{
			$bgcolor = "DCF7DC";
		}
		else
		{
			$bgcolor = "F6F7D7";
		}
	}
    $last_bldg_seq = $value;
}

Open in new window

0
 
breeze351Author Commented:
Ok, let me get this correct.

1. I'm iterating through "$even_array".
2. If I define "$display_array" with the same variables, the code will work?
0
 
Marco GasiFreelancerCommented:
I'll try to be clear. Your original code would work if the values of $even_array were arrays, taht is if $even_array were an array of arrays:

$even_array = array
(
	[0]=>array(
		"BNMB" => "some1",
		"STRT" => "",
		"STOREKEY" => "",
		"SEQ" => "",
		"FRONTAGE" => ""
	),
	[1]=>array(
		"BNMB" => "some2",
		"STRT" => "",
		"STOREKEY" => "",
		"SEQ" => "",
		"FRONTAGE" => ""
	),
);

Open in new window


In this case your code would work.

Now, you can call your array as you wish, but I don't understand your question "If I define "$display_array" with the same variables, the code will work?" Maybe you can post an example of what you mean.
Sure you can't iterate an array as you did if that array is not an array of array.
Wait for example...
0
 
breeze351Author Commented:
There are more than 1 variables in the array.
I'm confused on this line:
-------------------------------------------------------
foreach ($even_array as $key=>$value)
-------------------------------------------------------
Does $value contain the variable names?
0
 
haloexpertsexchangeCommented:
$key contains the name of the key of the current record of the array that you are looping through.
$value contains the value of the current record of the array that you are looping through.
0
 
Marco GasiFreelancerCommented:
In your case $key holds "BNMB", "STRT", "STOREKEY", "SEQ", "FRONTAGE" whereas $value holds empty strings.
0
 
breeze351Author Commented:
Marquas:

"$even_array" has the data and it does display in a test.  I need to iterate through it in a second part of the code.

If you would like to see what I'm trying to do (I'm not worried about it, it's test data):

Site: langsystems.net
Login: sales
ID: lsi

Click on "Walk By"
Address: 575
Street: lex

I've populated two arrays (left and right) on the top of the page.  Now I'm trying to display them under the header for the crossing street (East 42nd),
0
 
Marco GasiFreelancerCommented:
Suppose this is your array:

$even_array = array(
                  "BNMB" => "BNMB value",
                  "STRT" => "STRT value",
                  "STOREKEY" => "STOREKEY value",
                  "SEQ" => "SEQ value",
                  "FRONTAGE" => "FRONTAGE value"
                  );

Basically, you can iterate an array in three ways:

1) for loop

for ($i = 0; $i < count($even_array); $i++){
    echo $even_array[$i] . "\n";
}

Open in new window


The ouput is:

BNMB value
STRT value
STOREKEY value
SEQ value
FRONTAGE value

2) foreach equivalent
foreach ($evan_array as $value){
    echo $value . "\n";
}

Open in new window


The output is identical:

The ouput is:

BNMB value
STRT value
STOREKEY value
SEQ value
FRONTAGE value

3) foreach associative style

foreach ($evan_array as $key=>$value){
    echo $key . " = " . $value . "\n";
}

Open in new window


The ouput is:

BNMB = BNMB value
STRT = STRT value
STOREKEY = STOREKEY value
SEQ = SEQ value
FRONTAGE = FRONTAGE value

Hope this is clear enough :)
0
 
Marco GasiFreelancerCommented:
Well, I toke a look, but I need the code. I think my explanation should help you to build correclty the page, but if you need a deeper help, post the whole code here, please.
0
 
Dave BaldwinFixer of ProblemsCommented:
$even_array["BNMB"] is the value you are looking for.  The 'foreach' is doing nothing for you.  Unless you have more than one $even_array, you do not need to 'iterate' through it.
0
 
breeze351Author Commented:
Okay.
This code works.

-------------------------------------------
foreach ($even_array as $display_key=>$display_value)
                        {
------------------------------------------

Last stupid question.

I need the value from a single key. What is the syntact?

------------------------------------------------
echo "<td height = ";
echo $display_key['FRONTAGE']['Frontage'];
-------------------------------------------------

Frontage tells me the height of the row.

Thanks
0
 
haloexpertsexchangeCommented:
if $even_array is an array of arrays that match the format that you showed earlier then all you need is $display_value['FRONTAGE'] while in your foreach.
$display_key is the key to the current array record not an array itself.
0
 
Ray PaseurCommented:
In case you are wondering about what a particular PHP function does, you can easily look it up on PHP.net.  This is one of the greatest features of PHP.  I always work with a window open to the online manual.  Example:
http://php.net/foreach

Most of the descriptions include examples and user-contributed notes which are (mostly) correct usages of the functions.  The online man page is required reading before you use any PHP function!

I looked at the site, and this is what I'm seeing on the page.  Can you please tell us what these fields are named and what they mean?

Even_Array_Count = 3
Even_Bldg_Count = 0
Odd_Array_Count = 6
Odd_Bldg_Count = 1

Even Numbers
590^Lexington Avenue^1001^60^Scudder Funds Center^
590^Lexington Avenue^1002^100^345 Park Avenue Back Entrance^
590^Lexington Avenue^1003^40^Le Relais de Venise L'entrecote^
0^200^3^

Odd Numbers
575^Lexington Avenue^1001^20^Duane Reade^
575^Lexington Avenue^1002^20^New York Sports Club (2nd Floor)^
575^Lexington Avenue^1003^20^Building Entrance^
575^Lexington Avenue^1004^60^Camera Land^
591^Lexington Avenue^1001^20^Jean Louis David Hair Salon^
591^Lexington Avenue^1001^60^Oxford Cafe^
0^120^4^
1^80^2^

Open in new window

I am guessing as I look at this that you might find a huge lightbulb would go on if you made an array of objects.  Maybe I can post an example of how that might look.

Also, you might want to take a little while to create a template with valid markup.  The document is not going to work well in its current state.
http://validator.w3.org/check?uri=http%3A%2F%2Flangsystems.net%2FWalk_By_Map.php&charset=%28detect+automatically%29&doctype=Inline&group=0
0
 
Marco GasiFreelancerCommented:
The syntax is:

echo "<td height = ";
echo $display_key['FRONTAGE'];
0
 
Marco GasiFreelancerCommented:
I reccommend you to read this book http://www.sitepoint.com/books/phpmysql5/. It can help you to understand many thing about Php and how Php and MySql work together.
0
 
breeze351Author Commented:
I tried what you said.  I'm getting an error on lines 380 and 390:

Errors:
 Notice: Uninitialized string offset: 0 in /home/langsyst/public_html/Walk_Map_Display.php on line 380

Notice: Uninitialized string offset: 0 in /home/langsyst/public_html/Walk_Map_Display.php on line 390

Code:
----------------------------------------------------
// ****************************************************
                  // * Create table row for listings.                   *
                        // ****************************************************

                              echo '<tr>';
                                    echo '<td height = "';
                                    echo $display_value['FRONTAGE'];
                                    echo '" bgcolor = "';
                                    echo $bgcolor;
                                    echo '" style= "text-align:left"';
                                    echo ' font color= "blue">';
                                    
                        // ****************************************************
                  // * Display tenant names.                            *
                        // ****************************************************

                                    echo $display_value['TENANT'];
                                    echo "</td>";
--------------------------------------------------------------------
Lines 380 & 390 are bold and underlined.
0
 
Ray PaseurCommented:
When I run this line of code,
echo $display_value['FRONTAGE'];

I get this:
Notice:  Undefined variable: display_value in /public_html/RAY_temp_breeze351.php on line 5

When I run these two lines of code,
$display_value = array();
echo $display_value['FRONTAGE'];


I get this:
Notice:  Undefined index: FRONTAGE in /public_html/RAY_temp_breeze351.php on line 6

When I run these two lines of code,
$display_value = '';
echo $display_value['FRONTAGE'];


I get two messages, like this:
Warning:  Illegal string offset 'FRONTAGE' in /public_html/RAY_temp_breeze351.php on line 6
Notice:  Uninitialized string offset: 0 in /public_html/RAY_temp_breeze351.php on line 6

Could it be possible that you're getting two messages and only telling us about one of the messages?

Also, I second what @marqusG said about the SitePoint book.  It's a good place to start learning this technology.  You would probably want to take a step further back, and pick up the basics of HTML and CSS.  SitePoint has books for that, too.  For PHP, I also like this one: http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0321833899
0
 
Dave BaldwinFixer of ProblemsCommented:
You keep trying to treat $display_value as an array and it is Not.  It is simply a value.
0
 
Ray PaseurCommented:
Have a look at this.  It's amazingly simple to use an array of objects.  The syntax is much simpler than associative array keys, and I find it easier to keep the logic correct.  These arrays are easy to sort on any property of the object with usort().  There are just a lot of advantages to this kind of data structure!
http://www.laprbass.com/RAY_temp_breeze351.php

<?php // RAY_temp_breeze351.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28312538.html

/* BACKGROUND INFORMATION - REQUIRES CLARIFICATION

BNMB
STRT
STOREKEY
SEQ
FRONTAGE

Even_Array_Count = 3
Even_Bldg_Count = 0
Odd_Array_Count = 6
Odd_Bldg_Count = 1

Even Numbers
590^Lexington Avenue^1001^60^Scudder Funds Center^
590^Lexington Avenue^1002^100^345 Park Avenue Back Entrance^
590^Lexington Avenue^1003^40^Le Relais de Venise L'entrecote^
0^200^3^

Odd Numbers
575^Lexington Avenue^1001^20^Duane Reade^
575^Lexington Avenue^1002^20^New York Sports Club (2nd Floor)^
575^Lexington Avenue^1003^20^Building Entrance^
575^Lexington Avenue^1004^60^Camera Land^
591^Lexington Avenue^1001^20^Jean Louis David Hair Salon^
591^Lexington Avenue^1001^60^Oxford Cafe^
0^120^4^
1^80^2^
*/

// AN OBJECT TO CONTAIN THE INFORMATION
Class Thing
{
    // CONSTRUCTOR CREATES CLASS PROPERTIES FROM THE ARGUMENTS
    public function __Construct($a, $b, $c, $d, $e)
    {
        $this->a = $a;
        $this->b = $b;
        $this->c = $c;
        $this->d = $d;
        $this->e = $e;
    }
}

// CREATE A TEST DATA SET - TWO ARRAYS OF OBJECTS
$evens = array
( new Thing('590', 'Lexington Avenue', '1001',  '60', 'Scudder Funds Center')
, new Thing('590', 'Lexington Avenue', '1002', '100', '345 Park Avenue Back Entrance')
, new Thing('590', 'Lexington Avenue', '1003',  '40', 'Le Relais de Venise L\'entrecote')
)
;
$odds = array
( new Thing('575', 'Lexington Avenue', '1001',  '20', 'Duane Reade')
, new Thing('575', 'Lexington Avenue', '1002',  '20', 'New York Sports Club (2nd Floor)')
, new Thing('575', 'Lexington Avenue', '1003',  '20', 'Building Entrance')
, new Thing('575', 'Lexington Avenue', '1004',  '60', 'Camera Land')
, new Thing('591', 'Lexington Avenue', '1001',  '20', 'Jean Louis David Hair Salon')
, new Thing('591', 'Lexington Avenue', '1001',  '60', 'Oxford Cafe')
)
;

// SHOW HOW TO FIND THE INTERSECTIONS OF THE EVEN AND ODD ELEMENTS ON PROPERTY "c"
foreach ($evens as $even)
{
    foreach ($odds as $odd)
    {
        if ($even->c == $odd->c)
        {
            echo PHP_EOL;
            echo $even->c;
            echo " MATCHES FOUND AT ";
            echo "Even: $even->e, ";
            echo "Odd: $odd->e ";
        }
    }
}

Open in new window

0
 
breeze351Author Commented:
Dave:

I know I'm going about this in the wrong way, otherwise I could have figured it out.

I have a populated array called "$even_array".  This array contains all the buildings and stores that have an even number on one side of the street.  The array works for both sides of the street (even and odd (that's another array)).

On the even side of the street there could be anywhere from 1 to 100 addresses.  In each address there could be from 1 to 100 stores ( the 100 is silly, but you get my drift).

Each store has a frontage that I need to use to determine the row height as well as the tenant name.  This data goes into a table.

I want to iterate through it without having to keep a counter for the number of buildings and the number of stores in each building.

I've changed the code to read:
------------------------------------------------------------------------
foreach ($even_array as $display_key=>$display_value)  
------------------------------------------------------------------------

Since the array has multiple variables, what is the syntax to display an individual one?

Telling me to by a book is no help.  I'm sitting here with "PHP & MYSQL Novice to Ninja" and the code I've written came from there.

Thanks
Glenn
0
 
Ray PaseurCommented:
Are there any community colleges in your area?  You may be able to find one that offers classes in PHP programming.  Structured learning (progressively challenging examples in a spiraled curriculum) will give you a lot more value than just copying code and hoping that it works.  You might be able to get access to teachers and advisors.  There is no shortcut to learning any programming language (or any human language).  But it can be made more efficient with a classroom environment.
0
 
Dave BaldwinFixer of ProblemsCommented:
I have a populated array called "$even_array".  This array contains all the buildings and stores that have an even number on one side of the street.
Nothing that you have posted shows that.  All you have posted is $even_array with 5 separate values and None of them are arrays.  Which is why you keep getting errors when you try to access them as arrays.

Can you show me a copy of $even_array that actually contains data for ALL the buildings and not just one?
0
 
breeze351Author Commented:
Got it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.