2
Array ( [0] => Array ( [No.,"Ticker","Company"] => 
                        1,"A","Agilent Technologies Inc." ) 
        [1] => Array ( [No.,"Ticker","Company" ] => 
                        2,"AA","Alcoa, Inc.") 
      )

This is the output when i type print_r($arrayOne); What can I do so I can only get the Company column i.e. Agilent Technologies Inc, and Alocoa, Inc.

I tried echo $arrayOne['Company'] and $arrayOne[0]['Company'] but it just outputted 'Array'.

6
  • is it because array within an array?? Commented Nov 24, 2012 at 15:17
  • No, it is because there's no Company key in either of those arrays. Commented Nov 24, 2012 at 15:20
  • How the heck did you even construct this type of array? Never seen that before. Commented Nov 24, 2012 at 15:20
  • Not sure I've seen a print_r output like that before Commented Nov 24, 2012 at 15:21
  • $csvArr = csv_to_array(file_get_contents('companies.csv')); Then, i did print_r($csvArr); Commented Nov 24, 2012 at 15:21

2 Answers 2

3

What you have is an outer array with two sub-arrays, each of which has only one value. Its key is the description of the 3 columns, and its value is the CSV value. If you have PHP 5.3, use str_getcsv() to parse it and return the third column

// Loop over the outer array
foreach ($arrayOne as  $subarr) {
  // Loop over the sub-arrays (though your sample only has one value each)
  // could also use array_pop()
  foreach ($subarr as $key => $csvstring) {
    // $csvstring holds the CSV value like  '1,"A","Agilent Technologies Inc."'
    // Parse it with str_getcsv()
    $cols = str_getcsv($csvstring);
    // Company name is the 3rd value, key [2]
    echo $cols[2] . "\n";
  }
}

For example with the input:

$arrayOne = array(
  array('No.,"Ticker","Company"' => '1,"A","Agilent Technologies Inc."' ), 
  array('No.,"Ticker","Company"'  => '2,"AA","Alcoa, Inc."') 
);
// The above loop prints:
Agilent Technologies Inc.
Alcoa, Inc.
Sign up to request clarification or add additional context in comments.

3 Comments

It's definitely the right way to go (+1), still I wonder why this statement - echo $arrayOne[0]['Company'] - resulted in Array (as mentioned in the question).
so $csv = file_get_contents('finviz.csv'); ?
@ealeon No, file_get_contents() isn't needed. $csv is the loop iteration, which is an array value. I'll change it above because it isn't clear. Each of those is a subarray from $arrayOne.
0

Your array should be like this:

$arrayOne = array(array('no' => 1, 'ticker' => 'A', 'company' => 'Agilent Technologies Inc.'),
                  array('no' => 2, 'ticker' => 'AA', 'company' => 'Alcoa Inc.'));

echo $array[0]['company']; // will print Agilent Technologies

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.