php display only duplicate value from array (facebook api) - php

I want delete duplicate post from array of facebook graph data
my page data like :
{
"data": [
{
"link": "http://example.com/188",
"id": "427801497327797_428375477270399",
"created_time": "2013-06-29T14:16:26+0000"
},
{
"link": "http://example.com/188",
"id": "427801497327797_428375187270428",
"created_time": "2013-06-29T14:15:27+0000"
},
{
"link": "http://example.com/188",
"id": "427801497327797_428363873938226",
"created_time": "2013-06-29T13:33:17+0000"
},
{
"link": "http://example.com/196",
"id": "427801497327797_428363597271587",
"created_time": "2013-06-29T13:32:07+0000"
}
],
"paging": {
"previous": "",
"next": ""
}
}
You can see duplicate link example.com/188.
I want get id of all duplicate link.
I'm working with facebook-page-poster

If your concern is to remove duplicate link data then you can do like this
<?php
$duplicate_ids = array();
$all_links = array();
$facebook_data = array(
'data' => array(
array(
'link' => 'http=>//site.com/188',
'id' => '427801497327797_428375477270399',
'created_time' => '2013-06-29T14=>16=>26+0000'
), array(
'link' => 'http=>//site.com/188',
'id' => '427801497327797_428375187270428',
'created_time' => '2013-06-29T14=>15=>27+0000'
), array(
'link' => 'http=>//site.com/188',
'id' => '427801497327797_428363873938226',
'created_time' => '2013-06-29T13=>33=>17+0000'
), array(
'link' => 'http=>//site.com/196',
'id' => '427801497327797_428363597271587',
'created_time' => '2013-06-29T13=>32=>07+0000'
)
),
'paging' => array(
'previous' => '',
'next' => ''
)
);
$data_count = count($facebook_data['data']);
for ($i = 0; $i < $data_count; $i++) {
if (in_array($facebook_data['data'][$i]['link'], $all_links)) {
$duplicate_ids[] = $facebook_data['data'][$i]['id'];
unset($facebook_data['data'][$i]);
} else {
$all_links[] = $facebook_data['data'][$i]['link'];
}
}
echo '<pre>'; print_r($duplicate_ids); '</pre>';

Related

Simplify & Optimize PHP Array

I am working on combining the array into a key with the count of repeated "option""code". My Request JSON is like this
[{
"productId": "DENSUS-MARK",
"options": [
{
"code": "HIGLIGT_OPTION_HANDLE"
},
{
"code": "HIGLIGT_OPTION_HANDLE1"
}
]
},
{
"productId": "DENSUS-MARK",
"options": [
{
"code": "HIGLIGT_OPTION_HANDLE"
}
]
},
{
"productId": "DENSUS-MARK-II",
"options": [
{
"code": "HIGLIGT_OPTION_HANDLE"
}
]
}]
After combing the "productID" and the count of ["options"]["code"] (For ProductId - DENSUS-MARK, the code "HIGLIGT_OPTION_HANDLE" count is 2. So I am getting a output like this.
{
"productId": "DENSUS-MARK",
"options": [
{
"code": "HIGLIGT_OPTION_HANDLE",
"count": 2
},
{
"code": "HIGLIGT_OPTION_HANDLE1",
"count": 1
}
]
},
{
"productId": "DENSUS-MARK-II",
"options": [
{
"code": "HIGLIGT_OPTION_HANDLE",
"count": 1
}
]
}
}
This is my current php code and I need to optimize & simply this below code
$datas = json_decode($arr,true);
$formattedData = [];
foreach ($datas as $f) {
foreach ($f['options'] as $option) {
$formattedData[$f['productID']]['productID'] = $f['productID'];
$formattedData[$f['productID']]['options']['code'][$option['code']][] = $option['code'];
}
}
foreach ($formattedData as &$data) {
$formattedOptions = [];
foreach ($data['options']['code'] as $key => $codes) {
$formattedOptions[] = [
'code' => $key,
'count' => count($codes)
];
}
$data = $formattedOptions;
}
print_r($formattedData);
Someone, could you please help me in this.
I don't know if this is the optimization you want. Meanwhile, less than two loops, I have not found. It's not quite the expected result, but you should be able to fix it if you need to.
With:
$input = array (
0 =>
array (
'productId' => 'DENSUS-MARK',
'options' =>
array (
0 =>
array (
'code' => 'HIGLIGT_OPTION_HANDLE',
),
1 =>
array (
'code' => 'HIGLIGT_OPTION_HANDLE1',
),
),
),
1 =>
array (
'productId' => 'DENSUS-MARK',
'options' =>
array (
0 =>
array (
'code' => 'HIGLIGT_OPTION_HANDLE',
),
),
),
2 =>
array (
'productId' => 'DENSUS-MARK-II',
'options' =>
array (
0 =>
array (
'code' => 'HIGLIGT_OPTION_HANDLE',
),
),
)
);
Then just:
$result = [];
foreach($input as $row) {
foreach($row['options'] as $value) {
$result[$row['productId']][$value['code']] ??=0;
$result[$row['productId']][$value['code']] += count($value);
}
}
var_export($result);
Results:
array (
'DENSUS-MARK' =>
array (
'HIGLIGT_OPTION_HANDLE' => 2,
'HIGLIGT_OPTION_HANDLE1' => 1,
),
'DENSUS-MARK-II' =>
array (
'HIGLIGT_OPTION_HANDLE' => 1,
),
)

PHP/MySQL - Nested groups in JSON

I'm creating nested (hierarchical) groups in JSON. The structure is simply - grand parent -> parent -> child:
main
secondary
mcondition
This is using the following MySQL/PHP to so far format main -> mcondition.
How should I change this to add the second level ('secondary' e.g. the parent level) between main and mcondition?
The column for secondary is mcondition.secondary
$query = 'SELECT * FROM mcondition ORDER BY mcondition.main ASC';
$result = $connection->query( $query );
$results = array();
$temp = array();
while ($line = mysqli_fetch_array($result)) {
$results[] = $line;
}
foreach($results as $row) {
$temp[$row['main']]['text'] = $row['main'];
if(!isset($temp[$row['main']]['children'])) {
$temp[$row['main']]['children'] = array();
}
array_push($temp[$row['main']]['children'], array(
'id' => $row['mcondition_pk'],
'text' => $row['mcondition_name']
));
}
$temp = array_values($temp);
echo json_encode($temp);
This is what the JSON currently looks like:
[
{
"text": "Main Heading 1",
"children": [
{
"id": "1",
"text": "mcondition_1"
},
{
"id": "17",
"text": "mcondition_4"
}
]
},
{
"text": "Main Heading 2",
"children": [
{
"id": "49",
"text": "mcondition_2"
},
{
"id": "48",
"text": "mcondition_5"
}
]
},
{
"text": "Main Heading 3",
"children": [
{
"id": "68",
"text": "mcondition_3"
},
{
"id": "67",
"text": "mcondition_6"
}
]
}
]
This is the structure of the table mcondition:
+---------------+------------+------+-----------+
| mcondition_pk | mcondition | main | secondary |
+---------------+------------+------+-----------+
Column mcondition is unique.
I'm still not 100% sure what your expected json would look like, but you should be able to do something like this. You may need to make some modifications to get exactly what you're wanting. I'm using an array $results to simulate the data acquisition from the database. This will get you a nested hierarchical structure.
$out = [];
$results = [
['main' => 'mk1', 'secondary' => 'sk1', 'mcondition_pk' => 1, 'mcondition' => 'mcondition_1'],
['main' => 'mk1', 'secondary' => 'sk2', 'mcondition_pk' => 2, 'mcondition' => 'mcondition_2'],
['main' => 'mk1', 'secondary' => 'sk2', 'mcondition_pk' => 3, 'mcondition' => 'mcondition_3'],
['main' => 'mk2', 'secondary' => 'sk3', 'mcondition_pk' => 4, 'mcondition' => 'mcondition_4'],
['main' => 'mk2', 'secondary' => 'sk3', 'mcondition_pk' => 5, 'mcondition' => 'mcondition_5'],
['main' => 'mk3', 'secondary' => 'sk4', 'mcondition_pk' => 6, 'mcondition' => 'mcondition_6'],
['main' => 'mk3', 'secondary' => 'sk5', 'mcondition_pk' => 7, 'mcondition' => 'mcondition_7'],
['main' => 'mk3', 'secondary' => 'sk5', 'mcondition_pk' => 8, 'mcondition' => 'mcondition_8'],
];
foreach($results as $row) {
$mk = $row['main'];
$sk = $row['secondary'];
$pk = $row['mcondition_pk'];
$cond = $row['mcondition'];
$temp = ['id' => $pk, 'text' => $cond];
if(!isset($out[$mk])) { $out[$mk] = ['text' => $mk]; }
if(!isset($out[$mk][$sk])) { $out[$mk][$sk] = ['text' => $sk, 'children' => []]; }
$out[$mk][$sk]['children'][] = $temp;
}
echo "<pre>"; print_r($out); echo "</pre>";
$json = json_encode($out);
echo "<pre>"; print_r($json); echo "</pre>"; exit;

Json starting with [ and when using json_encode it starts with " in magento2 using php7.1

I want my json to start with { but if using json_encode it is getting converted into string, I am using php7.1 on ubuntu and working on magento 2.3
This is what I am getting with the below code, I don't want '['
[
{
"success": "true",
"data": {
"mainimages": [
{
Here is my code
$response = array(
array(
"success" => "true",
"data" => $alldata,
"newarrivalheading" => "NEW ARRIVALS",
"instagramheading" => "CELEBS IN LULU",
"specialpriceheading" => "SPECIAL PRICES",
"editorwishlistheading" => "EDITOR'S WISHLIST",
"stylehighlightheading" => "STYLE HIGHLIGHTS",
"styletagline" => "#Looks to swipe right",
"newarrivalindex" => 3,
"instagramindex" => 9,
"editorwishlistviewall" => "",
"sliderimage" => $sliderimage
)
);
return $response;
This is what I want
{
"success": "true",
"data": {
"mainimages": [
{
So remove the unnecessary outer array like so
$alldata = [1,2,3,4];
$sliderimage = ['xz.jpg','ab.png'];
$response = array(
"success" => "true",
"data" => $alldata,
"newarrivalheading" => "NEW ARRIVALS",
"instagramheading" => "CELEBS IN LULU",
"specialpriceheading" => "SPECIAL PRICES",
"editorwishlistheading" => "EDITOR'S WISHLIST",
"stylehighlightheading" => "STYLE HIGHLIGHTS",
"styletagline" => "#Looks to swipe right",
"newarrivalindex" => 3,
"instagramindex" => 9,
"editorwishlistviewall" => "",
"sliderimage" => $sliderimage
);
echo json_encode($response);
RESULT
{
"success": "true",
"data": [
1,
2,
3,
4
],
"newarrivalheading": "NEW ARRIVALS",
"instagramheading": "CELEBS IN LULU",
"specialpriceheading": "SPECIAL PRICES",
"editorwishlistheading": "EDITOR'S WISHLIST",
"stylehighlightheading": "STYLE HIGHLIGHTS",
"styletagline": "#Looks to swipe right",
"newarrivalindex": 3,
"instagramindex": 9,
"editorwishlistviewall": "",
"sliderimage": [
"xz.jpg",
"ab.png"
]
}

PHP - Multidimensional array based placeholders find and replace

I have a multidimensional array and I need to be able to replace JSON placeholders with the relevant value.
Also I need to be able to cascade the values too.
I'm probably just missing a simple thing but its been bugging me for days now.
I'd me very greatful if someone could point out a fix for me.
For example this is my data array:
$SCREEN = array(
'z5OrfqifU8RiA' =>
array(
'current_status' => 'Completed',
'email' => 'luke#test.com',
'Messages' =>
array(
array(
array(
'type' => 'message',
'messageIndex' => 0,
'ReceivedAt' => '17/11/2017 17:06:23',
'Name' => 'Luke ',
'Message' => 'Hello.'
),
array(
'type' => 'message',
'messageIndex' => 1,
'ReceivedAt' => '17/11/2017 17:06:25',
'Name' => 'Luke2 ',
'Message' => 'Hello back.'
)
)
),
'Message Count' => 2,
)
);
I need to take the placeholders that represent the array values as dot notation and replace them with the correct string OR child array using a wildcard.
Here are a few examples of before and afters...
From this:
{ "Messages": "{{SCREEN.Messages}}"}
To:
{
"Messages": [
{
"type": "message",
"messageIndex": 0,
"ReceivedAt": "17/11/2017 17:06:23",
"Name": "Luke",
"Message": "Hello."
},{
"type": "message",
"messageIndex": 1,
"ReceivedAt": "17/11/2017 17:07:06",
"Name": "Luke2",
"Message": "Hello back."
}
]
}
From this:
{ "email": "{{SCREEN.email}}"}
To:
{ "email": "luke#test.com"}
From this:
{ "everything": "{{SCREEN.*}}"}
To:
{
"everything": {
"z5OrfqifU8RiA": {
"current_status": "Completed",
"email": "luke#test.com",
"Messages": [
[
{
"type": "message",
"messageIndex": 0,
"ReceivedAt": "17/11/2017 17:06:23",
"Name": "Luke",
"Message": "Hello."
},
{
"type": "message",
"messageIndex": 1,
"ReceivedAt": "17/11/2017 17:07:06",
"Name": "Luke2",
"Message": "Hello back."
}
]
],
"Message Count": 2
}
}
}
This is the current code that works if you explicitly target a value but not for an array using a wildcard to get anything that is a child to that key.
$SCREEN = array(
'z5OrfqifU8RiA' =>
array(
'current_status' => 'Completed',
'email' => 'luke#test.com',
'Messages' =>
array(
array(
array(
'type' => 'message',
'messageIndex' => 0,
'ReceivedAt' => '17/11/2017 17:06:23',
'Name' => 'Luke ',
'Message' => 'Hello.'
),
array(
'type' => 'message',
'messageIndex' => 1,
'ReceivedAt' => '17/11/2017 17:06:25',
'Name' => 'Luke2 ',
'Message' => 'Hello back.'
)
)
),
'Message Count' => 2,
)
);
$X = parseVariables('{ "everything": "{{SCREEN.*}}"}');
$Y = parseVariables('{ "email": "{{SCREEN.email}}"}');
function parseVariables($array) {
GLOBAL $SCREEN;
foreach(flatten($SCREEN) AS $K => $V)$array = MD_ArrayFandR("{{SCREEN.".$K."}}",$V,$array);
return $array;
}
function flatten($array) {
$result = array();
foreach($array as $key=>$value) {
if(is_array($value)) {
$result = $result + flatten($value, $key . '.');
} else {
$result[$key] = $value;
}
}
return $result;
}
function MD_ArrayFandR($Find, $Replace,$Array){
if (is_array($Array)){
$ex=json_encode($Array);
return json_decode(str_replace($Find, $Replace,$ex),true);
} else {
return str_replace($Find, $Replace,$Array);
}
}
print_r($X);
print_r($Y);
Write a recursive function that can descend into the structure, like:
function recurse($foo) {
foreach( $foo as $key => $value ) {
if( is_array($value) ) {
$foo[$key] = recurse($value);
} else if( /* $value is a placeholder */ ) {
$foo[$key] = /* replacement */;
}
}
return $foo
}

Error with dynamic json array in php

I have an application that retrieves data from a mysql database and generates a json output with php to send to a plugin.
I'm generating the following json output from php:
{
"mapwidth":"1300",
"mapheight":"1000",
"categories":"[]",
"levels":{
"id":"lots",
"title":"Lots",
"map":"maps\/lot-map.svg",
"minimap":"",
"locations":[
{
"id":"lot1",
"title":"Lot 1",
"pin":"hidden",
"description":"<p>Status: <b style=\\\"color: #8eba5e;\\\">Available<\/b><br>Size:\u00a0<b>850 sqm<\/b><br>Please get in touch for an Offer.<\/p>",
"link":null,
"x":"0.4849",
"y":"0.4629",
"fill":null,
"category":"false",
"action":"tooltip"
}
]
},
"maxscale":"1.8"
}
But the format is incorrect. Should be like the following tested json file:
{
"mapwidth": "1300",
"mapheight": "1000",
"categories": [],
"levels": [
{
"id": "lots",
"title": "Lots",
"map": "maps/lot-map.svg",
"minimap": "",
"locations": [
{
"id": "lot12",
"title": "Lot 12",
"pin": "hidden",
"description": "<p>Status: <b style=\"color: #8eba5e;\">Available</b><br>Size: <b>850 sqm</b><br>Please get in touch for an Offer.</p>",
"link": "#more",
"x": "0.3726",
"y": "0.4565"
}
]
}
],
"maxscale": 1.8
}
The difference is in the "levels" key.
This is my php code:
$results = array(
'mapwidth' => '1300',
'mapheight' => '1000',
'categories' => '[]'
);
$results['levels'] = array(
'id' => 'lots',
'title' => 'Lots',
'map' => 'maps/lot-map.svg',
'minimap' => ''
);
if ($lotes)
{
// build usable array
foreach($lotes['results'] as $lote)
{
$results['levels']['locations'][] = array(
'id' => $lote['slug'],
'title' => $lote['title'],
'pin' => $lote['pin'],
'description' => $lote['description'],
'link' => $lote['link'],
'x' => $lote['position_x'],
'y' => $lote['position_y'],
'fill' => $lote['fill'],
'category' => $lote['category'],
'action' => $lote['action']
);
}
}
else
$results['error'] = lang('core error no_results');
$results['maxscale'] = '1.8';
// display results using the JSON formatter helper
display_json($results);
Any suggestions? Thanks
You need to make the levels a multidimensional array.
$results['levels'] = array();
$results['levels'][0] = array(
'id' => 'lots',
'title' => 'Lots',
'map' => 'maps/lot-map.svg',
'minimap' => ''
);
Then when you append to do, do it as follows:
$results['levels'][0]['locations'][] = array(

Categories