views:

27

answers:

1

Hi,

So Im working on a Excel xml parsing class, and Im trying to search through an array of the header values of the file, to return the key integer. If I can get the key int, then I can grab an entire column by the value of the header in the file. I have tried using in_array, search_array and a bunch of other functions, but nothing is seeming to work. Here is the array Im working with

array(30) { [0]=> array(1) { ["Data"]=> object(SimpleXMLElement)#37 (1) { [0]=> string(6) "Item #" } } [1]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#38 (0) { } } [2]=> array(1) { ["Data"]=> object(SimpleXMLElement)#39 (1) { [0]=> string(5) "Style" } } [3]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#40 (0) { } } [4]=> array(1) { ["Data"]=> object(SimpleXMLElement)#41 (1) { [0]=> string(5) "Brand" } } [5]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#42 (0) { } } [6]=> array(1) { ["Data"]=> object(SimpleXMLElement)#43 (1) { [0]=> string(5) "Color" } } [7]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#44 (0) { } } [8]=> array(1) { ["Data"]=> object(SimpleXMLElement)#45 (1) { [0]=> string(4) "Size" } } [9]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#46 (0) { } } [10]=> array(1) { ["Data"]=> object(SimpleXMLElement)#47 (1) { [0]=> string(12) "Active Price" } } [11]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#48 (0) { } } [12]=> array(1) { ["Data"]=> object(SimpleXMLElement)#49 (1) { [0]=> string(6) "Gender" } } [13]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#50 (0) { } } [14]=> array(1) { ["Data"]=> object(SimpleXMLElement)#51 (1) { [0]=> string(4) "Cost" } } [15]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#52 (0) { } } [16]=> array(1) { ["Data"]=> object(SimpleXMLElement)#53 (1) { [0]=> string(11) "On-hand Qty" } } [17]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#54 (0) { } } [18]=> array(1) { ["Data"]=> object(SimpleXMLElement)#55 (1) { [0]=> string(9) "Dept Name" } } [19]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#56 (0) { } } [20]=> array(1) { ["Data"]=> object(SimpleXMLElement)#57 (1) { [0]=> string(15) "Color Full Name" } } [21]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#58 (0) { } } [22]=> array(1) { ["Data"]=> object(SimpleXMLElement)#59 (1) { [0]=> string(8) "ItemType" } } [23]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#60 (0) { } } [24]=> array(1) { ["Data"]=> object(SimpleXMLElement)#61 (1) { [0]=> string(3) "UPC" } } [25]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#62 (0) { } } [26]=> array(1) { ["Data"]=> object(SimpleXMLElement)#63 (1) { [0]=> string(6) "UPCNEW" } } [27]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#64 (0) { } } [28]=> array(1) { ["Data"]=> object(SimpleXMLElement)#65 (1) { [0]=> string(3) "MPN" } } [29]=> array(1) { ["NamedCell"]=> object(SimpleXMLElement)#66 (0) { } } } array(1) { ["Data"]=> object(SimpleXMLElement)#37 (1) { [0]=> string(6) "Item #" } } 

So ya basically I am just trying to for instance search for "Brand" within that array, to return the array key that corrosponds to brand (2)

Any help with this would be much appreciated!

+1  A: 

It's hard to answer your question without seeing what you used to generate the var_dump output above.

My best guess is that that data itself looks something like this:

$data = array(array("Data" => simplexml_load_string("<Tag>Brand</Tag>")));

If that's the case, you can return the value "Brand" by calling $data[0]["Data"]

You could iterate over the array searching for the term "Brand" by doing this:

$rowNum = 0;
$cellNum = 0;
foreach($data as $row){
    $rowNum++;
    foreach($row as $cellKey => $cellVal){
        $cellNum++;
        echo "Row $rowNum at cell $cellNum has key '$cellKey' and val '$cellVal'";
    }    
}

This will output:

Row 1 at cell 1 has key 'Data' and val 'Brand'

With that said, Gordon is probably correct, and an existing library, such as phpexcel is probably a way less painful way to do this type of thing.

unchek