Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Associative Array in an Indexed Array

I have a $string:

'Name   Height  Weight
 John   177     142
 Jill   156     123
 Jacob  183     157'

And I'm turning it into an $array of the following structure:

Array (
    [0] => Array (
        ['Name'] => 'John'
        ['Height'] => '177'
        ['Weight'] => '142'
    )
    [1] = > Array (
        ['Name'] => 'Jill'
        ['Height'] => '156'
        ['Weight'] => '123'
    )
    [2] = > Array (
        ['Name'] => 'Jacob'
        ['Height'] => '183'
        ['Weight'] => '157'
    )
)

Using the following code:

$rows = explode("\n",$string); //creates an indexed array of rows as strings
$headers = explode("\t",$rows[0]); //creates an indexed array of headers as strings
$rows = array_slice($rows,1); //removes headers from $rows
$array = Array();
foreach($rows as $row) {
    $array[] = array_combine($headers, explode("\t",$row)); //creates associative arrays for each row
}

However, I cannot access the associative arrays inside of the indexed $array

For example, this doesn't work:

echo $array[0]['Name'];

Even though echo implode(', ', array_keys($array[0])); gives:

Name, Height, Weight

I've tried many different ways of accessing the associative arrays inside of the indexed array, but with no luck. What am I doing wrong?

EDIT:

So,

$string = "Name Height  Weight
John    177 142
Jill    156 123
Jacob‌​ 183 157";

Does not work, but

$string = "Name\tHeight\tWeight\nJohn\t177\t142\nJill\t156\t123\nJacob‌​‌​\t183\t157";

does...

So I suppose the question is: What's the difference? And how would I interpret the former string as the latter?

like image 687
jeffbeene Avatar asked Dec 09 '25 08:12

jeffbeene


1 Answers

Your code does not produce that array structure but it can be fixed like this:

$string = 'Name   Height  Weight
 John   177     142
 Jill   156     123
 Jacob  183     157';

$rows = explode("\n",$string); //creates an indexed array of rows as strings

$headers = preg_split("#\s+#",trim($rows[0], "\n\r\t ")); //creates an indexed array of headers as strings, by splitting by any white space
var_dump($headers);

$rows = array_slice($rows,1); //removes headers from $rows
$array = Array();
foreach($rows as $row) {
    $array[] = array_combine($headers, preg_split("#\s+#",trim($row, "\n\r\t "))); //creates associative arrays for each row, by splitting by any white space
}

var_dump($array);

This produces output:

array(3) {
  [0]=>
  string(4) "Name"
  [1]=>
  string(6) "Height"
  [2]=>
  string(6) "Weight"
}
array(3) {
  [0]=>
  array(3) {
    ["Name"]=>
    string(4) "John"
    ["Height"]=>
    string(3) "177"
    ["Weight"]=>
    string(3) "142"
  }
  [1]=>
  array(3) {
    ["Name"]=>
    string(4) "Jill"
    ["Height"]=>
    string(3) "156"
    ["Weight"]=>
    string(3) "123"
  }
  [2]=>
  array(3) {
    ["Name"]=>
    string(5) "Jacob"
    ["Height"]=>
    string(3) "183"
    ["Weight"]=>
    string(3) "157"
  }
}

The main ideas are that you must trim evey row string by any additional whitespaces and to split by the longest whitespace sequence.

like image 195
Constantin Galbenu Avatar answered Dec 10 '25 22:12

Constantin Galbenu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!