How can I echo one element from this array?

Here's the PHP:

$columnRowsArray = array(
                   $case_no,$name,$title,$date_of_injury,$where,$description,
                   $death,$da,$dr,$other,
                   $total_da,$total_dr,
                   $injury,$skin,$respiratory,$poison,$hearing,$all_other
            );

It's then parsed out like this:

      foreach ($columnRowsArray as $value) {
            $columnRows .= '<td style="border: 1px solid #929191;">'.$value.'</td>';
            }

Echo one of those variables is falling neatly into its own cell and the table looks like this:



When I do a var_dump and isolate one "row," it looks like this:

The resulting data when I do a var_dump(columnRowsArray) looks like this:

string(10) "623 - 2017"
string(19) "Stephanie Weinstein"
string(11) "Nurse RN II"
string(10) "01/04/2017"
string(7) "office " 
string(299) "Sliding door from cubicle fell and struck the back of head and upper back causing headache EE refused medical tx when recommended. EE called Medcor again due to persistent moderate headache but refused medical treatment when recommended and stated she will take OTC and then see how she is feeling."
string(0) "" 
string(0) "" 
string(0) "" 
string(1) "x"
int(0)
int(0)
string(1) "x"
string(0) "" 
string(0) "" 
string(0) "" 
string(0) "" 
string(0) "" 

If I wanted to evaluate "string(1) "x"," I'm thinking I would write something like "echo $value[9]," but it's not working and I don't know why.

What am I missing?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
What do you mean by evaluate "string(1) "x"," ?

If you're just trying to echo it inside the loop, then your code looks right. This code would echo the 10th row value from each row in the array:

foreach ($columnRowsArray as $value) {
  echo $value[9];
}

If it's working sometimes and not other times, it might be that sometimes rows do not always have the same lengths for some reason.

Otherwise, I'd suggest expanding on the details of "it's not working" - is there an error? What's the end result?
0
Olaf DoschkeSoftware DeveloperCommented:
You're composing $columnRowsArray as array of single variables, column 10 is $other, column 9 is $dr (remember arrays are addressed 0 based if you address them via element index at all). If you don't want to depend on the knowledge how you compose the array, you may address $columnRowsArray[9].

By the way the vardump should show you an array with its element index, there's really nothing complex going on here, unless you are hiding further facts (unintentionally), eg $columnRowsArray being extended row by row to be an array of arrays, then a foreach will give a single row array, which you then address by $row[9] to get column 10 of a single row.

Bye, Olaf.
0
mohan singhWeb developerCommented:
You can do like this
$columnRowsArray = array($case_no,$name,$title,$date_of_injury,$where,$description,$death,$da,$dr,$other,$total_da,$total_dr,                $injury,$skin,$respiratory,$poison,$hearing,$all_other);

// explode your array 

$explode = explode(',',$columnRowsArray);
$case_no = $explode[0];
$name= $explode[1];
$title= $explode[2];
$date_of_injury= $explode[3];
$where= $explode[4]; 
.
.
.
.

Open in new window

Thank You
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Bruce GustPHP DeveloperAuthor Commented:
OK, guys!

Here's a more comprehensive explanation of what's going on.

First off, let me give you a screenshot of what I'm contending with:

screenshot of graph
What I want to do is center all of the values to the right of the description. Where you see "x's" in the columns "Death" all the way to "All other illnesses," I want to center those values.

I'm going to go ahead and give you a comprehensive snapshot of the code. It may be more than what you want or need, but at least this way, you've got some background.

Beginning on line #1, you've got the SELECT and then online #229, you can see where they're stuffing the array with different values.

Those values are being distributed on line #247.

When I do a var_dump of $value, I get this:

value array
Notice the absence of any indexes. I'm looking at http://php.net/manual/en/control-structures.foreach.php and it seems that when I do a "foreach" and use "$value," I get an automatic reset after each row. But, at the same time, it doesn't look like you can do something like "$value[0]."

When I did a var_dump of $columnRowsArray, I got something like over and over again:

array(18) { [0]=> string(10) "623 - 2017" [1]=> string(19) "Stephanie Weinstein" [2]=> string(11) "Nurse RN II" [3]=> string(10) "01/04/2017" [4]=> string(7) "office " [5]=> string(299) "Sliding door from cubicle fell and struck the back of head and upper back causing headache EE refused medical tx when recommended. EE called Medcor again due to persistent moderate headache but refused medical treatment when recommended and stated she will take OTC and then see how she is feeling." [6]=> string(0) "" [7]=> string(0) "" [8]=> string(0) "" [9]=> string(1) "x" [10]=> int(0) [11]=> int(0) [12]=> string(1) "x" [13]=> string(0) "" [14]=> string(0) "" [15]=> string(0) "" [16]=> string(0) "" [17]=> string(0) "" }

(notice "array(18)." That's my clue that this is a repeating dynamic, but I can't figure out how to get to values [6] thru [17].

When I tried to do "echo $value[0]," I got this:

string(1) "6" string(1) "S" string(1) "N" string(1) "0" string(1) "o" string(1) "S" string(0) "" string(0) "" string(0) "" string(1) "x" NULL NULL string(1) "x" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(1) "5" string(1) "L" string(1) "P" string(1) "0" string(1) "p" string(1) "s" string(0) "" string(0) "" string(0) "" string(1) "x" NULL NULL string(1) "x" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(1) "5" string(1) "D" string(1) "E" string(1) "0" string(1) "o" string(1) "E" string(0) "" string(1) "x" string(0) "" string(0) "" NULL NULL string(1) "x" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(1) "6" string(1) "J" string(1) "I" string(1) "0" string(1) "R" string(1) "E" string(0) "" string(0) "" string(0) "" string(1) "x" NULL NULL string(1) "x" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(1) "5" string(1) "E" string(1) "N" string(1) "0" string(1) "p" string(1) "E" string(0) "" string(0) "" string(0) "" string(1) "x" NULL NULL string(1) "x" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(1) "6" string(1) "J" string(1) "P" string(1) "0" string(1) "p" string(1) "W" string(0) "" string(0) "" string(0) "" string(1) "x" NULL NULL string(1) "x" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(1) "6" string(1) "A" string(1) "A" string(1) "0" string(1) "u" string(1) "W" string(0) "" string(0) "" string(1) "x" string(0) "" NULL NULL string(1) "x" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(1) "6" string(1) "K" string(1) "P" string(1) "0" string(1) "S" string(1) "E" string(0) "" string(0) "" string(1) "x" string(0) "" NULL NULL string(1) "x" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(1) "6" string(1) "P" string(1) "N" string(1) "0" string(1) "C" string(1) "E" string(0) "" string(0) "" string(0) "" string(1) "x" NULL NULL string(1) "x" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(1) "6" string(1) "K" string(1) "I" string(1) "0" string(1) "I" string(1) "W" string(0) "" string(0) "" string(0) "" string(1) "x" NULL NULL string(1) "x" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(1) "6" string(1) "T" string(1) "P" string(1) "0" string(1) "U" string(1) "E" string(0) "" string(0) "" string(1) "x" string(0) "" NULL NULL string(1) "x" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(1) "6" string(1) "I" string(1) "N" string(1) "0" string(1) "P" string(1) "E" string(0) "" string(1) "x" string(0) "" string(0) "" NULL NULL string(1) "x" string(0) "" st

Open in new window


And that's me doing this:

$columnRows .= '<tr style = "border: 1px solid #929191;">';
            foreach ($columnRowsArray as $value) {
                  var_dump($value[0]);
            $columnRows .= '<td style="border: 1px solid #929191;">'.$value.'</td>';
            }
            $columnRows .= '</tr>';

So, how can I access to values [6] thru [17] and adjust the style of that particular cell so it's centered?
0
Olaf DoschkeSoftware DeveloperCommented:
Mixing vardumps with HTML is a bad idea to see anything clearer.

Your vardump of $columnRowsArra shows it's the simple array you get from the code you initially showed. Then $value is just a single value, not an array and you don't get something of $value[0].

 $index=0;
foreach ($columnRowsArray as $value) {
                  echo $index++.":".$value."<br>";}

Open in new window

This should list out all $columnRowsArray elements. But does this help you find what you seek at all?

If all you want os to center cells, you don't do so by changing values (eg with tabs or spaces), you design css to center the TD tag content. Do you have any CSS file in your project? You're using CSS styling here, but directly writing out the css style attribute with border css. That'll just result in very verbose and repeated HTML. Keep it unchanged at first, but keep an eye on it, there's much potential for improvement.

A td cell can be centered by the styling it text-align:center

Bye, Olaf.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bruce GustPHP DeveloperAuthor Commented:
Hey, Olaf!

You're right about the CSS. The thing is, without being able to parse out the values, then I'm obligated to center all of the rows and I don't want to that. That's why I was looking for a way to "explode" or separate the values within the $value array.

Thing is, I've never worked with this kind of configuration before. Most of the arrays that I create and / or wind up working with are indexed in some way, so that's why this thing had me baffled.

But, thanks to your help the dragon has been slayed.

Here's what I did:

Here's the original code:

$columnRows .= '<tr style = "border: 1px solid #929191;">';
foreach ($columnRowsArray as $value) {
  $columnRows .= '<td style="border: 1px solid #929191;">'.$value.'</td>';
  }
$columnRows .= '</tr>';

Here's what I did to make my dreams come true...

$columnRows .= '<tr style = "border: 1px solid #929191;">';
$column_index=0;
foreach ($columnRowsArray as $value) {
      if($column_index>5)
      {
            $columnRows .= '<td style="border: 1px solid #929191; text-align:center; vertical-align:middle;">'.$value.'</td>';
      }
      else
      {
            $columnRows .= '<td style="border: 1px solid #929191; vertical-align:middle;">'.$value.'</td>';
      }
      $column_index=$column_index+1;
}
$columnRows .= '</tr>';

Basically, I just inserted my own index and determined where I needed to assert a "text-align:center" and we're gold!

Thanks so much!
0
Olaf DoschkeSoftware DeveloperCommented:
OK, just perhaps a tip for the future, as you get so unreadable output from vardump in the context of HTML, put it inside <pre></pre> tags and you should see lines. Also the native PHP function is var_dump, not vardump. You might have defined a function, which doesn't give you the array indexes.

Run this as an example of the output and formatting differences of var_dump vs print_r:
<?php $columnRowsArray = array('a','b','c'); ?>
<pre><?php var_dump($columnRowsArray)?></pre>
<hr>
<pre><?php print_r($columnRowsArray)?></pre>
<hr>
<?php var_dump($columnRowsArray)?>
<hr>
<?php print_r($columnRowsArray)?></pre>

Open in new window

0
Bruce GustPHP DeveloperAuthor Commented:
Thanks for the tip, Olaf. I did use var_dump. The "vardump" was a typo. But throwing the result between two <pre> tags is a good idea.

Thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.