How to Get an Array of Specific "Key" in Multidimensional Array Without Looping

How to get an array of specific key in multidimensional array without looping

Since PHP 5.5, you can use array_column:

$ids = array_column($users, 'id');

This is the preferred option on any modern project. However, if you must support PHP<5.5, the following alternatives exist:

Since PHP 5.3, you can use array_map with an anonymous function, like this:

$ids = array_map(function ($ar) {return $ar['id'];}, $users);

Before (Technically PHP 4.0.6+), you must create an anonymous function with create_function instead:

$ids = array_map(create_function('$ar', 'return $ar["id"];'), $users);

Multidimensional Array: How to get all values of a specific key?

You don't even need nested loop here, if you want to print only the url. Try this:

foreach ($abstract_details as $abstract_detail) {
$result .= $abstract_detail['url'] . '<br>';
}

Output:

http://yourclick.ch
http://google.com

How to find value inside multidimensional array without looping?

Use https://www.php.net/manual/en/function.array-column.php for this.

As this is a builtin function it should perform better than doing your own loop.

Example:

$accountDescriptions = array_column($accounts,'desc','account');

Will result in:

Array
(
[ACR016] => Salary
[ACR017] => Bonuses
)

Alternatively you can use:

Example:

$accountDescriptions = array_column($accounts,null,'account');

Then you will get all data keyed with account instead of numeric index.

How to use array values as keys without loops?

Simply use array_combine along with the array_column as

array_combine(array_column($array,'id'), array_column($array,'name'));

Or you can simply use array_walk if you have PHP < 5.5 as

$result = array();
array_walk($array, function($v)use(&$result) {
$result[$v['id']] = $v['name'];
});

Edited:

For future user who has PHP > 5.5 can simply use array_column as

array_column($array,'name','id');

Fiddle(array_walk)

Remove number of keys from multidimensional array without loop

Finally i have done using array_map

$data = array_map(
function (array $subArr) {
unset($subArr['id']);
unset($subArr['group_id']);
unset($subArr['star_rating']);
return $subArr;
},
$array
);

print_r($data);

create a single dimension array from multidimensional array key

We can use array_column to extract a single dimensional array from the multidimensional array based on a key.

$roles = array_column($roles, 'ID');

Returns

Array (
[0] => 53
[1] => 55
[2] => 58
)

How can I check index in array 2 dimensional without loop in php?

Using array_search(), and array_column()

<?php

$list_team = array(
(object)array(
'id' => 1,
'name' => 'chelsea.jpg'
),
(object)array(
'id' => 2,
'name' => 'mu.jpg'
),
(object)array(
'id' => 3,
'name' => 'arsenal.jpg'
),
);

$team = 'chelsea.jpg';

// array column, returns all value of sub array, with key name
// array_search will return key
$key = array_search($team, array_column($list_team, 'name'));

if($key!==false){

// your object will be
print_r($list_team[$key]);

// access remaining..
echo $list_team[$key]->name.' '. $list_team[$key]->id.PHP_EOL;
}
?>

how to get data from multidimensional array without any key Value in Javascript?

You can use Object.keys(), As the object is an array use index to access array element then the method can be used.

console.log(Object.keys(data[0]))

var data = [{  "7acafaa5-f276-9094": {    "name": "kaa",    "number": "46645",    "city": "Surat",    "hobby": "hockey",    "birthdate": "2017-06-21"  },  "566934ae-879a-58cb": {    "name": "Karnav B Pargi",    "number": "09601096013",    "city": "Ahmedabad",    "hobby": "hockey",    "birthdate": "2017-06-15"  },  "365867ca-9f09-367b": {    "name": "Karnav B Pargi",    "number": "09601096013",    "city": "Ahmedabad",    "hobby": "hockey",    "birthdate": "2017-06-15"  }}];console.log(Object.keys(data[0]))

How to get value by key from multidimensional array from an object attribute as number in PHP

You need to use foreach twice.

PHP Code:

/* Generating structure */
$rawdata = array(
'',
1,
array(
array(
'TradeID' => 15950315,
'Price' => 0.00000170,
'Type' => 'buy',
'Amount' => 712.85989430,
'Total' => 0.00121368,
'Time' => 1535337908,
),
array(
'TradeID' => 15908375,
'Price' => 0.00000300,
'Type' => 'buy',
'Amount' => 574.71264368,
'Total' => 0.00172673,
'Time' => 1535022882,
)
)
);
$data = (object)$rawdata;
print_r($data);// same output as shown in the question


/** Getting TradeID */

foreach ($data as $key => $value) {
if (is_array($value)) {
foreach ($value as $tradeKey => $tradevalue) {
echo $tradevalue['TradeID'].'<br/>';
}
}
}

Please check output at: https://3v4l.org/d3KbN



Related Topics



Leave a reply



Submit