Different Arrays convert into multi-dimension array - php

I want to combine multiple arrays output in single array. Below is the array I am getting when I do this.
print_r($getData_milestone);
I have arrays as below:
[milestone] => Array
(
[0] => milestone 1
[1] => milestone 2
[2] => milestone 3
)
[date] => Array
(
[0] => 10/25/2015
[1] => 10/30/2015
[2] => 11/25/2015
)
[status] => Array
(
[0] => 1
[1] => 1
[2] => 0
)
And I want to get output such as below:
Array
(
[0] => Array
(
[milestone] => milestone 1
[date] => 10/25/2015
[status] => 1
)
[1] => Array
(
[milestone] => milestone 2
[date] => 10/30/2015
[status] => 1
)
[2] => Array
(
[milestone] => milestone 3
[date] => 11/25/2015
[status] => 0
)
)
I have tried by this code
foreach($getData_milestone['milestone'] as $miledata)
{
$allDatamile[$i]=$getData_milestone;
$allDatamile[$i]=$getData_milestone['date'];
$allDatamile[$i]=$getData_milestone['status'];
$i++;
}

Try this out, and let me know the result. It should work.
I am considering the given array as an associative array with keys "milestone", "date" and "status" .. Correct me if I'm wrong.
$outputArray = array();
foreach($givenArray['milestone'] as $key=>$val){
$outputArray[$key]['milestone'] = $val;
$outputArray[$key]['date'] = $givenArray['date'][$key];
$outputArray[$key]['status'] = $givenArray['status'][$key];
}
print_r($outputArray)

try this,
$a["milestone"][] = "milestone 1";
$a["milestone"][] = "milestone 2";
$a["milestone"][] = "milestone 3";
$a["date"][] = "10/25/2015";
$a["date"][] = "10/30/2015";
$a["date"][] = "11/25/2015";
$a["status"][] = "1";
$a["status"][] = "1";
$a["status"][] = "0";
foreach ($a['milestone'] as $key => $val) {
$a1[$key]["milestone"] = $val;
$a1[$key]["date"] = $a['date'][$key];
$a1[$key]["status"] = $a['status'][$key];
}
output is
Array
(
[0] => Array
(
[milestone] => milestone 1
[date] => 10/25/2015
[status] => 1
)
[1] => Array
(
[milestone] => milestone 2
[date] => 10/30/2015
[status] => 1
)
[2] => Array
(
[milestone] => milestone 3
[date] => 11/25/2015
[status] => 0
)
)

array_column (PHP 5 >= 5.5.0) might help -
$keys = array_keys($arr);
// if the number of element increases(to make it more dynamic)
$count = count($arr['milestone']);
$i= 0;
while($i < $count) {
$new[] = array_column($arr, $i);
$i++;
}
foreach($new as $k => $n) {
$new[$k] = array_combine($keys, $n);
}
var_dump($new);
DEMO

Try it out the bellow code
$out= array();
$milestone=array
(
"milestone 1",
"milestone 2",
"milestone 3"
);
$m_date=array
(
"10/25/2015",
"10/25/2015",
"10/25/2015"
);
$status=array
(
0,1,1
);
for($i=0;$i<count($milestone);$i++){
$comArray=array
(
"milestone" => $milestone[$i],
"date" => $m_date[$i],
"status" => $status[$i]
)
$out[]=$comArray;
}
Hope it will solve your problem.

Related

How can i arrange array before specific array key in PHP

I would like to arrange Array before found a specific array key. For example
Following is array.
Array(
[0] => Array([package_name] => 10.4)
[1] => Array([final_total] => 10.4)
[2] => Array([package_name] => 10.5)
[3] => Array([package_name] => 4.5)
[4] => Array([final_total] => 15)
[5] => Array([package_name] => 15.2)
[6] => Array([final_total] => 15.2)
[7] => Array([package_name] => 8.4)
[8] => 8.4
)
And I want like array.
(
[0] => Array
(
[package_name] => array([0]=>10.4),
[final_total] => 10.4
)
[1] => Array
(
[package_name] => array(
[0] => 10.5,
[1] => 4.5
),
[final_total] => 15
)
[2] => Array
(
[package_name] => array([0]=>15.2)
[final_total] => 15.2
)
[3] => Array
(
[package_name] => array([0]=>8.4)
[final_total] => 8.4
)
)
So What i want If final_total key is found from array then set previous values(package_name) of final_total in a array.
Above example you can see there are 4 final_total key's of array so i want to set each package_name's value in a array that are previous value of final_total.
Following is my code.
This is my array
$main = array(array('package_name' => 10.4),array('final_total' => 10.4),array('package_name' => 10.5),array('package_name' => 4.5),array('final_total' => 15)
,array('package_name' => 15.2),array('final_total' => 15.2));
Code.
<?php
$newArray = [];
$newPackag=[];
$previousValue='';
$currentKey=0;
$PreviousKey=0;
$i=0;
$main_keys = array_keys($main);
foreach ($main as $key => $value) {
$curtent_item[] = isset($main[$key]['package_name']) ? $main[$key]['package_name'] : '';
$currentKey = $key;
if(#$main[$key]['final_total'] ==#$value['final_total']){
$previousValue = #$value['package_name'];
$newArray[] = $previousValue;
$myarray= array(#$main[$key]['package_name']);
if (array_key_exists("final_total",$main[$key])){
if($PreviousKey ==0){
$PreviousKey = $key+1;
}else{
$PreviousKey = $key;
}
}else{
$keys = array_keys($main);
$position = array_search($key, $keys);
echo "Curent Key =".$currentKey.'PreviousKey'.$PreviousKey.'</br>';
if($currentKey != $PreviousKey){
$nextKey = $keys[$currentKey+1 ];
}
$newPackag1[] = array('package_name'=>#$myarray);
}
$mainArray = array('package'=>$newPackag1);
}
$i++;
}
echo "<pre>NE page";print_r($newPackag1);
echo "<pre>";print_r($main);
anyone has better and correct solution. Above code which i am trying not able to get desire output.
Here is the snippet with modified data(surely will work for your case too),
$result = [];$i= 0;
foreach ($main as $key => $value) {
if (is_array($value)) {
$k = key($value);$v = array_shift($value);
($k != 'final_total' ? $result[$i][$k][] = $v : $result[$i][$k] = $v);
if($k == 'final_total'){
$i++;
}
} elseif (!empty($value)) {
$result[$i]['final_total'] = $value;
}
}
print_r($result);
Demo
Output:-
Array
(
[0] => Array
(
[package_name] => Array
(
[0] => 6.5
[1] => 9
)
[final_total] => 15.5
)
[1] => Array
(
[package_name] => Array
(
[0] => 10.5
)
[final_total] => 10.5
)
[2] => Array
(
[package_name] => Array
(
[0] => 17.1
)
[final_total] => 17.1
)
[3] => Array
(
[package_name] => Array
(
[0] => 9.8
)
[final_total] => 9.8
)
[4] => Array
(
[package_name] => Array
(
[0] => 16
)
[final_total] => 16
)
[5] => Array
(
[package_name] => Array
(
[0] => 10.5
)
[final_total] => 10.5
)
)

foreach loop to extract and group sub-arrays

Array:
Array
(
[0] => Array
(
[date] => 2018-05-23
[content] => Array
(
[0] => Array
(
[0] => AAAAAA
[1] => BBBBBB
[2] => CCCCCC
[3] => DDDDDD
[4] => EEEEEE
[5] => FFFFFF
[6] => GGGGGG
[7] => HHHHHH
[8] => IIIIII
[9] => JJJJJJ
)
)
)
[1] => Array
(
[date] => 2018-05-22
[content] => Array
(
[0] => Array
(
[0] => KKKKKK
[1] => LLLLLL
[2] => MMMMMM
[3] => NNNNNN
...
I need to be able to create a foreach loop that will create a database record from 3 variables.
foreach #1:
$date = $array['content']['date']; //2018-05-23
$headline = $array['content'][0][$key]; // AAAAAA
$content = $array['content'][0][$key]; // BBBBBB
foreach #2:
$date = $array['content']['date']; //2018-05-23
$headline = $array['content'][0][$key]; // CCCCCC
$content = $array['content'][0][$key]; // DDDDDD
Until it finishes with the first sub-array and then goes to the second array:
foreach #6:
$date = $array['content']['date']; //2018-05-22
$headline = $array['content'][0][$key]; // KKKKKK
$content = $array['content'][0][$key]; // LLLLLL
I've been trying to group the arrays with array_chunk with no success and then I tried to write a small fix to order the array properly with this:
if ($x <= 10) {
if ( $a < 2 ) {
$a++;
} else {
$x++;
$a = 1;
}
$res[$i]['content'][$x][] = ltrim($text);
} else {
$res[$i]['content'][$x][] = ltrim($text);
$x = 0;
}
Result:
[date] => 2018-05-23
[content] => Array
(
[0] => Array
(
[0] => AAAAAA
[1] => BBBBBB
)
[1] => Array
(
[0] => CCCCCC
[1] => DDDDDD
)
[2] => Array
(
[0] => EEEEEE
[1] => FFFFFF
)
[3] => Array
(
[0] => GGGGGG
[1] => HHHHHH
)
Which worked for the first array but all the other arrays lost order and were not categorized properly.
Any ideas how this can be created?
EDIT: content will always have 24 records (0-23), so if divided into chunks we should get 12 array chunks for every content sub-array.
I'm not sure how your DB structure looks like because you didn't mention, but you can do something like that:
<?php
$array = [
[
"date" => "date",
"content" => [["A", "B", "C"]]
],
[
"date" => "date",
"content" => [["E", "F", "G", "H"]]
],
];
$sql = "INSERT INTO table (content, date) VALUES ";
foreach ($array as $key => $item) {
$content = $item["content"][0];
for ($i = 0; $i < count($content); $i += 2) {
$letters = $content[$i];
if (isset($content[$i + 1])) {
$letters .= $content[$i + 1];
}
$sql .= "('$letters', '$item[date]'),";
}
}
$sql = rtrim($sql, ",") . ";";
echo $sql;
Will output:
INSERT INTO table (content, date) VALUES ('AB', 'date'),('C', 'date'),('EF', 'date'),('GH', 'date');
<?php
$collection =
array
(
array
(
'date' => '2018-05-23',
'content' => array
(
array
(
'AAAAAA',
'BBBBBB',
'CCCCCC',
'DDDDDD',
'EEEEEE',
'FFFFFF',
'GGGGGG',
'HHHHHH',
'IIIIII',
'JJJJJJ'
)
)
),
array
(
'date' => '2018-05-22',
'content' => array
(
array
(
'KKKKKK',
'LLLLLL',
'MMMMMM',
'NNNNNN'
)
)
)
);
function getChunks($data){
$result = array();
$length = count($data);
for($i=0;$i<$length;$i += 2){
$result[] = array($data[$i],$data[$i+1]);
}
return $result;
}
function groupContentByDate($collection){
$result = array();
foreach($collection as $each_data){
$result[$each_data['date']] = array('content' => array());
foreach($each_data['content'] as $each_content){
$result[$each_data['date']]['content'] = array_merge($result[$each_data['date']]['content'],getChunks($each_content));
}
}
return $result;
}
echo "<pre>";
print_r(groupContentByDate($collection));
OUTPUT
Array
(
[2018-05-23] => Array
(
[content] => Array
(
[0] => Array
(
[0] => AAAAAA
[1] => BBBBBB
)
[1] => Array
(
[0] => CCCCCC
[1] => DDDDDD
)
[2] => Array
(
[0] => EEEEEE
[1] => FFFFFF
)
[3] => Array
(
[0] => GGGGGG
[1] => HHHHHH
)
[4] => Array
(
[0] => IIIIII
[1] => JJJJJJ
)
)
)
[2018-05-22] => Array
(
[content] => Array
(
[0] => Array
(
[0] => KKKKKK
[1] => LLLLLL
)
[1] => Array
(
[0] => MMMMMM
[1] => NNNNNN
)
)
)
)

Merge array key values - PHP

Hi all i need to merge the same key to convert to single array from multiple array list please any one help me to the problem
for example here the array.
Array
(
[0] => Array
(
[0] => Mr.
[1] => Mrs.
)
[1] => Array
(
[0] => Rob
[1] => Tam
)
[2] => Array
(
[0] => kar
[1] => Man
)
[3] => Array
(
[0] => 55345345345
[1] => 44545345435
)
)
i need the output is
Array
(
[0] => Array
(
[0] => Mr.
[1] => Rob
[2] => kar
[3] => 55345345345
)
[1] => Array
(
[0] => Mrs.
[1] => Tam
[2] => Man
[3] => 44545345435
)
)
Please any one help
Thanks
For PHP version >= 5.5.0 You can use array_column() and array_merge() for this as
$result = array_merge(array_column($records, '0'), array_column($records, '1'));
print_r($result);
$a = array(
0 => array(
0 => 'Mr.',
1 => 'Mrs.'
),
1 => array
(
0 => 'Rob',
1 => 'Tam'
),
2 => array
(
0 => 'kar',
1 => 'Man'
),
3 => array
(
0 => 55345345345,
1 => 44545345435
)
);
$arr1 = array();
foreach($a as $arr)
{
foreach($arr as $key=>$value)
{
$arr1[$key][] = $value;
}
}
echo '<pre>';
print_r($arr1);
Use this one. You can get output same as you want.
try like this
$out = array();
foreach ($arr1 as $key => $value){
$out[] = (object)array_merge((array)$arr2[$key], (array)$value);
}
print_r($out)
$title = $array[0];
$firstname = $array[1];
$lastname = $array[2];
$number = $array[3];
$output = array();
for($i=0; $i < count($title); $i++)
{
$output[] = array($title[$i],$firstname[$i],$lastname[$i],$number[$i])
}
var_dump($output);

PHP fetch 3d array

I have below array and i want to fetch [2] => Array with foreach but it's showing me an error.
for example array name is $other
Array
(
[0] => Array
(
[0] => aaaaaaaaaaaa
[1] => bbbbbbbbbbbb
[2] => cccccccccccc
)
[1] => Array
(
[0] => dddddddddddd
[1] => eeeeeeeeeeee
[2] => ffffffffffff
)
[2] => Array
(
[0] => gggggggggggg
[1] => hhhhhhhhhhhh
[2] => iiiiiiiiiiii
)
)
fetch array:
foreach ($other[2] as $value) {
echo $value.'<br/>';
}
How do I print all the values of the second array?
You need to nest further more
foreach ($other as $arr)
{
foreach($arr as $k=>$v)
{
if($k==2)
{
echo $v.'<br/>';
}
}
}
OUTPUT :
cccccccccccc
ffffffffffff
iiiiiiiiiiii
Try Something like this,
<?php
$x = array
(
0 => array
(
0 => 'aaaaaaaaaaaa',
1 => 'bbbbbbbbbbbb',
2 => 'cccccccccccc'
),
1 => array
(
0 => 'dddddddddddd',
1 => 'eeeeeeeeeeee',
2 => 'ffffffffffff'
),
2 => array
(
0 => 'gggggggggggg',
1 => 'hhhhhhhhhhhh',
2 => 'iiiiiiiiiiii'
)
);
$count = count($x);
$w = $count - 1;
var_dump($x[$w]);
?>

PHP combine array based on value of particular element

I have an array that looks like this
Array (
[0] => Array
(
[id] => 123
[count] => 1
)
[1] => Array
(
[id] => 123
[count] => 3
)
[2] => Array
(
[id] => 513
[count] => 0
)
[3] => Array
(
[id] => 561
[count] => 1
)
[4] => Array
(
[id] => 613
[count] => 7
)
)
What I want to do is create a new array, that totals the count where the id values are the same. So for example, the new array would look like this:
Array (
[0] => Array
(
[id] => 123
[count] => 4
)
[1] => Array
(
[id] => 513
[count] => 0
)
[2] => Array
(
[id] => 561
[count] => 1
)
[3] => Array
(
[id] => 613
[count] => 7
)
)
Would anyone know a good method to do this?
Thank you!
Short and simple:
$new_array = array();
foreach($data_array as $value) {
if(array_key_exists($value['id'], $new_array)) {
$new_array[$value['id']] += $value['count'];
} else {
$new_array[$value['id']] = $value['count'];
}
}
echo print_r($new_array,true);
I made it id => value since there didn't seem to need another array for data when the id could be used as the array's key.
Any reason why you are not making an associative array of id => count
You can do it with the following function (I didn't tested the code, but it should work ):
function get_count_array($arr) {
$new_array = array();
foreach($arr as $item) {
$found = false;
//loop through all the current new items to check if we already have it
for($i = 0; $i < count($new_array); $i++) {
//do we have it?
if($new_array[$i]['id'] == $item['id']) {
$new_array[$i]['count'] += 1;
$found = true;
break;
}
}
if(!$found) {
$new_array[] = array('id' => $item['id'], 'count' => 0);
}
}
return $new_array;
}

Categories