Linked Questions
135 questions linked to/from Is there a function to extract a 'column' from an array in PHP?
158 votes
14 answers
459k views
Convert stdClass object to array in PHP [duplicate]
I fetch post_id from postmeta as: $post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')"); when i try print_r($...
74 votes
24 answers
217k views
Convert multidimensional array into single array [duplicate]
I have an array which is multidimensional for no reason /* This is how my array is currently */ Array ( [0] => Array ( [0] => Array ( [plan] => basic ...
168 votes
4 answers
240k views
How to get an array of specific "key" in multidimensional array without looping [duplicate]
Let's assume I have the following multidimensional array (retrieved from MySQL or a service): array( array( [id] => xxx, [name] => blah ), array( [id] =>...
70 votes
4 answers
93k views
array_map inline anonymous function [duplicate]
I tested inline anonymous function with array_map here and it worked but when I tried same with $user_meta it is not working. $user_meta = Array ( [interest] => Array ( [0] => Array ) [type] =&...
45 votes
5 answers
118k views
PHP foreach with Nested Array? [duplicate]
I have a nested array in which I want to display a subset of results. For example, on the array below I want to loop through all the values in nested array[1]. Array ( [0] => Array ( [0] =>...
27 votes
4 answers
65k views
How can I create an array from the values of another array's key? [duplicate]
I have an array as follows: $arr1 = array( 0 => array( 'name' => 'tom', 'age' => 22 ), 1 => array( 'name' => 'nick', 'age' => 18 ) ); However I want to ...
27 votes
8 answers
18k views
In PHP, is there a function that returns an array made up of the value of a key from an array of associative arrays? [duplicate]
I'm sure this question has been asked before, my apologies for not finding it first. The original array: [0] => Array ( [categoryId] => 1 [eventId] => 2 [...
18 votes
7 answers
44k views
PHP array mapping [duplicate]
Is there a cleaner way than foreach to get an array of all "label" values? $methods[0]['label'] = 'test'; $methods[0]['nr'] = 99; $methods[1]['label'] = 'whatever'; $methods[1]['nr'] = 10; ...
31 votes
2 answers
32k views
Get specific element from each sub array [duplicate]
I have a common pattern which Im sure there must be a built-in array function in PHP to handle but just can't see it. I have multiple arrays such as the following: $testArray = array ( '...
15 votes
7 answers
22k views
PHP Get the first value of all arrays in a multidimensional array [duplicate]
Here's a section my multidimensional array: Array ( [0] => Array ( [0] => Height [1] => 40 ) [1] => Array ( [0] => Weight [1] => 15 ) [2] => Array ( [0] => Ctr_Percent [1] =...
9 votes
5 answers
32k views
PHP array_filter to get only one value from an array [duplicate]
I'm trying to exclude foreach-loops and refactor them with array functions. I was under the assumption the code below would give me a result with all first items from the source array. <?php $...
12 votes
2 answers
46k views
getting array values without foreach loop [duplicate]
Is there any way to get all values in one array without using foreach loops, in this example? <?php $foo = array(["type"=>"a"], ["type"=>"b"], ["type"=>"c"]); The output I need is array(...
3 votes
7 answers
8k views
How to extract particular fields from an array [duplicate]
I have an array that looks $articles = array([0] => array('title' => 'When ....', 'description' => '....', 'created' => '...
8 votes
6 answers
24k views
PHP array. Simple split based on key [duplicate]
This is more a request for a quick piece of advice as, I find it very hard to believe there isn't a native function for the task at hand. Consider the following: array => (0) => array("id" =&...
7 votes
3 answers
13k views
Multidimensional Array: How to get all values of a specific key? [duplicate]
I have an multidimensional array which includes IDs and URLs. I would like to output the URLs only. $abstract_details = array( array( 'id' => ...