Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the array of a single type from a multi dimensional array (without a loop)

I have the following array $foo

array(10) {
[0] => array(4) {

["merchantId"] => string(5) "12e21"
["programId"] => string(27) "ddd3333"
["networkId"] => int(4)
["clientId"] => int(178)
}
[1] => array(4) {

["merchantId"] => string(5) "112e1"
["programId"] => string(27) "2vfrdbv1&=10&tmfdpid=csss"
["networkId"] => int(4)
["clientId"] => int(178)
}
[2] => array(4) {

["merchantId"] => string(5) "112e1"
["programId"] => string(27) "2vfrdbv1&=10&tmfdpid=csss"
["networkId"] => int(4)
["clientId"] => int(178)
}

And I need an array of clientId's (only)

Is it possible to access just the clientId to create an array of id's without a loop?

Something like:

$foo['clientId']; //which doesn't work
like image 718
David Sigley Avatar asked Sep 13 '13 11:09

David Sigley


People also ask

How do you find the specific value of an array?

Use filter if you want to find all items in an array that meet a specific condition. Use find if you want to check if that at least one item meets a specific condition. Use includes if you want to check if an array contains a particular value. Use indexOf if you want to find the index of a particular item in an array.

How are multi-dimensional arrays different that one-dimensional arrays?

There are two types of arrays as 1D and 2D arrays. The main difference between 1D and 2D array is that the 1D array represents multiple data items as a list while 2D array represents multiple data items as a table consisting of rows and columns.

Is a multidimensional array an array of arrays?

A multidimensional array is an array containing one or more arrays.

How will you identify a single dimensional array?

In the simplest terms, a one-dimensional array in C is a list. Each element of the list contains only one value, whether that value be an int, char, or float. To declare an array, three values are typically needed: the data type, name of the array, and the number of elements to be contained in the array.


1 Answers

In PHP 5.5:

$rgResult = array_column($foo, 'clientId');

in PHP <=5.5:

$rgResult = array_map(function($rgItem)
{
  return $rgItem['clientId'];
}, $foo);

(put <= since this, of cause, will work in 5.5 too)

like image 191
Alma Do Avatar answered Nov 14 '22 22:11

Alma Do