I have the following array:
$original_array = [
[
"group" => "g1",
"department" => "d1",
"team" => null,
"data_col1" => "some_data1",
"data_col2" => "some_data2"
],
[
"group" => "g1",
"department" => "d1",
"team" => null,
"data_col1" => "some_data3",
"data_col2" => "some_data4"
],
[
"group" => "g1",
"department" => "d1",
"team" => "t3",
"data_col1" => "some_data5",
"data_col2" => "some_data6"
],
[
"group" => "g4",
"department" => "d6",
"team" => "t11",
"data_col1" => "some_data7",
"data_col2" => "some_data8"
]
];
I want to "group" the array into common group/department/team.
I tried the following - I created a new array, then I assign the relevant keys (group/department/team) and otherwise I assign the value:
$new_array = [];
foreach ($original_array as $row) {
foreach ($row as $key => $value) {
if ($key === "group" || $key === "department" || $key === "team") {
$new_array[$key] = $value;
} else {
$new_array[] = $value;
}
}
}
The expected result is:
[
0 => [
group => "g1",
department => "d1",
team => null,
data => [
0 => [data_col1 => "some_data1", data_col2 => "some_data2"],
1 => [data_col1 => "some_data3", data_col2 => "some_data4"]
],
1 => [
group => "g1",
department => "d1",
team => "t3",
data => [
0 => [data_col1 => "some_data5", data_col2 => "some_data6"]
],
2 => [
group => "g4",
department => "d6",
team => "t11",
data => [
0 => [data_col1 => "some_data7", data_col2 => "some_data8"]
]
]
But the result is:
[
"group" => "g4"
"department" => "d6"
"team" => "t11"
0 => "some_data1"
1 => "some_data2"
2 => "some_data3"
3 => "some_data4"
4 => "some_data5"
5 => "some_data6"
6 => "some_data7"
7 => "some_data8"
]
why? Looks like it only assigns values to the last row in the original array
This code should give the good result :
foreach ($original_array as $row) {
$key = md5($row['group'] . $row['department'] . $row['team']);
if(!isset($new_array[$key])){
$new_array[$key] = [
'group' => $row['group'],
'department' => $row['department'],
'team' => $row['team'],
'data' => [],
];
}
$data = [];
foreach ($row as $k => $v) {
if ($k !== "group" AND $k !== "department" AND $k !== "team") {
$data[$k] = $v;
}
}
$new_array[$key]['data'][] = $data;
}
This give me that :
Array
(
[336a26e12ea082ff360f8482891e137c] => Array
(
[group] => g1
[department] => d1
[team] =>
[data] => Array
(
[0] => Array
(
[data_col1] => some_data1
[data_col2] => some_data2
)
[1] => Array
(
[data_col1] => some_data3
[data_col2] => some_data4
)
)
)
[f06623fdd946c84a0c0c30378ba3f6f5] => Array
(
[group] => g1
[department] => d1
[team] => t3
[data] => Array
(
[0] => Array
(
[data_col1] => some_data5
[data_col2] => some_data6
)
)
)
[0b284dc4a7dac108ec2b1205a6484056] => Array
(
[group] => g4
[department] => d6
[team] => t11
[data] => Array
(
[0] => Array
(
[data_col1] => some_data7
[data_col2] => some_data8
)
)
)
)
Related
I have a requirement to gather all the comments of posts and show them in CSV.
For that, I have this kind of array ( post-wise ) :
[21069] => Array
(
[0] => Array
(
[comment_author] => author1
[comment_content] => for testing
)
[1] => Array
(
[comment_author] => author2
[comment_content] => this is
)
[2] => Array
(
[comment_author] => author3
[comment_content] => good
)
)
[21070] => Array
(
[0] => Array
(
[comment_author] => author4
[comment_content] => file reading
)
[1] => Array
(
[comment_author] => author5
[comment_content] => hi
)
)
I want to show this data in CSV , for that, I need to convert the array into a string in this form.
[0]=>Array
(
['author_comment']=>'author1:for testing|author2:this is|author3:good'
)
[1]=>Array
(
['author_comment']=>'author4:file reading|author5:hi'
)
I am not getting how do I use implode function.
EDIT 1 : Please find var_export in below code.
$newArray = [
'21069' => [
'0' => [
'comment_author' => 'author1',
'comment_content' => 'for testing'
],
'1' => [
'comment_author' => 'author2',
'comment_content' => 'this is'
],
'2' => [
'comment_author' => 'author3',
'comment_content' => 'good'
],
],
'21070' => [
'0' => [
'comment_author' => 'author4',
'comment_content' => 'file reading'
],
'1' => [
'comment_author' => 'author5',
'comment_content' => 'hi '
],
],
];
I Manage to concat the comments using below code.
$new1Array = [];
foreach($newArray as $key => $value){
foreach($value as $value1) {
$new1Array[$key][]['author_comment'] = $value1['comment_author'].':'.$value1['comment_content'];
}
}
$finalArraynew = [];
foreach($new1Array as $key => $value) {
$finalArraynew[$key][] = implode('|', array_map(function ($entry) {
return $entry['author_comment'];
}, $value));
}
I have an API which I'm trying to get an ID from, but the output is a mutli-dimensional array and I can't grab just the ID.
This is what the array looks like:
Array ( [{ data] => [ { account_key [ is_owner] => true [ id] => 1 [ name] => test [ display_name] => test [ balance] => 0 [ paid_to_date] => 0 [ updated_at] => 1577724986 [ archived_at] => null [ address1] => [ address2] => [ city] => [ state] => [ postal_code] => [ country_id] => 0 [ work_phone] => [ private_notes] => [ public_notes] => [ last_login] => [ website] => [ industry_id] => 0 [ size_id] => 0 [ is_deleted] => false [ payment_terms] => 30 [ vat_number] => [ id_number] => [ language_id] => 0 [ currency_id] => 0 [ custom_value1] => [ custom_value2] => [ invoice_number_counter] => 1 [ quote_number_counter] => 1 [ task_rate] => 0 [ shipping_address1] => [ shipping_address2] => [ shipping_city] => [ shipping_state] => [ shipping_postal_code] => [ shipping_country_id] => 0 [ show_tasks_in_portal] => true [ send_reminders] => true [ credit_number_counter] => 1 [ custom_messages] => {} [ contacts] => [ { account_key [ is_owner] => true [ id] => 1 [ first_name] => test [ last_name] => test [ email] => me#idontlikespam.com [ contact_key] => mq1dzpkqznfgtqwhdwt9nte1ohmvsju1 [ updated_at] => 1577724986 [ archived_at] => null [ is_primary] => true [ phone] => [ last_login] => [ send_invoice] => true [ custom_value1] => [ custom_value2] => } ] } ] [ meta] => { pagination [ count] => 1 [ per_page] => 15 [ current_page] => 1 [ total_pages] => 1 [ links] => [] } } } )
This is the code I have so far to split it out:
$clients = str_replace('"', "", $clients);
$convert_to_array = explode(',', $clients);
for($i=0; $i < count($convert_to_array ); $i++){
$key_value = explode(':', $convert_to_array [$i]);
$end_array[$key_value [0]] = $key_value [1];
}
print_r($end_array);
foreach ($end_array as $key => $item)
{
echo "[" . $key . "] => " . $item . "<br />";
if ($key = ' id')
{
echo $item;
}
}
It's just the final step I'm missing, it's probably something easy, but I'm still running on a Christmas brain...
New data:
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv [is_owner] => 1 [id] => 1 [name] => test [display_name] => test [balance] => 0 [paid_to_date] => 0 [updated_at] => 1577724986 [archived_at] => [address1] => [address2] => [city] => [state] => [postal_code] => [country_id] => 0 [work_phone] => [private_notes] => [public_notes] => [last_login] => [website] => [industry_id] => 0 [size_id] => 0 [is_deleted] => [payment_terms] => 30 [vat_number] => [id_number] => [language_id] => 0 [currency_id] => 0 [custom_value1] => [custom_value2] => [invoice_number_counter] => 1 [quote_number_counter] => 1 [task_rate] => 0 [shipping_address1] => [shipping_address2] => [shipping_city] => [shipping_state] => [shipping_postal_code] => [shipping_country_id] => 0 [show_tasks_in_portal] => 1 [send_reminders] => 1 [credit_number_counter] => 1 [custom_messages] => {} [contacts] => Array ( [0] => stdClass Object ( [account_key] => jvg7qgtw2btrlmpigrq2zpco48eegxvv [is_owner] => 1 [id] => 1 [first_name] => test [last_name] => test [email] => ema#il.co.uk [contact_key] => mq1dzpkqznfgtqwhdwt9nte1ohmvsju1 [updated_at] => 1577724986 [archived_at] => [is_primary] => 1 [phone] => [last_login] => [send_invoice] => 1 [custom_value1] => [custom_value2] => ) ) ) ) [meta] => stdClass Object ( [pagination] => stdClass Object ( [total] => 1 [count] => 1 [per_page] => 15 [current_page] => 1 [total_pages] => 1 [links] => Array ( ) ) ) )
In Array you can access to values by [index].
In stdObject you can access to properties by -> because it is object.
So
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [account_key]
Is
Object->(property data has value array)[index 0 in array and is object]->(property account_key)
On your data you need this.
foreach ($end_array->data as $key => $item)
{
echo $item->id . "<br />";
}
or if you know if data can have always one item you can access with this shortcut
echo $end_array->data[0]->id;
I have a array like this :
Array
(
[0] => Array
(
[id] => 1
[child_name] => "Emma"
[parent_name] => "Mr Brown"
)
[1] => Array
(
[id] => 2
[child_name] => "John"
[parent_name] => "Mr Brown"
)
[2] => Array
(
[id] => 3
[child_name] => "Joseph"
[parent_name] => "Mr Thomas"
)
[3] => Array
(
[id] => 4
[child_name] => "Pretty"
[parent_name] => "Mr Thomas"
)
[4] => Array
(
[id] => 5
[child_name] => "Raphel"
[parent_name] => "Mr Brown"
)
[5] => Array
(
[id] => 6
[child_name] => "Tommy"
[parent_name] => "Mr Thomas"
)
[6] => Array
(
[id] => 7
[child_name] => "Tim"
[parent_name] => "Mr Thomas"
)
)
From this array I want to generate a view like this :
The parent_name field becomes the MainCategory and the child_name becomes the subcategory. The names have checkboxes in front of them.
How do I achieve this? I don't have much experience in php. I code in node js but this task needs this to be done in php.
How do I do this?
Try this, Here i use simple index() method and pass data to view file as an example.
You can use below code and test in your codeigniter.
Hope this will work for you.
Welcome.php (Controller)
public function index()
{
$array = [
[
'id' => 1,
'child_name' => "Emma",
'parent_name' => "Mr Brown",
],
[
'id' => 2,
'child_name' => "John",
'parent_name' => "Mr Brown",
],
[
'id' => 3,
'child_name' => "Joseph",
'parent_name' => "Mr Thomas",
],
[
'id' => 4,
'child_name' => "Pretty",
'parent_name' => "Mr Thomas",
],
[
'id' => 5,
'child_name' => "Raphel",
'parent_name' => "Mr Brown",
],
[
'id' => 6,
'child_name' => "Tommy",
'parent_name' => "Mr Thomas",
],
[
'id' => 7,
'child_name' => "Tim",
'parent_name' => "Mr Thomas",
],
[
'id' => 8,
'child_name' => "William",
'parent_name' => "",
],
];
$resultArray = [];
foreach ($array as $key => $value) {
if($value['parent_name'] != ''){
$resultArray[$value['parent_name']][] = $value;
}else{
$resultArray[$value['child_name']] = [];
}
}
$data = ['resultArray' => $resultArray];
$this->load->view('welcome_message', $data);
}
welcome_message.php (view)
<div>
<form name='sample'>
<?php
foreach ($resultArray as $parentKey => $parentValue) {
echo '<input type="checkbox" name="'.$parentKey.'" value="'.$parentKey.'">'.$parentKey.'<br>';
foreach ($parentValue as $childKey => $childValue) {
echo ' <input type="checkbox" name="'.$childValue['child_name'].'" value="'.$childValue['child_name'].'">'.$childValue['child_name'].'<br>';
}
}
?>
</form>
</div>
Output
Check this,
Loop your array, it will capture all values with same parent_name.
$result = [];
foreach ($array as $key => $value) {
$result[$value['parent_name']][] = $value['child_name']; // loop your array
}
It should work.
The task is to merge ("inexpensively") two arrays, which have matching key-value pairs of subarrays. E.g.:
Array 1:
Array
(
[0] => Array
(
[count] => 1
[da_table] => article
[da_class] => classes\elements\tables\Article
[da_page_class] => Page_Article
)
[1] => Array
(
[count] => 2
[da_table] => client_contract_specification_price
[da_class] => classes\elements\tables\ClientContractSpecificationPrice
[da_page_class] => Page_ClientContractSpecification
)
[2] => Array
(
[count] => 2
[da_table] => supplier
[da_class] => classes\elements\tables\Supplier
[da_page_class] => Page_Supplier
)
)
Array 2:
Array
(
[0] => Array
(
[name] => Articles
[name_short] =>
[da_page_class] => Page_Article
)
[1] => Array
(
[name] => Client contract specifications
[name_short] => cc_specifications
[da_page_class] => Page_ClientContractSpecification
)
[2] => Array
(
[name] => Suppliers
[name_short] =>
[da_page_class] => Page_Supplier
)
)
How to merge the two above arrays by a matching [da_page_class] => ... pairs, so the resulting array will contain both key-values of the first and the second array, i.e.:
...
[0] => Array
(
[count] => 1
[da_table] => article
[da_class] => classes\elements\tables\Article
[da_page_class] => Page_Article
[name] => Articles
[name_short] =>
)
...
Additional requirements:
Subarrays may come in random order. Also, there can be "orphans", which contain values of ['da_page_class'], but have no match in another array. These should be ignored.
Well, you simply iterate over the array elements and combine them:
<?php
$data1 = [
[
'count' => 1,
'da_table' => 'article',
'da_class' => 'classes\elements\tables\Article',
'da_page_class' => 'Page_Article'
],
[
'count' => 2,
'da_table' => 'client_contract_specification_price',
'da_class' => 'classes\elements\tables\ClientContractSpecificationPrice',
'da_page_class' => 'Page_ClientContractSpecification'
],
[
'count' => 2,
'da_table' => 'supplier',
'da_class' => 'classes\elements\tables\Supplier',
'da_page_class' => 'Page_Supplier'
]
];
$data2 = [
[
'name' => 'Articles',
'name_short' => null,
'da_page_class' => 'Page_Article'
],
[
'name' => 'Client contract specifications',
'name_short' => 'cc_specifications',
'da_page_class' => 'Page_ClientContractSpecification'
],
[
'name' => 'Suppliers',
'name_short' => null,
'da_page_class' => 'Page_Supplier'
]
];
$output = [];
for ($i=0; $i<count($data1); $i++) {
$output[$i] = array_merge($data1[$i], $data2[$i]);
}
print_r($output);
The output obviously is:
Array
(
[0] => Array
(
[count] => 1
[da_table] => article
[da_class] => classes\elements\tables\Article
[da_page_class] => Page_Article
[name] => Articles
[name_short] =>
)
[1] => Array
(
[count] => 2
[da_table] => client_contract_specification_price
[da_class] => classes\elements\tables\ClientContractSpecificationPrice
[da_page_class] => Page_ClientContractSpecification
[name] => Client contract specifications
[name_short] => cc_specifications
)
[2] => Array
(
[count] => 2
[da_table] => supplier
[da_class] => classes\elements\tables\Supplier
[da_page_class] => Page_Supplier
[name] => Suppliers
[name_short] =>
)
)
Alternatively you could also merge the contents of the elements of the second array into the corresponding elements of the first array. That reduces the memory footprint for large data sets.
Considering the additional requirement you specified in your comment I changed the merge strategy to allow for arbitrary orders of the two sets:
<?php
$data1 = [
[
'count' => 1,
'da_table' => 'article',
'da_class' => 'classes\elements\tables\Article',
'da_page_class' => 'Page_Article'
],
[
'count' => 2,
'da_table' => 'client_contract_specification_price',
'da_class' => 'classes\elements\tables\ClientContractSpecificationPrice',
'da_page_class' => 'Page_ClientContractSpecification'
],
[
'count' => 2,
'da_table' => 'supplier',
'da_class' => 'classes\elements\tables\Supplier',
'da_page_class' => 'Page_Supplier'
]
];
$data2 = [
[
'name' => 'Articles',
'name_short' => null,
'da_page_class' => 'Page_Article'
],
[
'name' => 'Client contract specifications',
'name_short' => 'cc_specifications',
'da_page_class' => 'Page_ClientContractSpecification'
],
[
'name' => 'Suppliers',
'name_short' => null,
'da_page_class' => 'Page_Supplier'
]
];
$output = [];
array_walk($data1, function($entry, $key) use (&$output, $data2) {
$output[$key] = $entry;
foreach($data2 as $cand) {
if ($entry['da_page_class'] == $cand['da_page_class']) {
$output[$key] = array_merge($output[$key], $cand);
}
}
});
print_r($output);
The resulting output obviously is identical to above.
O(m*n) solution:
$result = array();
foreach ($array1 as $value1) {
// do not handle elements without pageclass
if (!array_key_exists('da_page_class', $value1) || !$value1['da_page_class']) {
continue;
}
foreach ($array2 as $value2) {
if (!array_key_exists('da_page_class', $value2) || !$value2['da_page_class']) {
continue;
}
if ($value1['da_page_class'] == $value2['da_page_class']) {
array_push($result, $value1 + $value2)
break;
}
}
}
print_r($result);
O(m+n) solution:
$result = array();
foreach ($array1 as $value) {
// do not handle elements without pageclass
if (!array_key_exists('da_page_class', $value) || !$value['da_page_class']) {
continue;
}
$result[$value['da_page_class']] = $value;
}
foreach ($array2 as $value) {
if (
// do not handle elements without pageclass
!array_key_exists('da_page_class', $value) || !$value['da_page_class'] ||
// do not handle elements that do not exist in array 1
!array_key_exists($value['da_page_class'], $result)
) {
continue;
}
// merge values of this pageclass
$result[$value['da_page_class']] = array_merge($result[$value['da_page_class']], $value);
}
print_r($result);
EDIT: added O(m+n) solution
Example my array
Array (
[0] => Array
(
[product_name] => T-Shirt
[product_id] => 231
[user_id] => 22977
)
[1] => Array
(
[product_name] => Shirt
[product_id] => 220
[user_id] => 22977
)
[2] => Array
(
[product_name] => T-Shirt
[product_id] => 226
[user_id] => 16916
)
[3] => Array
(
[product_name] => Bags
[product_id] => 230
[user_id] => 16916
)
[4] => Array
(
[product_name] => Hats
[product_id] => 233
[user_id] => 22977
)
)
How to generate this array to be
User-Id: 22977
1/ Hats
2/ Shirt
3/ T-Shirt
User-Id: 16916
1/ Bags
2/ T-Shirt
$a = array();
$a[] = array("product_name" => "T-Shirt", "product_id" => 231, "user_id" => 22977);
$a[] = array("product_name" => "Shirt", "product_id" => 220, "user_id" => 22977);
$a[] = array("product_name" => "T-Shirt", "product_id" => 226, "user_id" => 16916);
$a[] = array("product_name" => "Bags", "product_id" => 230, "user_id" => 16916);
$a[] = array("product_name" => "Hats", "product_id" => 233, "user_id" => 22977);
$return = array();
foreach ($a as $key => $value) {
$return[$value["user_id"]][] = $value["product_name"];
}
foreach ($return as $key => $value) {
echo "User-Id: " . $key . "\r\n";
$i = 0;
foreach ($value as $val) {
echo ++$i . "/ " . $val . "\r\n";
}
}
Output will be:
User-Id: 22977
1/ T-Shirt
2/ Shirt
3/ Hats
User-Id: 16916
1/ T-Shirt
2/ Bags
You can use this:
$testarray = array(
array(
"product_name" => 'T-Shirt',
"product_id" => 231,
"user_id" => 22977),
array
(
"product_name" => 'Shirt',
"product_id" => 220,
"user_id" => 22977,
),
array
(
"product_name" => 'T-Shirt',
"product_id" => 226,
"user_id" => 16916,
),
array
(
"product_name" => 'Bags',
"product_id" => 230,
"user_id" => 16916,
),
array
(
"product_name" => 'Hats',
"product_id" => 233,
"user_id" => 22977,
),
);
$newArray = array();
foreach ($testarray as $subArray) {
$newArray[$subArray["user_id"]][] = $subArray['product_name'];
}
var_dump ($newArray);
Output is:
array
22977 =>
array
0 => string 'T-Shirt' (length=7)
1 => string 'Shirt' (length=5)
2 => string 'Hats' (length=4)
16916 =>
array
0 => string 'T-Shirt' (length=7)
1 => string 'Bags' (length=4)
<?php
//initialize array
$array = Array(
'0' => Array
(
'product_name' => 'T-Shirt',
'product_id' => 231,
'user_id' => 22977
),
'1' => Array
(
'product_name' => 'Shirt',
'product_id' => 220,
'user_id' => 22977
),
'2' => Array
(
'product_name' => 'T-Shirt',
'product_id' => 226,
'user_id' => 16916
),
'3' => Array
(
'product_name' => 'Bags',
'product_id' => 230,
'user_id' => 16916
),
'4' => Array
(
'product_name' => 'Hats',
'product_id' => 233,
'user_id' => 22977
)
);
//result will be here
$result = array();
foreach ($array as $key => $value) {
//check if we have keys group or names to avoid errors
if(!isset($value['user_id']) || !isset($value['product_name']))
continue;
//make a key in result array if its not exist
if(!isset($result[$value['user_id']]))
{
$result[$value['user_id']] = array($value['product_name']);
}
else
{
//add a values to key if it exists
$result[$value['user_id']][] = $value['product_name'];
//filter same values
$result[$value['user_id']] = array_values(array_unique($result[$value['user_id']]));
}
}
echo '<pre>';
print_r($result);
echo '</pre>';
?>