PHP - generate multi-dimentional array - php

This is my array:
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
I've to convert this array into multi-dimentional array as below:
$arr = array(
'jan-2015' => array(
0 => array(
'title' => 'test1',
'count' => 4,
),
1 => array(
'title' => 'test2',
'count' => 10,
),
),
'jun-2015' => array(
0 => array(
'title' => 'test3',
'count' => 14,
),
),
'july-2015' => array(
0 => array(
'title' => 'test4',
'count' => 45,
),
),
);
I've tried to make it as expected but unfortunately i can't make this.
Is any other solutions for this?

According to your data structure :
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
try this:
$newArray = array();
foreach($arr as $key => $val) {
$newArray[$val['month']][] = $val;
}
echo '<pre>'.print_r($newArray,1).'</pre>';
Output:
Array
(
[jan-2015] => Array
(
[0] => Array
(
[title] => test1
[count] => 4
[month] => jan-2015
)
[1] => Array
(
[title] => test2
[count] => 10
[month] => jan-2015
)
)
[jun-2015] => Array
(
[0] => Array
(
[title] => test3
[count] => 14
[month] => jun-2015
)
)
[july-2015] => Array
(
[0] => Array
(
[title] => test4
[count] => 45
[month] => july-2015
)
)
)

You could use this function:
function transform($input) {
// Extract months, and use them as keys, with value set to empty array
// The array_fill_keys also removes duilicates
$output = array_fill_keys(array_column($input, 'month'), array());
foreach ($input as $element) {
$copy = $element;
// remove the month key
unset($copy["month"]);
// assign this to the month key in the output
$output[$element["month"]][] = $copy;
}
return $output;
}
Call it like this:
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
print_r (transform($arr));
Output:
Array
(
[jan-2015] => Array
(
[0] => Array
(
[title] => test1
[count] => 4
)
[1] => Array
(
[title] => test2
[count] => 10
)
)
[jun-2015] => Array
(
[0] => Array
(
[title] => test3
[count] => 14
)
)
[july-2015] => Array
(
[0] => Array
(
[title] => test4
[count] => 45
)
)
)

By using answer of #Girish Patidar, You can achieve this by:
$outputArr = array();
$to_skip = array();
foreach($arr as $row){
$to_skip = $row;
unset($to_skip['month']);
$outputArr[$row['month']][] = $to_skip;
}
echo "<pre>";
print_r($outputArr);
die;

There could many way to do this. Please try this one if it works for you
<?php
$newArr=NULL;
foreach($arr as $array)
{
$temp=NULL;
$temp['title']=$array['title'];
$temp['count']=$array['count'];
$newArr[$array['month']][]=$temp;
}
var_dump($newArr);
?>

Related

PHP : How to make parallel arrays from multidimentional array

I have this type of array
$arr = array(
0 => array(
0 => array(
'name' => 'test1',
'country' => 'abc'
)
1 => array(
'name' => 'test2',
'country' => 'xyz'
)
)
1 => array(
'name' => 'test3',
'country' => 'pqr'
)
);
How can I make all arrays as parallel arrays. So that all sub arrays are parallel to each other without using any loop.
Like this
$arr = array(
0 => array(
'name' => 'test1',
'country' => 'abc'
)
1 => array(
'name' => 'test2',
'country' => 'xyz'
)
2 => array(
'name' => 'test3',
'country' => 'pqr'
)
);
Any help is much appreciated. !
A dynamic version of Nigel's code would be to loop the array and merge each subarray.
$new = [];
foreach($arr as $subarr){
$new = array_merge($new, $subarr);
}
var_dump($new);
https://3v4l.org/np2ZD
You could simply merge the arrays...
$out = array_merge($arr[0], [$arr[1]]);
print_r($out);
Which gives...
Array
(
[0] => Array
(
[name] => test1
[country] => abc
)
[1] => Array
(
[name] => test2
[country] => xyz
)
[2] => Array
(
[name] => test3
[country] => pqr
)
)

Finding duplicate values in a multidimensional array for Search Method

My Code was :
$data = array();
foreach ($table as $key => $var) {
$data[] = ['id' => $var->id, 'value' => $var->designation];
}
My Data array should be like this
array (
0 => array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 22,
'value' => 'laravel developer',
),
3 => array (
'id' => 23,
'value' => 'laravel casts',
),
4 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
I need only one value i tried all the php core library function output:
array (
0 =>
array (
'id' => 27,
'value' => 'laravel',
),
1 => array (
'id' => 1,
'value' => 'laravel tester',
),
2 => array (
'id' => 23,
'value' => 'laravel casts',
),
3 => array (
'id' => 24,
'value' => 'laravel developer',
),
)
Based on the name only i need to remove duplicate bacause in my search bar it shows repeated mode.
You can use array_unique, wich saves indexes, and get the result by array_intersect_key
$temp = array_unique(array_column($arr, 'value'));
$res = array_intersect_key($arr, $temp);
print_r($res);

php, 3 array to one array merge alternative

I have 3 arrays. These inter-related. I want to combine these statements.
<?php
$post = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1'
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2'
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3'
),
);
$user = array(
array (
'id' => 1,
'name' => 'Mark'
),
array (
'id' => 2,
'name' => 'Selena'
)
);
$post_user = array(
array (
'id' => 1,
'post_id' => 1,
'user_id' => 1
),
array (
'id' => 2,
'post_id' => 2,
'user_id' => 1
),
array (
'id' => 3,
'post_id' => 3,
'user_id' => 2
),
);
$merge = array();
foreach($posts_user as $data){
foreach ($posts as $post){
if($data['post_id'] == $post['id'] ){
foreach ($users as $user){
if($data['user_id'] == $user['id']){
$post['user'] = $user;
$merge['post'][] = $post;
}
}
}
}
}
print_r($merge);
I want to be as follows. How can I do it?
I want results.
$merge = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1',
'user' => array (
'id' => '1',
'name' => 'Mark'
),
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2',
'user' => array (
'id' => '1',
'name' => 'Mark'
),
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3',
'user' => array (
'id' => '2',
'name' => 'Selena'
),
),
);
Is there another alternative? For example; How do I do with them?
array_walk_recursive(), ArrayIterator(), RecursiveArrayIterator()
I tried to do.
<?php
$post = array(
array (
'id' => 1,
'title' => 'Title 1',
'content' => 'Content 1'
),
array (
'id' => 2,
'title' => 'Title 2',
'content' => 'Content 2'
),
array (
'id' => 3,
'title' => 'Title 3',
'content' => 'Content 3'
),
);
$user = array(
array (
'id' => 1,
'name' => 'Mark'
),
array (
'id' => 2,
'name' => 'Selena'
)
);
$post_user = array(
array (
'id' => 1,
'post_id' => 1,
'user_id' => 1
),
array (
'id' => 2,
'post_id' => 2,
'user_id' => 1
),
array (
'id' => 3,
'post_id' => 3,
'user_id' => 2
),
);
$newuser = array();
foreach($post_user as $data){
foreach ($user as $users){
if($data['user_id'] == $users['id']){
$newuser[] = $users;
}
}
}
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_ASSOC);
$mi->attachIterator(new ArrayIterator($post),1);
$mi->attachIterator(new ArrayIterator($newuser),'user');
$newArray = array();
foreach($mi as $details) {
$newArray[] = $details;
}
print_r($newArray);
Results
Array
(
[0] => Array
(
[1] => Array
(
[id] => 1
[title] => Title 1
[content] => Content 1
)
[user] => Array
(
[id] => 1
[name] => Mark
)
)
[1] => Array
(
[1] => Array
(
[id] => 2
[title] => Title 2
[content] => Content 2
)
[user] => Array
(
[id] => 1
[name] => Mark
)
)
[2] => Array
(
[1] => Array
(
[id] => 3
[title] => Title 3
[content] => Content 3
)
[user] => Array
(
[id] => 2
[name] => Selena
)
)
)

PHP merge associative arrays by replacing same keys and adding new

I have an array of variable size structured like this (categories is only one of the keys inside data):
print_r($json[123]["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
)
print_r($json[456]["data"]["categories"]);
array(
array(
'id' => '21',
'description' => 'Downloadable Content'
),
array(
'id' => '1',
'description' => 'Multi-player'
)
)
Now, I want to merge these sub-arrays (they can be in variable number) and have all keys added and replaced. I've tried array_merge but it replaces the keys without adding new ones.
In this case I need to obtain this array:
print_r($merged["data"]["categories"]);
array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
),
array(
'id' => '21',
'description' => 'Downloadable Content'
)
)
Any help?
Edit:
I think I didn't expressed myself well enough. $json[$id]["data"] has multiple keys I want to merge (categories is just an example). Also the number of $json[$id] keys is variable
Edit2:
The arrays can have duplicate values, and the depth of the keys can be variable. I need to get something like array_merge_recursive() but with same values replaced.
Edit3:
This is the current array. http://pastebin.com/7x7KaAVM I need to merge all keys that have sub-arrays
Try below code:
$json = array(
'123' => array('data' => array('categories' => array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
))
),
'456' => array('data' => array('categories' => array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
))
),
);
//print_r($json);
$merged = array();
foreach($json as $j1)
{
foreach($j1 as $j2)
{
foreach($j2 as $key => $j3)
{
foreach($j3 as $j4)
{
$merged[$key][] = $j4;
}
}
}
}
print_r($merged);
Result:
Array
(
[categories] => Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
)
)
Demo:
http://3v4l.org/X61bE#v430
Try this . To generalize I have added some more arrays.
<?php
$merged_array = array();
$final_array = array();
$json[123]["data"]["categories"] = array(
array(
'id' => '2',
'description' => 'Single-player'
),
array(
'id' => '1',
'description' => 'Multi-player'
),
array(
'id' => '9',
'description' => 'Co-op'
),
array(
'id' => '22',
'description' => 'Steam Achievements'
),
array(
'id' => '28',
'description' => 'Full controller support'
)
);
$json[456]["data"]["categories"] = array(
array(
'id' => '21',
'description' => 'Downloadable Content'
)
);
$json[786]["data"]["categories"] = array(
array(
'id' => '31',
'description' => 'Downloadable Content'
)
);
$json[058]["data"]["categories"] = array(
array(
'id' => '41',
'description' => 'Downloadable Content'
)
);
foreach($json as $key=>$value){
array_push($merged_array,$json[$key]["data"]["categories"]);
}
foreach($merged_array as $value){
foreach($value as $val){
array_push($final_array,$val);
}
}
print_r($final_array);
?>
RESULT
Array
(
[0] => Array
(
[id] => 2
[description] => Single-player
)
[1] => Array
(
[id] => 1
[description] => Multi-player
)
[2] => Array
(
[id] => 9
[description] => Co-op
)
[3] => Array
(
[id] => 22
[description] => Steam Achievements
)
[4] => Array
(
[id] => 28
[description] => Full controller support
)
[5] => Array
(
[id] => 21
[description] => Downloadable Content
)
[6] => Array
(
[id] => 31
[description] => Downloadable Content
)
[7] => Array
(
[id] => 41
[description] => Downloadable Content
)
)

how to insert batch codeigniter

Ii everyone, I have an post data in array like this, I'm so confused how create the logic in controller:
POST Data:
Array
(
[nama_agenda] => blalala
[kilasan] => asdsadsadasd
[tgl_agenda] => 2014-06-01
[jam_agenda] => 13:27:30
[komisi] => Array
(
[0] => 1
[1] => 3
)
[fraksi] => Array
(
[0] => 1
[1] => 4
)
[badan] => Array
(
[0] => 1
[1] => 3
)
[anggota] => Array
(
[0] => 1
[1] => 4
)
[bagian] => Array
(
[0] => 2
[1] => 4
)
)
My question is how to insert into database, in controller? Thank's for help. I'll appreciate.
Since your structure is not well formed for insert_batch method. Your need to restructure it first. Consider this example:
$original_values = array(
'nama_agenda' => 'blalala',
'kilasan' => 'asdsadsadasd',
'tgl_agenda' => '2014-06-01',
'jam_agenda' => '13:27:30',
'komisi' => array(1, 3),
'fraksi' => array(1, 4),
'badan' => array(1, 3),
'anggota' => array(1, 4),
'bagian' => array(2, 4),
);
$new_values = array();
for($x = 0, $size = count($original_values['komisi']); $x < $size; $x++) {
foreach($original_values as $key => &$value) {
if(!is_array($value)) {
$new_values[$x][$key] = $value;
} else {
$new_values[$x][$key] = array_shift($value);
}
}
}
echo '<pre>';
print_r($new_values);
Should yield something like:
Array
(
[0] => Array
(
[nama_agenda] => blalala
[kilasan] => asdsadsadasd
[tgl_agenda] => 2014-06-01
[jam_agenda] => 13:27:30
[komisi] => 1
[fraksi] => 1
[badan] => 1
[anggota] => 1
[bagian] => 2
)
[1] => Array
(
[nama_agenda] => blalala
[kilasan] => asdsadsadasd
[tgl_agenda] => 2014-06-01
[jam_agenda] => 13:27:30
[komisi] => 3
[fraksi] => 4
[badan] => 3
[anggota] => 4
[bagian] => 4
)
)
Now you can use insert_batch() method.
$this->db->insert_batch('table_name', $new_values);
get all the data in array using $this->input->post() eg:
$bagian= $this->input->post('bagian');
and create a array()
$arr=array(
'db_table_col_1'=>$bagian,
'db_table_col_2'=>$post_data,
'db_table_col_2'=>$post_data
);
pass this array to model
$this->your_model_name->function_name($arr);
then in model create function
function_name($arg){
$this->db->insert('table_name',$arr);
}
if you want to insert multiple row then just use foreach
<?php
$arr1=array();
$arr= array(
'nama_agenda' => 'blalala',
'kilasan' => 'asdsadsadasd',
'tgl_agenda' => '2014-06-01',
'jam_agenda' => '13:27:30',
'komisi' => array
(
'0' => 1,
'1' => 3
),
'fraksi' => array
(
'0' => 1,
'1' => 4
),
'badan' => array
(
'0' => 1,
'1' => 3
),
'anggota' => array
(
'0' => 1,
'1' => 4
),
'bagian' => array
(
'0' => 2,
'1' => 4
)
);
foreach($arr as $row){
if(is_array($row)){
array_push($arr1,$row);
}
}
print_r($arr1);
and then pass this array to batch_insert
function_name($arr1){
$this->db->insert_batch('table_name',$arr1);
}
note arr1 syntax must be
$arr1 = array(
array(
'table_col1' => 'My title' ,
'table_col2' => 'My Name'
),
array(
'table_col1' => 'other title' ,
'table_col2' => 'other Name'
)
);
?>

Categories