This is my array that I wants to convert
Array
(
[0] => stdClass Object
(
[Item1] => 10/12/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[1] => stdClass Object
(
[Item1] => 10/11/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[2] => stdClass Object
(
[Item1] => 10/10/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[3] => stdClass Object
(
[Item1] => 10/09/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[4] => stdClass Object
(
[Item1] => 10/08/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[5] => stdClass Object
(
[Item1] => 10/07/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[6] => stdClass Object
(
[Item1] => 10/06/2021
[Item2] => Array
(
[0] => 3729195
[1] => 0
[2] => 0
[3] => 0
)
)
[7] => stdClass Object
(
[Item1] => 10/05/2021
[Item2] => Array
(
[0] => 7458390
[1] => 0
[2] => 0
[3] => 0
)
)
[8] => stdClass Object
(
[Item1] => 10/04/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
[9] => stdClass Object
(
[Item1] => 10/03/2021
[Item2] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
)
)
I wants this array output like this -
[{name:"", data:[0,0,0,0,0,0,3729195,7458390,0,0]},
{name:"", data:[0,0,0,0,0,0,0,0,0,0]},
{name:"", data:[0,0,0,0,0,0,0,0,0,0]},
{name:"", data:[0,0,0,0,0,0,0,0,0,0]}]
The problem is that the first element of [Item2][0] input array is the first elements of each 4 rows of data:[0] and second element of [Item2][1] is second column of each data set of data[1] elements of all 4 rows, I hope you understand what I mean.
And the dates should be in another separate list like this -
['10/12/2021', '10/11/2021', '10/10/2021', '10/09/2021', '10/08/2021', '10/07/2021', '10/06/2021', '10/05/2021', '10/04/2021', '10/03/2021'],
I have already done some tests and here is what I did so far -
$ChartDataNew = stripcslashes(trim(json_encode($jobs->charts_last10days), "\""));
$replace1 = str_replace('Item1', name, $ChartDataNew);
$replace2 = str_replace('Item2', data, $replace1);
print_r($replace2);
The above code is giving me output of an JSON object
[{"name":"10/12/2021","data":[0,0,0,0]},{"name":"10/11/2021","data":[0,0,0,0]},{"name":"10/10/2021","data":[0,0,0,0]},{"name":"10/09/2021","data":[0,0,0,0]},{"name":"10/08/2021","data":[0,0,0,0]},{"name":"10/07/2021","data":[0,0,0,0]},{"name":"10/06/2021","data":[3729195,0,0,0]},{"name":"10/05/2021","data":[7458390,0,0,0]},{"name":"10/04/2021","data":[0,0,0,0]},{"name":"10/03/2021","data":[0,0,0,0]}]
This JSON seems ok, but its not correct. First of all I wants to make the JSON output as given above without "" and name should be name:"". When you see the data in output of my json object is not matching to date in input array.
To get the list of dates I have done this and its fine -
$chartDataSeries = array();
$chartDateLabels = array();
$chartSeriesList = array();
foreach($jobs->charts_last10days as $chartData){
array_push($chartDateLabels, "'".$chartData->Item1."'");
array_push($chartDataSeries, $chartData->Item2);
}
$chartDateLabels1 = implode(', ', ($chartDateLabels));
echo ($chartDateLabels1);
Its giving me the correct output for list of dates, see
['10/12/2021', '10/11/2021', '10/10/2021', '10/09/2021', '10/08/2021', '10/07/2021', '10/06/2021', '10/05/2021', '10/04/2021', '10/03/2021']
please let me know what I am doing wrong or help me out how to do that, may be write some functions or any other way to do this.
Thanks in Advance
There's probably a more elegant solution, but the easiest way I could think of was using iterating through each element, and then constructing another array:
$resultArr = [];
// Iterate over each element of the original array
foreach ($dataArr as $key => $arr) {
// Set the date
$datesArr[$key] = $arr->Item1;
// Iterate over each element of Item2
foreach ($arr->Item2 as $i => $v) {
// Set 'data' on the resulting array item $i to the value
// using the $key from the outer loop as the index for the inner data array
$resultArr[$i]['data'][$key] = $v;
// Set the name
$resultArr[$i]['name'] = '';
}
}
// Convert from an array to JSON
echo json_encode($resultArr) . PHP_EOL;
// The dates are in $datesArr
echo json_encode($datesArr) . PHP_EOL;
$dataArr is the variable where your data is currently stored. After running this loop, $resultArr will be an array, of which each element is another array containing the data in the right format, and $datesArr is the dates from the data in their own array.
I have three arrays first array include ids and employees name and second array have monthly collection with employee ids and third array have daily collection with employee id and daily collection I want to merge these array with ids and name and dcollection and monthly collection but the desired output is not coming here my first array $ids is
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Rohit
)
[1] => stdClass Object
(
[id] => 2
[name] => Emop1
)
[2] => stdClass Object
(
[id] => 3
[name] => Pankaj
)
[3] => stdClass Object
(
[id] => 4
[name] => tejpal singh
)
)
second array $q1 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[mcollecton] => 100
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[mcollecton] => 1222
)
)
third array $q2 is
Array
(
[0] => stdClass Object
(
[name] => Rohit
[id] => 1
[dcollecton] => 300
)
[1] => stdClass Object
(
[name] => Emop1
[id] => 2
[dcollecton] => 150
)
)
so far what I have tried
$new_array = array();
foreach($ids as $k) {
$q1n = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1) {
if($k->id==$k1->id){
$mc = array("mc"=>$k1->mcollecton);
array_merge($q1n,$mc);
}
}
foreach($q2 as $k1){
if($k->id==$k1->id){
$dc = array("dc"=>$k1->dcollecton);
array_merge($q1n,$dc);
}
}
$a = array_merge($q1n,$mc);
$av = array_merge($q1n,$dc);
array_push($new_array,$q1n);
}
but the output is coming as
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
)
[1] => Array
(
[id] => 2
[name] => Emop1
)
[2] => Array
(
[id] => 3
[name] => Pankaj
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
)
)
I want the output be like
Array
(
[0] => Array
(
[id] => 1
[name] => Rohit
[mcollection] => 100
[dcollection] => 300
)
[1] => Array
(
[id] => 2
[name] => Emop1
[mcollection] => 1222
[dcollection] => 150
)
[2] => Array
(
[id] => 3
[name] => Pankaj
[mcollection] => 0
[dcollection] => 0
)
[3] => Array
(
[id] => 4
[name] => tejpal singh
[mcollection] => 0
[dcollection] => 0
)
)
So I have tried many times but the desired output is not coming . please help me out how to get the desired output.
It seemed like that answer could be modified, or put in a function that you could call multiple times if needed to combine more than two arrays.
There's probably cleaner ways to handle this with array functions like array_merge or array_walk, but this is the general idea of how I might approach it. I haven't tested this, but maybe it's useful.
foreach($first as $key1 => $value){
foreach($second as $key2 => $value2){
// match the ids and check if array key exists on first array
if($value['id'] === $value2['id'] && empty($first[$key2])){
$first[$key][$key2] = $value2;
}
}
}
EDIT: Based on the answer you posted vs the question you asked, are you incrementing the collection numbers or just setting them? In other words why use +=? You should also be able to remove array_merge and array_push.
Below is geared more towards what you're trying to do. I haven't tested this either, but if you run into errors, post your code with the errors returned so that it's easier to debug:
foreach($ids as $k)
{
$thisArray = $newArray[] = array("id"=>$k->id,"name"=>$k->name);
foreach($q1 as $k1)
{
if($k->id == $k1->id && !empty($k1->mcollecton))
{
$thisArray['mc'] = $k1->mcollecton;
}
}
foreach($q2 as $k2)
{
if($k->id == $k2->id && !empty($k2->dcollecton))
{
$thisArray['dc'] = $k2->dcollecton;
}
}
}
// This should have both new collections fields on all array items
print_r($newArray)
I have this array :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
[1] => Array
(
[0] => Array
(
[0] => 11
[id_list] => 2
[id] => 2
)
[1] => Array
(
[0] => 12
[id_list] => 2
[id] => 2
)
)
[2] => Array
(
[0] => Array
(
[0] => 13
[id_list] => 4
[id] => 4
)
)
)
and this code (where $dataListe is the result of a fetchAll query) :
$result = [];
foreach($dataListe as $listeDiff){
$result[] = $listeDiff;
}
// $resultUnique = array_unique($result);
echo "<pre>".print_r($result, true)."</pre>";
as you can see, there's some contact similar in my first and my second array (but contact can be the same in the 1st and the 3rd array, is I choose to add my contact in my 3rd array).
I want to remove the duplicate of each element in the general array.
But when I use array unique, I get this result :
Array
(
[0] => Array
(
[0] => Array
(
[0] => 10
[id_list] => 1
[id] => 1
)
[1] => Array
(
[0] => 11
[id_list] => 1
[id] => 1
)
[2] => Array
(
[0] => 12
[id_list] => 1
[id] => 1
)
)
)
Please I need help to only keep 1 item of each array at the end !
EDIT : I have almost the good result with the code below, but the id 12 is missing
$result = [];
foreach($dataListe as $listeDiff){
foreach($listeDiff as $contact){
if(!in_array($contact,$result)){
$result[] = $contact;
}
break;
}
}
As the PHP docs says :
Note: Note that array_unique() is not intended to work on multi dimensional arrays. (http://php.net/manual/en/function.array-unique.php)
You can try this solution
$uniqueResult = array_map("unserialize", array_unique(
array_map("serialize", $result)
));
as suggested by #daveilers on this question How to remove duplicate values from a multi-dimensional array in PHP.
In my PHP application, get the results from DB. After processing the results I need to convert the results like below using foreach
Array
(
[1] => Array -----> This is intent 1, this key indicates all intent values which is equal to 1, should belongs to here.
(
[0] => Array
(
[name] => A
[indent] => 1
)
[1] => Array
(
[name] => B
[indent] => 1
)
)
[2] => Array
(
[0] => Array
(
[name] => B
[indent] => 2
)
[1] => Array
(
[name] => A
[indent] => 2
)
)
[3] => Array
(
[0] => Array
(
[name] => A
[indent] => 3
)
)
)
That I have some intent value common, common intent values are stored in array like array('1'=> array(array[0],array[1]));.
What I tried is
foreach($results as $data){
$root_array[$data['intent']] = array($data);
}
This will replace the old array and insert the last intent value which is common.
I get result like below, the intent 1 and intent 2 are replaced with last data
Array
(
[1] => Array
(
[0] => Array
(
[name] => B
[indent] => 1
)
)
[2] => Array
(
[0] => Array
(
[name] => A
[indent] => 2
)
)
[3] => Array
(
[0] => Array
(
[name] => A
[indent] => 3
)
)
)
In the loop you must check if the current indent has been initialized. If not then create it, else just append the new data to it.
foreach($results as $data) {
if (!isset($root_array[$data['indent']])) {
$root_array[$data['indent']] = array($data);
} else {
$root_array[$data['indent']][] = $data;
}
}
I have two arrays that need to be merged together and trying to figure out the correct way of doing it.
this is the first array
Array
(
[IndividualOutmsg] => Array
(
[0] => Array
(
[user_id] => 3
[number] => 414566765
[msg] => some message
)
[1] => Array
(
[user_id] => 3
[number] => 410335509
[msg] => any message
)
)
)
this is the second array:
Array
(
[0] => Array
(
[0] => OK
[1] => 0
[2] => d142b46128b869d0
[3] => 6178977058476937
)
[1] => Array
(
[0] => OK
[1] => 0
[2] => 60f403f4e243e684
[3] => 6198708709873543
)
)
what i want to get is this:
Array
(
[IndividualOutmsg] => Array
(
[0] => Array
(
[user_id] => 3
[number] => 414566765
[msg] => some message
[sms_status] => OK
[error_code] => 0
[msg_id] => d142b46128b869d0
[msg_id_2] => 6178977058476937
)
[1] => Array
(
[user_id] => 3
[number] => 410335509
[msg] => any message
[sms_status] => OK
[error_code] => 0
[msg_id] => 60f403f4e243e684
[msg_id_2] => 6198708709873543
)
)
)
In that format, you really have to do a lot of the legwork yourself and can't just use array_merge to combine the arrays. It would have to be a more custom job, like so:
$count = count($second_array);
for($i=0; $i<$count; $i++){
$first_array['IndividualOutmsg'][$i]['sms_status'] = $second_array[0];
$first_array['IndividualOutmsg'][$i]['error_code'] = $second_array[1];
$first_array['IndividualOutmsg'][$i]['msg_id'] = $second_array[2];
$first_array['IndividualOutmsg'][$i]['msg_id2'] = $second_array[3];
}
If you were to output the second array with the associative keys set, it would be much easier to combine them using array_merge, provided the keys didn't conflict.
$count = count($second_array);
for($i=0; $i<$count; $i++){
$first_array['IndividualOutmsg'][$i] =
array_merge($first_array['IndividualOutmsg'][$i], $second_array[$i]);
}
http://au.php.net/manual/en/function.array-merge.php
Array merge might be what you're looking for...
Though you'll need to probably write a loop or function that can get to the right place in your multi-dimensional array, perform the merge and also change the relevant keys.