String to Multidimensional Array with Unique value in PHP - php

I have below string and want to create multidimensional array using unique value
$string = 'test.jpg|test1.jpg|^22##29##p-test.jpg^22##29##test.video^22##30##p-test2.jpg^22##30##p-test3.jpg^22##30##test4.jpg^22##31##p-CCA_Nestea.jpg^22##31##p-test3.jpg|^24##32##p-test3.jpg^24##32##p-test3.jpg^24##33##p-test3.jpg|test1.jpg';
So I have tried below code but not working
$string = 'test.jpg|test1.jpg|^22##29##p-test.jpg^22##29##test.video^22##30##p-test2.jpg^22##30##p-test3.jpg^22##30##test4.jpg^22##31##p-test22.jpg^22##31##p-test3.jpg|^24##32##p-test3.jpg^24##32##p-test3.jpg^24##33##p-test3.jpg|test1.jpg';
$content_arr = explode("|", $string);
global $db;
$arr1 = array(); $p =0;$prev_layout ='';
foreach($content_arr as $arr){
if(strpos($arr, '^') !== false) {
$l_data = explode("^",$arr);
$larr = array();
foreach ($l_data as $l) {
if(strpos($l, '##') !== false) {
$p = explode("##",$l);
$l_name = 'test lay';
$p_name = 'test pame';
if(!in_array($prev_layout,$p_arr)){
$p_arr = array();
$p_arr['l_id'] =$p[0];
$p_arr['l_name'] =$l_name;
$p_arr['l_interval'] ='';
}
$p_arr['panel'][$p++] = array('p_id'=>$p[1],'p_name'=>$p_name,'p_interval'=>'','p_element'=>$p[2]);
if($p_arr['layout_id'] != $p[0])
//if(!in_array($prev_layout,$p_arr, true)){
array_push($arr1,$p_arr);
//}
if (substr($p[2], -6) != '.video') {
$c++;
}
$prev_layout = $p[0];
}
}
}
}
?>
OUTPUT would be
[0] => Array
(
[l_id] => 22
[l_name] => test Menu
[l_interval] =>
[panel] => Array
(
[0] => Array
(
[p_id] => 29
[p_name] => Left
[p_interval] =>
[p_element] => [0] => Array
(
[0] => test.jpg,
[1] => test1.jpg
)
)
)
)

I have tried your code and modify that is it correct According to your estimated output?
$string = 'test.jpg|test1.jpg|^22##29##p-test.jpg^22##29##test.video^22##30##p-test2.jpg^22##30##p-test3.jpg^22##30##test4.jpg^22##31##p-test22.jpg^22##31##p-test3.jpg|^24##32##p-test3.jpg^24##32##p-test3.jpg^24##33##p-test3.jpg|test1.jpg';
$arr1 =$p_arr= array(); $p =0;$prev_layout ='';
$content_arr = array_filter(explode("|", $string));
global $db;
foreach($content_arr as $arr){
if(strpos($arr, '^') !== false) {
$larr = array();
$l_data = array_filter(explode("^",$arr));
foreach ($l_data as $l) {
if(strpos($l, '##') !== false) {
$l_name = 'test lay'; $p_name = 'Left';//'test pame';
$p=array();
$p = explode("##",$l);
$p0 = $p[0];
$p1 = $p[1];
unset($p[0]);unset($p[1]);
if(!array_key_exists($p0,$p_arr)){
$p_arr[$p0] = array('l_id'=>$p0,
'l_name'=>$l_name,
'l_interval'=>'','panel'=>array());
}
if(!array_key_exists($p1,$p_arr[$p0]['panel'])){
$p_arr[$p0]['panel'][$p1] = array('p_id'=>$p1,'p_name'=>$p_name,'p_interval'=>'','p_element'=>array(array_values($p)));
}else{
$p_arr[$p0]['panel'][$p1]['p_element'][] = array_values($p);
//$p_arr[$p0]['panel'][$p1]['p_element'] = array_merge($p_arr[$p0]['panel'][$p1]['p_element'],array_values($p));//if you want to mearge array
}
}
}
}
}
print_r($p_arr);die;
And Out put of that is
Array
(
[22] => Array
(
[l_id] => 22
[l_name] => test lay
[l_interval] =>
[panel] => Array
(
[29] => Array
(
[p_id] => 29
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test.jpg
)
[1] => Array
(
[0] => test.video
)
)
)
[30] => Array
(
[p_id] => 30
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test2.jpg
)
[1] => Array
(
[0] => p-test3.jpg
)
[2] => Array
(
[0] => test4.jpg
)
)
)
[31] => Array
(
[p_id] => 31
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test22.jpg
)
[1] => Array
(
[0] => p-test3.jpg
)
)
)
)
)
[24] => Array
(
[l_id] => 24
[l_name] => test lay
[l_interval] =>
[panel] => Array
(
[32] => Array
(
[p_id] => 32
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test3.jpg
)
[1] => Array
(
[0] => p-test3.jpg
)
)
)
[33] => Array
(
[p_id] => 33
[p_name] => Left
[p_interval] =>
[p_element] => Array
(
[0] => Array
(
[0] => p-test3.jpg
)
)
)
)
)
)

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
)
)

Compare different arrays structures

I have two arrays with different structures. Array 1 and Array 2 that I will name from MyList and MyFiles. I would get to return only MyList values that do not have in MyFiles. But the two arrays have different structures and I'm having trouble trying to compare
MyList
Array
(
[info] => Array
(
[0] => Array
(
[player] => Messi
[week] => Array
(
[id] => 252
[videos] => Array
(
[0] => Array
(
[id] => 2929850
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929848
[link] => best.mp4
)
[2] => Array
(
[id] => 2929847
[link] => dribbling.mp4
)
)
)
)
[1] => Array
(
[player] => CR7
[week] => Array
(
[id] => 251
[videos] => Array
(
[0] => Array
(
[id] => 2929796
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929795
[link] => best.mp4
)
)
)
)
[2] => Array
(
[player] => Neymar
[week] => Array
(
[id] => 253
[videos] => Array
(
[0] => Array
(
[id] => 2929794
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929793
[link] => best.mp4
)
)
)
)
)
)
MyFiles Array
Array
(
[252] => Array
(
[0] => Array
(
[id] => 2929850
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929848
[link] => best.mp4
)
)
[251] => Array
(
[0] => Array
(
[id] => 2929796
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929795
[link] => best.mp4
)
)
)
the comparison must be made by id of the week and id of the video
I tried this but it did not work out:
$new = array();
foreach ($list['info'] as $source) {
foreach ($source["week"]['videos'] as $keys => $videos) {
foreach ($file as $key => $upload) {
if ($source["week"]["id"] == $key ) {
for($i=0; $i<count($source["week"]["videos"]); $i++){
if($videos["id"] == $upload[$i]["id"]){
unset($videos);
}else{
$new[] = $videos;
}
}
} else {
$new[] = $videos;
}
}
}
}
The expected return would be something like this.
Array
(
[info] => Array
(
[0] => Array
(
[player] => Messi
[week] => Array
(
[id] => 252
[videos] => Array
(
[2] => Array
(
[id] => 2929847
[link] => dribbling.mp4
)
)
)
)
[2] => Array
(
[player] => Neymar
[week] => Array
(
[id] => 253
[videos] => Array
(
[0] => Array
(
[id] => 2929794
[link] => goals.mp4
)
[1] => Array
(
[id] => 2929793
[link] => best.mp4
)
)
)
)
)
)
I have hidden the array in a usable format for my sake in the future should this answer be incorrect and need changing.
$desired = array();
$desired['info'][0]['player'] = 'Messi';
$desired['info'][0]['week']['id'] = 252;
$desired['info'][0]['week']['videos'][2]['id'] = 2929847;
$desired['info'][0]['week']['videos'][2]['link'] = 'dribbling.mp4';
$desired['info'][2]['player'] = 'Neymar';
$desired['info'][2]['week']['id'] = 253;
$desired['info'][2]['week']['videos'][0]['id'] = 2929794;
$desired['info'][2]['week']['videos'][0]['link'] = 'goals.mp4';
$desired['info'][2]['week']['videos'][1]['id'] = 2929793;
$desired['info'][2]['week']['videos'][1]['link'] = 'best.mp4';
$list = array();
$list["info"][0]["player"] = "Messi";
$list["info"][0]["week"]["id"] = "252";
$list["info"][0]["week"]["videos"][0]["id"] = 2929850;
$list["info"][0]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][0]["week"]["videos"][1]["id"] = 2929848;
$list["info"][0]["week"]["videos"][1]["link"] = "best.mp4";
$list["info"][0]["week"]["videos"][2]["id"] = 2929847;
$list["info"][0]["week"]["videos"][2]["link"] = "dribbling.mp4";
$list["info"][1]["player"] = "CR7";
$list["info"][1]["week"]["id"] = "251";
$list["info"][1]["week"]["videos"][0]["id"] = 2929796;
$list["info"][1]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][1]["week"]["videos"][1]["id"] = 2929795;
$list["info"][1]["week"]["videos"][1]["link"] = "best.mp4";
$list["info"][2]["player"] = "Neymar";
$list["info"][2]["week"]["id"] = "253";
$list["info"][2]["week"]["videos"][0]["id"] = 2929794;
$list["info"][2]["week"]["videos"][0]["link"] = "goals.mp4";
$list["info"][2]["week"]["videos"][1]["id"] = 2929793;
$list["info"][2]["week"]["videos"][1]["link"] = "best.mp4";
$file = array();
$file[252][0]['id'] = 2929850;
$file[252][0]['link'] = 'goals.mp4';
$file[252][1]['id'] = 2929848;
$file[252][1]['link'] = 'best.mp4';
$file[251][0]['id'] = 2929796;
$file[251][0]['link'] = 'goals.mp4';
$file[251][1]['id'] = 2929795;
$file[251][1]['link'] = 'best.mp4';
Edit
function array_diff_assoc_recursive($array1, $array2) {
$difference=array();
foreach ($array1 as $key => $value) {
if (is_array($value)) {
if( !isset($array2[$key]) || !is_array($array2[$key])) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if (!empty($new_diff))
$difference[$key] = $new_diff;
}
} else if (!array_key_exists($key,$array2) || $array2[$key] !== $value) {
$difference[$key] = $value;
}
}
return $difference;
}
$new = array('info' => array());
foreach ($list['info'] as $key => $item) {
$a = $item['week']['videos'];
//$b = $file[$item['week']['id']] ?? []; // This is PHP7+
$b = isset($file[$item['week']['id']]) ? $file[$item['week']['id']] : [];
$c = array_diff_assoc_recursive($a, $b);
if (!empty($c)) {
$new['info'][$key] = $item;
$new['info'][$key]['week']['videos'] = $c;
}
}
You will need a function that will check the difference between the videos arrays.
What I do is simply iterate over the list array and then check the difference between that item and the file array. The difference is then stored in $c.
If there is a difference then if statement is fired which will store that player in the $new array and then replace the videos array with the difference array.
This is similar to what you were doing when you were unsetting variables.

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
)
)
)
)

Find Same Array value from one single array and merge its value into new array group wise

I have one multidimensional array and from that array I want to create a new array from same id1 from first array and want to get all matching sub arrays value into its inner array with its element
Below is the array I have:
Array
(
[0] => Array
(
[id1] => 109891
[id2] => 67
)
[1] => Array
(
[id1] => 828393
[id2] => 67
)
[2] => Array
(
[id1] => 828393
[id2] => 68
)
[3] => Array
(
[id1] => 816714
[id2] => 70
)
[4] => Array
(
[id1] => 816714
[id2] => 67
)
[5] => Array
(
[id1] => 816714
[id2] => 68
)
)
And I want output like in this way:
Array
(
[109891] => Array
(
[0] => Array
(
[id2] => 67
)
)
[828393] => Array
(
[0] => Array
(
[id2] => 67
)
[1] => Array
(
[id2] => 68
)
)
[816714] => Array
(
[0] => Array
(
[id2] => 70
)
[1] => Array
(
[id2] => 67
)
[2] => Array
(
[id2] => 68
)
)
)
Any Help Would be Appreciated
This should do it:
$output = array();
foreach($input as $r) {
$id1 = $r['id1'];
if(!isset($output[$id1])) {
$output[$id1] = array();
}
$output[$id1][] = array('id2' => $r['id2']);
}
print_r($output);
origin array:
$array = array(
array('id1' => '109891', 'id2' => '67'),
array('id1' => '828393', 'id2' => '67'),
array('id1' => '828393', 'id2' => '68'),
array('id1' => '816714', 'id2' => '70'),
);
use this foreach:
$new = array();
foreach($array as $element) {
$new[$element['id1']][] = array('id2' => $element['id2']);
}
or this in a function:
function editMyArray($array) {
$newArray = array();
foreach($array as $element) {
$newArray[$element['id1']][] = array('id2' => $element['id2']);
}
return $newArray;
}
Output:
echo "<pre>";
print_r($new);
echo "</pre>";
Array
(
[109891] => Array
(
[0] => Array
(
[id2] => 67
)
)
[828393] => Array
(
[0] => Array
(
[id2] => 67
)
[1] => Array
(
[id2] => 68
)
)
[816714] => Array
(
[0] => Array
(
[id2] => 70
)
)
)
Use below tested code:
$i = 0;
$sepr = 0;
$arr = array();
foreach($your_array as $your_reslut_array) {
$k = $i; $k = ($k > 0 ? $k-1 : 0);
foreach($your_reslut_array as $key => $value) {
if($key != "ID" && $value != $your_reslut_array[$k][$key]) {
$sepr++;
}
}
$arr[$sepr][] = $car;
$i++;
}
print_r($arr);

Create 2 dimensional array from 1 dimensional

How from this array:
Array
(
[0] => Array
(
[BLACK] => Array
(
[0] => 3171
[1] => 3173
[2] => 3175
)
[WHITE] => Array
(
[0] => 3170
[1] => 3172
[2] => 3174
)
)
[1] => Array
(
[SMALL] => Array
(
[0] => 3170
[1] => 3171
)
[MEDIUM] => Array
(
[0] => 3172
[1] => 3173
)
[LARGE] => Array
(
[0] => 3174
[1] => 3175
)
)
)
I could create something like this:
$array['BLACK']['SMALL'] = 3171;
$array['BLACK']['MEDIUM'] = 3173;
$array['BLACK']['LARGE'] = 3175;
$array['WHITE']['SMALL'] = 3170;
$array['WHITE']['MEDIUM'] = 3172;
$array['WHITE']['LARGE'] = 3174;
so create 2 dimensional array from 1 dimensional, where option is same.
<?php
$colors = array('BLACK' => array('3171','3173','3175'),'WHITE' => array('3170','3172','3174'));
$sizes = array('SMALL' => array('3170','3171'),'MEDIUM' => array('3172','3173'), 'LARGE' => array('3174','3175'));
$merged = array();
foreach ($colors as $c_key => $color) {
foreach ($color as $c_val) {
foreach ($sizes as $s_key => $size) {
foreach ($size as $s_val) {
if ($c_val == $s_val) {
$merged[$c_key][$s_key] = $c_val;
}
}
}
}
}
// var_dump($merged);
?>

Categories