0

I have a multidimensional array that is being populated by a mysql query. I need to pull out a value and cannot figure out how to do it, I can only get the keys and not the actual value. The array is show below and I want to extract the "SERVICE" value using a loop so i can echo each one out

Here is the array:

array (
  0 => 
  array (
    0 => 'SERVICE 1',
    'cwa' => 'SERVICE 1',
  ),
  1 => 
  array (
    0 => 'SERVICE 2',
    'cwa' => 'SERVICE 2',
  ),
  2 => 
  array (
    0 => 'SERVICE 3',
    'cwa' => 'SERVICE 3',
  )
)

$result = $conn->query($sql);


$anames = array();


while ($row = mysqli_fetch_array($result))
{
    $anames[] = $row;
}

foreach($anames as $key => $value) {
    echo($key);
}
2
  • If you are fetching the key , you will get the key , $key is a $key of value and $value is value of key , replace echo($key); with echo $value; Commented Jun 21, 2016 at 8:37
  • What do you mean by "SERVICE" value?? Commented Jun 21, 2016 at 8:38

2 Answers 2

1

If you have the array like you shown in question and name $anames then do this.

Using the associative index:

foreach($anames as $key => $value) {
    echo $value['cwa']; //SERVICE 1, SERVICE 2, SERVICE 3
}

OR, using the non-associative index:

foreach($anames as $key => $value) {
    echo $value[0]; //SERVICE 1, SERVICE 2, SERVICE 3
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, missed the obvious ! :)
0

Use echo $value[0]; / echo $value[cwa]; instead of echo($key);

1 Comment

Thanks, missed the obvious ! :)

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.