1

I have a php array output like this

Array ( 
[DISEASE] => Array ( [0] => DM [1] => HT ) 
[DRUG] => Array ( [0] => INSULIN [1] => DIURETIC) 
) 

Now I want to print the following

For Disease DM, INSULIN is used
For Disease HT, DIURETIC is used

i.e I want to match the value from array Disease with that of Drug. Please help me.

5 Answers 5

4

Why don't you use the keys in an array? http://php.net/manual/en/language.types.array.php (see example one)

Then the array would look like:

$items = array(
    array('disease' => 'DM', 'drug' => 'INSULIN'),
    array('disease' => 'HT', 'drug' => 'DIURETIC'),
);

And you could call it like:

foreach($items as $item)
{
    echo $item['disease'] . ' - ' .  $item['drug'];
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Good suggestion, and this can be easily accomplished with array_combine in this case even, see my answer
2

The key of an array points to its value. So your keys are DISEASE and DRUG each with keys of 0 and 1. So - we are matching keys not values.

There are many ways to print arrays. It is difficult for me to assume the most flexible way of printing these values for the future, but here is one way:

foreach ($items['DISEASE'] as $id => $disease)
{
   echo 'For Disease ' . $disease . ', ' . $items['DRUG'][$id] . ' is used'."\n";
}

The key $id was used to match between the Disease and Drug sub-arays.

Comments

1

Assuming both arrays have the same length, you could do something like this:

for ($i=0; $i < sizeof($yourarray['DISEASE']); ++$i) {
    echo 'For Disease ', $yourarray['DISEASE'][$i], ', ';
    echo $yourarray['DRUG'][$i], ' is used';
}

4 Comments

This wouldn't work since you're counting the outer array when you should be counting the inner array
OMG! Don't calc sizeof everytime! use for ($i=0,$s =sizeof($yourarray['DISEASE']); $i < $s; ++$i) instead
Whatever...it's a bad solution anyway. ;) And I doubt very much that the performance change will be tangible. But in general you are right, of course.
As foreach is just syntax for the loop I used, there is no reinvention there anywhere.
0

The

[DISEASE] => Array ( [0] => DM [1] => HT ) 

contains your keys.

The

[DRUG] => Array ( [0] => INSULIN [1] => DIURETIC)

contains the according values.

Assuming your array would be named $array, you can just combine both:

$mapped = array_combine($array['DISEASE'], $array['DRUG']);

Then you can access each drug for disease via the key:

$mapped['DM']; # INSULIN

To print all, just iterate

foreach($array['DISEASE'] as $disease)
{
    $drug = $mapped[$disease]
    echo "For Disease $disease, $drug is used.\n";
}

Comments

0

Here is the trick: You may get the point and can solve your problem

$an = array(
        'numbers'=>array(1,2,3,4,5),
        'alphabates'=>array('a','b','c','d','e')
    ); 

foreach($an['numbers'] as $key=>$value){
    echo $value." => ".$an['alphabates'][$key]."<br>";
}

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.