0

Hi all I have an array "$decodedData" of object from json data.

var_export($decodedData);

returns next:

array ( 0 => array ( 'number' => '2', 'type' => 'accs', ), 1 => array ( 'number' => '5', 'type' => 'accs', ), )

I'm trying to output all the "numbers" values:

foreach ($decodedData as $number)
{
    echo implode(',', $number);
}

but I'm getting "type" values either

2,accs5,accs

How can I get rid of those?

1
  • 2
    Have you read the PHP docs for foreach? There is a way to get the key and value. Commented Nov 13, 2013 at 17:42

2 Answers 2

3

You can use array_map to accomplish that.

The first parameter is a callback function that will receive each element and return something to replace it with. In this case, we are returning the number key of each element.

$result = array_map(function($val) {
    return $val['number'];
}, $array);

echo implode(',', $result);
Sign up to request clarification or add additional context in comments.

Comments

1

You're looping through an array of arrays, so $number is returning a complete array, not the number value. To access the number value of each, do something like this:

foreach ($decodedData as $number=>$val){
    echo implode(',', $val['number']);
}

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.