I have this nested array:
Array
(
[id] => 1
[name] => Group 1
[0] => Array
(
[id] => 1
[name] => Group 1
)
[1] => Array
(
[id] => 2
[name] => Group 2
)
[2] => Array
(
[id] => 7
[name] => Group 7
)
)
And I would like to extract sub arrays [0], [1], and [2] in one single array BUT following this format:
array(
[id] => [name]
)
In other words I would like to have this result:
Array
(
[1] => Group 1
[2] => Group 2
[7] => Group 7
)
*Note: I tried with Set::classicExtract($my_array['Group'], '{n}.name'); but I can't figure out how to get group.id as a key for my array. Any guidance will be appreciated.
This should work for you:
(Here I first array_filter() all values out, which don't have a numeric key. After this I simply array_combine() the id column with the name column which I get with array_column())
<?php
$result = array_filter($arr, function($k){
return is_numeric($k);
}, ARRAY_FILTER_USE_KEY);
$result = array_combine(array_column($result, "id"), array_column($result, "name"));
print_r($result);
?>
output:
Array ( [1] => Group 1 [2] => Group 2 [7] => Group 7 )
I figured out in case if someone else will be interested. Thanks a lot for the quick answers but I was looking on a solution using CakePHP.
Set::combine($my_array, '{n}.id', '{n}.name'); did the trick, cheers!
Related
Here is my Sample table:
Here is My Query:
SELECT * FROM table
Result for above query is:
Array
(
[0] => Array
(
[id] => 1
[name] => a
[country] => x
)
[1] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
I need to get the field names in the first element of the array.
Expected result array is:
Array
(
[0] => Array
(
[id] => id
[name] => name
[country] => country
)
[1] => Array
(
[id] => 1
[name] => a
[country] => x
)
[2] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
How can I modify the Query to get this result?
Thanks in advance...
Display issues should generally be dealt with in application code, not SQL queries. If you have your values in an array (called $results, say), you can use this code to add the desired entry:
array_unshift($results, array_combine(array_keys($results[0]), array_keys($results[0])));
print_r($results);
Output:
Array
(
[0] => Array
(
[id] => id
[name] => name
[country] => country
)
[1] => Array
(
[id] => 1
[name] => a
[country] => x
)
[2] => Array
(
[id] => 2
[name] => b
[country] => y
)
)
Demo on 3v4l.org
SELECT 'id' id, 'name' name, 'country' country
UNION ALL
SELECT id, name, country FROM table
ORDER BY 'id' != id
One way to do it with array_keys, array_combine and array_unshift. If I were you I will get the SQL result as $array variable and do some processing on php side like below on query side.
<?php
$array = [['id'=>1,'name'=>'a','country'=>'x'],['id'=>1,'name'=>'b','country'=>'y']];
$keys = array_keys($array[0]);
$first = array_combine($keys,$keys);
array_unshift($array,$first);
print_r($array);
?>
WORKING DEMO: https://3v4l.org/IqZVk
I have a query which fetches all comments for question and answers in one page (each page is containing at least one post (question), and maybe plus one or more posts (answers)). Now I need to separate comments according to its own question or answers.
The result of my query (fetching all comments) is something like this:
while ($results = $sth->fetch(PDO::FETCH_ASSOC)) {
echo '<pre>';
print_r($results);
}
/* Output:
Array
(
[id] => 1
[post_id] => 1
[content] => Have you any tried so far?
)
Array
(
[id] => 2
[post_id] => 3
[content] => Great answer, upvote
)
Array
(
[id] => 3
[post_id] => 3
[content] => That semicolon is redundant in line 5. Pplease edit it
)
Array
(
[id] => 4
[post_id] => 2
[content] => Won't work ...!
)
Array
(
[id] => 5
[post_id] => 3
[content] => #alex You are right thanks, Edited.
)
So, as you see in the output, all comments are mixed.. Now I need to classify them. Something like this:
/* NewOutput:
[1] => Array
post_id ^ (
[0] => Array
(
[id] => 1
[content] => Have you any tried so far?
)
)
[2] => Array
post_id ^ (
[0] => Array
(
[id] => 4
[content] => Won't work ...!
)
)
[3] => Array
post_id ^ (
[0] => Array
(
[id] => 2
[content] => Great answer, upvote
)
[1] => Array
(
[id] => 3
[content] => That semicolon is redundant in line 5. Pplease edit it
)
[2] => Array
(
[id] => 5
[content] => #alex You are right thanks, Edited.
)
)
Well, Now I want to know, how can I create a nested array according to the value of specific key in the array? exactly like above.
Try this...
$output = array();
foreach($results as $r){
$key = "post_".$r["post_id"];
if(!array_key_exists($key, $output)){
$output[$key] = array();
}
array_push($output[$key], array("id"=>$k["id"], "content"=>$k["content"]));
}
print_r($output);
I have trying to create a list of manufactures with a count.
This is my query
$query = $this->pdo->prepare('SELECT manufacture, count(*) AS count
FROM listed_watches
GROUP BY manufacture');
But when i do print_r, its duplicating the results. It is showing "manufacture" and "count" but its also showing [0] and [1], how come?
I just want it to show [manufacture], [count]
Array
(
[0] => Array
(
[manufacture] => Audemars Piguet
[0] => Audemars Piguet
[count] => 2
[1] => 2
)
[1] => Array
(
[manufacture] => Bell and Ross
[0] => Bell and Ross
[count] => 3
[1] => 3
)
[2] => Array
(
[manufacture] => Bulova
[0] => Bulova
[count] => 1
[1] => 1
)
)
try this :
$result = $query->fetchAll(PDO::FETCH_ASSOC);
You can use by default this fetching method in the initialization of your connection with :
$pdo_options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;
Simple question that I am struggling to find answer for. I have an array as follows:
Array (
[0] => Array ( [0] => 2014-05-14 02:11:16 [1] => 1 )
[1] => Array ( [0] => 2014-05-19 05:05:17 [1] => 76 )
[2] => Array ( [0] => 2014-05-20 00:28:41 [1] => 35 )
[3] => Array ( [0] => 2014-05-21 01:24:01 [1] => 25 )
)
All I need to do is count how many Arrays there are.
The answer based on the above would be 4 (0,1,2&3).
I am positive this is a very simple thing but I cannot fathom how - any and all suggestions welcomed.
Using count() should work for you :
$num = count($array);
Given this array:
Array
(
[0] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[1] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[2] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
[3] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
)
The ssm_featured_post_id value corresponds to the value of the array items in the second array.
I want to order the first array items in the same order as the items in the second array
Array
(
[1] => 63
[0] => 70
[3] => 1
[2] => 49
)
so the result after sorting would be
Array
(
[0] => Array
(
[title] => sdfsfsdf
[ssm_featured_post_id] => 63
)
[1] => Array
(
[title] => this is the newest post
[ssm_featured_post_id] => 70
)
[2] => Array
(
[title] => Hello world!
[ssm_featured_post_id] => 1
)
[3] => Array
(
[title] => test
[ssm_featured_post_id] => 49
)
)
The simpler way would be to use usort and write a function that uses the second table to compare two values from first table.
You may want to check out array_multisort, particularly the third example given. The idea is that you create arrays based on the "columns" of the multidimensional array, then sort them simultaneously, and put the result back in the original array.