0

I am trying to read each element of an Object array, the code reads values from an excel file and then assign the values to a variable.

for ($row = 1; $row <= $highestRow; ++ $row) {

    $fname = $worksheet->getCellByColumnAndRow(0, $row);

    echo '<td>' . $fname . '<br></td>';
}

The output of 'fname' is four different names, however I want to read each value using a loop and then assign them to a link as follows: www.example.com/fname so that four different links get opened up when I execute the code. I just can't figure out how to access each element of 'fname' object one by one. I read over examples available online, but to no avail. Please bear in mind I am a newbie to PHP. Thanks

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
DashD
  • 83
  • 1
  • 1
  • 11
  • If `example.com` in a localhosted site we cannot see it – RiggsFolly Nov 30 '16 at 22:59
  • What does `$row` look like. Try `var_dump()`. – AbraCadaver Nov 30 '16 at 23:00
  • @RiggsFolly the link I put here is just as an example, it it not a live url – DashD Nov 30 '16 at 23:03
  • Now it is just an example – RiggsFolly Nov 30 '16 at 23:04
  • @RiggsFolly it was an example to start with. I don't see why I would give out the real website link on a public form – DashD Nov 30 '16 at 23:05
  • @AbraCadaver var_dump($row) gave the following output "int(5)" – DashD Nov 30 '16 at 23:06
  • I am not suggesting you should. But it was a live clickable link originally, that went nowhere – RiggsFolly Nov 30 '16 at 23:06
  • @RiggsFolly ok sorry about that. I did so because in the past when I put a link and didn't use html tag , the question got rejected and such. – DashD Nov 30 '16 at 23:08
  • What and all are the `keys` you have in $fname ? – Nandan Bhat Nov 30 '16 at 23:08
  • I dont follow what you mean when you say _The output of 'fname' is four different names_ Do you mean each `$fname` actually contains 4 items of data and you want to fiddle with those bits to built an anchor tag? – RiggsFolly Nov 30 '16 at 23:10
  • I wanted to know whether it's `key-value` pair or just an array of 4 values. – Nandan Bhat Nov 30 '16 at 23:13
  • Show us the results of this line `echo '' . $fname . '
    ';` And then show us what you want to be output please
    – RiggsFolly Nov 30 '16 at 23:14
  • @RiggsFolly I mean that when I echo $fname it outputs the following: 'Tim John Sam Tom' , so it has 4 elements in it, now if I wanna assign $fname[1] to a variable , or even echo $fname[1] , it results into this error: "Fatal error: Cannot use object of type PHPExcel_Cell as array" – DashD Nov 30 '16 at 23:14
  • Do you mean you want to `$names = explode(' ', $fname); echo $names[0]; echo $names[1];` – RiggsFolly Nov 30 '16 at 23:21

2 Answers2

0

Check if this works,

    foreach($fname as $values)
    {
        $value1 = $values->keyname1;
        $value2 = $values->keyname2;
        $value3 = $values->keyname3;
        $value4 = $values->keyname4;
    }

    $url = "http://example.com/".$value1;
Nandan Bhat
  • 1,573
  • 2
  • 9
  • 21
-1

If $fname contains a space delimited list of names like Tim John Sam Tom then you can use explode() to turn that STRING into an array of strings.

for ($row = 1; $row <= $highestRow; ++ $row) {

    $fname = $worksheet->getCellByColumnAndRow(0, $row);
    $names = explode(' ', $fname);
    foreach ( $names as $name ) {
        echo '<td>' . $name . '</td>';
    }
}

So if you use the $names array

$names[0] would be `Tim`
$names[1] would be `John`
$names[2] would be `Sam`
$names[3] would be `Tom`
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149