How to add new array to existing arrray? - php

I've an following array called $title:
Array
(
[pt_id] => 4
[pt_doc_title] => Solapur IT Park
[pt_doc_id] => 1
[delete_flag] =>
[pt_doc_file_iname] => Array.php
)
But I want this array in below form:
Array
(
[0] => Array
(
[pt_doc_id] => 1
[pt_id] => 4
[pt_doc_title] => Solapur IT Park
[pt_doc_file_iname] =>
[pt_doc_added_date] => 1390390546
[pt_doc_updated_date] => 1390390704
)
)
How can I achieve this? Though here array key is zero I'm having another key values in my hand. The issue I'm facing is how to add these key numbers (as 0 is added above)? Please help.

save your array in this format.
$title = array();
$title[] = array(
'pt_doc_id'=>'1',
'pt_id'=>'4',
'pt_doc_title'=>'Solapur IT Park',
'pt_doc_file_iname'=>'',
'pt_doc_added_date'=>'1390390546',
'pt_doc_updated_date'=>'1390390704'
);
set your array format like this.
i hop this is working.

Suppose your array1 & array2
<?php
$array1=Array
(
'pt_id' => '4',
'pt_doc_title' => 'Solapur IT Park'
);
$array2[0]=$array1;
print_r($array2);
?>
Will give you output:
Array
(
[0] => Array
(
[pt_id] => 4
[pt_doc_title] => Solapur IT Park
)
)

Live demo : https://eval.in/93311
use array_push it will auto indexing your array. if you have multi-dimensional array then you can run this steps in a loop.
$arr =array
(
"pt_id" => 4,
"pt_doc_title" =>" Solapur IT Park",
"pt_doc_id" => 1,
"delete_flag" => '',
"pt_doc_file_iname" => "Array.php"
);
$arr['pt_doc_added_date'] = 1390390546; //add your new element
$arr['pt_doc_updated_date'] = 1390390704;
$result = array();
array_push($result,$arr); //push this array in result array
print_r($result);
OUTPUT:
Array
(
[0] => Array
(
[pt_id] => 4
[pt_doc_title] => Solapur IT Park
[pt_doc_id] => 1
[delete_flag] =>
[pt_doc_file_iname] => Array.php
[pt_doc_added_date] => 1390390546
[pt_doc_updated_date] => 1390390704
)
)

Related

split one array have two key split into two array in php

Question :
I have one array have two key or more split into two or more create array based on array in php.
my Array :
array
(
[RAJAHMUNDRY] => Array
(
[unspcp_code] => 46182005
[title] => 3M™ Half Face Reusable Respirator HF-52 with Holder 1700 And Filter 1744
[total] => 2
[head_quarter] => RAJAHMUNDRY
[0] => 2
)
[HYDERABAD] => Array
(
[unspcp_code] => 46182005
[title] => 3M™ 6200 HALF FACE MASK WITH 7093 FILTER
[total] => 2
[head_quarter] => HYDERABAD
[0] => 2
)
)
I want output like this :
output:
array
(
[RAJAHMUNDRY] => Array
(
[unspcp_code] => 46182005
[title] => 3M™ Half Face Reusable Respirator HF-52 with Holder 1700 And Filter 1744
[total] => 2
[head_quarter] => RAJAHMUNDRY
[0] => 2
)
)
)
array(
[HYDERABAD] => Array
(
[unspcp_code] => 46182005
[title] => 3M™ 6200 HALF FACE MASK WITH 7093 FILTER
[total] => 2
[head_quarter] => HYDERABAD
[0] => 2
)
)
I am not sure how you want to store those arrays, but let me help you.
I assume you have a datastructure like this, so one array with multiple values.
array (
key1 => ...values...,
key2 => ...values...,
...
key_n => ...values...
)
And you want something like this, si multiple arrays with single keys well you need to store that array somehow.
array (
key1 => ...values...
)
array (
key2 => ...values...
)
...
array (
key_n => ...values...
)
If you do not know the exact number of arrays, you can't $array1, $array2, ... $array_n and it also not efficent, so you shoudl have an array of arrays. So something like this:
array(
array (
key1 => ...values...
)
array (
key2 => ...values...
)
...
array (
key_n => ...values...
)
)
So you should iterate trough the keys of the input array and then
So the code
<?php
//example input array
$arr = array (
"key1" => "val1",
"key2" => "val2"
);
$keys = array_keys($arr); //get the keys of the input array, see phpdoc
$output = [];
foreach($keys as $key) {
$output[] = array ($arr[$key]);
}
?>
This will output an array of arrays, with single key of the inner array.
If this is not you answer, reply.
Research:
https://www.php.net/manual/en/function.array-keys.php
https://www.php.net/manual/en/control-structures.foreach.php
php.net - arrays manual Example #6 Accessing array elements
Maybe this document will help you
This may also help you
<?php
$stdArray = array(
"foo" => "bar",
42 => 24,
"dimensional" => array(
"fname" => "jon",
"lname" => "doe",
),
"multi" => array(
"RAJAHMUNDRY" => array(
"unspcp_code" => 46182005,
"head_quarter" => "RAJAHMUNDRY",
0 => 2
),
"HYDERABAD" => array(
"unspcp_code" => 46182005,
"head_quarter" => "HYDERABAD",
0 => 2
),
)
);
print_r($stdArray);
print_r($stdArray["multi"]);
print_r($stdArray["multi"]["RAJAHMUNDRY"]);

Remove duplicate data based on postID

I have an array which is as follows:
Array
(
[0] => Array
(
[postId] => 105
[postTitle] => Test
[postNonArray] => Subzero
[postDesc] => Array
(
[0] => Array
(
[para] => Subzero
[align] => L
)
)
[postDate] => 25.08.2016
[postTime] => 13:44
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472112857.png
[postVideo] =>
)
[1] => Array
(
[postId] => 106
[postTitle] => Test 2
[postNonArray] => Test
[postDesc] => Array
(
[0] => Array
(
[para] => Test
[align] => L
)
)
[postDate] => 26.08.2016
[postTime] => 18:08
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472215085.jpg
[postVideo] =>
)
[2] => Array
(
[postId] => 106
[postTitle] => Test 2
[postNonArray] => Test
[postDesc] => Array
(
[0] => Array
(
[para] => Test
[align] => L
)
)
[postDate] => 26.08.2016
[postTime] => 18:08
[postImage] => http://testyourprojects.biz/custom/ci/tharjumal/uploads/post/post_1472215085.jpg
[postVideo] =>
)
)
As you can see, there is two post details with postId=106;
How can I remove the redundant data from the array based on postId?
The project is on PHP.
I think this is what you are trying to achieve:-
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
echo "<pre/>";print_r($array);
Check output(whole code with your original array):- https://eval.in/630678
Note:- It will remove the duplicate values (so whole duplicate array will gone as you asked in comment)
I would suggest loop like the one below. It will go through all the elements from $your_array_name and will make an unique array of id where we will store the postIds. We will also check if there are duplicated in the $unique_ids array, and if so we will remove that duplicate element.
$unique_ids = array();
foreach($your_array_name as $key => $value){
//check if the postId is in the array of the unique ids
if(!in_array($value['postId'], $unique_ids)()){
array_push($unique_ids,$value['postId']); //if it is not - push it there
} else {
unset($your_array_name($key)); //if it is - remove the whole element from the array
}
}
You will need to loop the data and create a new array with unique values so here you go:
$ShowNewArray = array();
foreach($array as $key => $value){
if(!array_key_exists('postId', $ShowNewArray)){
$ShowNewArray[$value['postId']] = $value;
}
}
print_r($ShowNewArray);
Hope it will help you.

merging of 2 identical keys in one array

Need help from the team,
I have this scenario of having 2 identical keys in each array with different values, i want them to be merged into one key were the values also are in it
example:
arrayData1(
[2] => Array
(
[EXP1] => Array (records...)
[EXP2] => Array (records...)
)
)
arrayData2(
[2] => Array
(
[EXP3] => Array (records...)
[EXP4] => Array (records...)
)
)
Having the output like this:
arrayFinal (
[2] => Array
(
[EXP1] => Array (records...)
[EXP2] => Array (records...)
[EXP3] => Array (records...)
[EXP3] => Array (records...)
)
)
Thanks!
First of all you cannot have two same keys in a single array, what you can do is use the array_merge_recursive function in php to merge both the arrays, and the repeating keys will have a new array with all the repeating key values..
$array1 = [
'EXP1' => [1,2,3],
'EXP2' => [2,3,4]
];
$array2 = [
'EXP2' => [5,6,7],
'EXP3' => [8,9,10]
];
Now there are two EXP2 keys, so when you use array_merge_recursive() you get something like this,
print_r(array_merge_recursive($array1, $array2));
//output Array (
[EXP1] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[EXP2] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
[5] => 7
)
[EXP3] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)

Integration of two fields in a database column

Please see my DEMO and answer my question: why date in values html on offset 2 not as date and it is a number?
DEMO: http://codepad.viper-7.com/r9FYnb
$data = array();
$data_1 = $_POST['data_1'];
$static = $_POST["static"];
foreach($static as $idx=>$val){
$data[] = array(
'data_1' => json_encode(Array($data_1[$idx*2],$data_1[$idx]*2+1)),
'static' => $static[$idx]
);
}
This is output:
Array
(
[0] => Array
(
[data_1] => ["2011\/8\/02",4023] **//4023 !?**
[static] => 12
)
[1] => Array
(
[data_1] => ["2011\/8\/09",4023] **// 4023!?**
[static] => 34
)
[2] => Array
(
[data_1] => ["2011\/8\/16",4023] **// 4023 !?**
[static] => 56
)
)
I'm not certain what you are trying to do here, but I see an inconsistency between how you manipulate $idx
'data_1' => json_encode(Array($data_1[$idx*2],$data_1[$idx]*2+1)),
// -----^^^^^^^^-------^^^^^^^^^^^^
For the second offset, perhaps you intend to modify $idx inside the []
'data_1' => json_encode(Array($data_1[$idx*2],$data_1[($idx*2)+1])),
// ---------------------^^^^^^^^^^^^
Sample output after modifying your demo:
Array
(
[0] => Array
(
[data_1] => ["2011\/8\/02","2011\/8\/08"]
[static] => 12
)
[1] => Array
(
[data_1] => ["2011\/8\/09","2011\/8\/15"]
[static] => 34
)

array_slice in multidimensional array?

I have an array in php like this :
Array
(
[0] => Array
(
[915] => 1
[1295] => 1
[1090] => 1
[1315] => 0.93759357774
[128] => 0.93759357774
[88] => 0.731522789561
[1297] => 0.731522789561
[1269] => 0.525492880722
[1298] => 0.525492880722
[121] => 0.519133966069
)
[1] => Array
(
[585] => 1
[1145] => 1
[1209] => 1
[375] => 1
[1144] => 1
[913] => 1
[1130] => 0.996351158355
[215] => 0.937096401456
[1296] => 0.879373313559
[30] => 0.866473953643
[780] => 0.866473953643
[1305] => 0.866473953643
[1293] => 0.866473953643
)
)
How do I get the 1st-5th rows of sub-array for each array, like this :
Result :
Array
(
[0] => Array
(
[915] => 1
[1295] => 1
[1090] => 1
[1315] => 0.93759357774
[128] => 0.93759357774
)
[1] => Array
(
[585] => 1
[1145] => 1
[1209] => 1
[375] => 1
[1144] => 1
)
)
$multid_array = array(/* Your Multidimensional array from above*/);
$sliced_array = array(); //setup the array you want with the sliced values.
//loop though each sub array and slice off the first 5 to a new multidimensional array
foreach ($multid_array as $sub_array) {
$sliced_array[] = array_slice($sub_array, 0, 5);
}
The $sliced_array will then contain the output you wanted.
Iterate over the array.
Read the value by reference.
Delete key-values from offset 5 till
the end. You need not collect the return value because we are using the reference to the original array.
.
foreach($mainArray as $key => &$value) {
array_splice($value,5);
}
Working ideone link
You might want to look into the php function array_splice.
http://no.php.net/manual/en/function.array-slice.php

Categories