Jump to content

Need 'Age' in $MyArray[$i]['Age']


sen5241b
 Share

Recommended Posts

Posted (edited)

More detail and context as requested:

I use the following lines of code a lot because I use deal with a lot of multi-dimensional associative arrays:

$headerNames = implode('‖', array_keys(current($AnyArray)));
$ColumnNameLookup = explode('‖', $headerNames);  

$ColumnNameLookup is a lil array to lookup column names in an associative array. I call them 'column names' but in  proper PHP terminology they're called "key names".  As you can see its fairly easy to extract the key names  and put them in a little array. Those two lines of code has made it easy to write some data manipulation functions that can perform all sorts of operations on a specific column name in any generic array I pass to it. E.g.  such as concatenate a string to a string already in the array or highlight a string within a string:

HighlightColumnsWithBadData($AnyArray, $ColumnName)

My current problem:  I have a  func that validates a long list of data elements in a multi-dimensional associative array.  If a particular element, like  'Age' in $MyArray[$i]['Age'] has bad data, then I need to pass the key name 'Age' to a func that reports the data problem. That is why I need the  'Age' in $MyArray[$i]['Age'] or the 'Name' in  $MyArray[$i]['Name'].

Why not:

if ($MyArray[$i]['Age'] >  700) then pass 'Age' to reporting func? 

Key names for the numerous arrays I deal with are not hard coded anywhere in the generic data manipulation funcs and that makes these funcs immune to key renames and flexible enough  to process any array.

 

 

 

Edited by sen5241b
Link to comment
Share on other sites

Yep. I use foreaches all the time. I enhanced your code a bit.  Foreach prints the key value  and but not the key name.

<?php
$myArray = array();
$myArray[0] = 'fred';
$myArray[1] = 'Joe';

foreach ($myArray as $key => $value):
  echo '<br> '  . $myArray[$key];
endforeach;

$myArray = array();
$myArray[0]['Name'] = 'fred';
$myArray[0]['Age'] = 55;
$myArray[1]['Name'] = 'Joe';
$myArray[1]['Age']= 765;
print_r($myArray);
foreach ($myArray as $key => $value):
  echo '<br> ';
  echo '<br> '  . $myArray[1]['Age'];
endforeach;
?> 

 

Link to comment
Share on other sites

The key names there - you just have to use it.

$myArray = array();
$myArray[0]['Name'] = 'fred';
$myArray[0]['Age'] = 55;
$myArray[1]['Name'] = 'Joe';
$myArray[1]['Age']= 765;

echo '<pre>', print_r($myArray, 1), '</pre>';

foreach ($myArray as $key => $value):
    echo "<br><b>Person $key:</b><br>";
    foreach ($value as $k => $v) {
        echo " &bull; $k : $v<br>";
    }
endforeach;

Outputs

Array
(
    [0] => Array
        (
            [Name] => fred
            [Age] => 55
        )

    [1] => Array
        (
            [Name] => Joe
            [Age] => 765
        )

)

Person 0:
• Name : fred
• Age : 55

Person 1:
• Name : Joe
• Age : 765

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.