sorting json array by value in php - php

I have an array of json as shown below and i want to sort it by size
Array
(
[0] => {"file_name":"Desert - Copy.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000012","sha256":"010f60d2927a35d0235490136ef9f4953b7ee453073794bcaf153d20a64544ea","size":"845941"}
[1] => {"file_name":"Hydrangeas - Copy.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000013","sha256":"3b92fede080f9b0ec902afc58831191b5b8ccbaf6732352fd7a8b445d1e9f0bd","size":"595284"}
[2] => {"file_name":"Tulips.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000014","sha256":"b9352f2565260219db72fc1fc896113a26c85866b69c50d3970c4d9f5cce830a","size":"620888"}
)
Result expected:
Array
(
[0] => {"file_name":"Hydrangeas - Copy.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000013","sha256":"3b92fede080f9b0ec902afc58831191b5b8ccbaf6732352fd7a8b445d1e9f0bd","size":"595284"}
[1] => {"file_name":"Tulips.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000014","sha256":"b9352f2565260219db72fc1fc896113a26c85866b69c50d3970c4d9f5cce830a","size":"620888"}
[2] => {"file_name":"Desert - Copy.jpg","content_type":"image\/jpeg","tmp_path":"\/var\/www\/mywebsite.com\/public_html\/www\/tmp\/0000000012","sha256":"010f60d2927a35d0235490136ef9f4953b7ee453073794bcaf153d20a64544ea","size":"845941"}
)
How do i accomplish this in an efficient manner

<?php
function sortByYear($a, $b) {
$dA = new DateTime($a['date']);
$dB = new DateTime($b['date']);
return $dA->format('y') - $dB->format('y');
}
$data = '{"info":[{"id":1, "title":"original title", "name":"john doe", "date":"2010-05-15"}, {"id":2, "title":"another title", "name":"foo bar", "date":"2009-04-11"}]}';
$d = json_decode($data, true);
$info = $d['info'];
usort($info, 'sortByYear');
print_r($info);
?>
source:http://codepad.org/6N4fmbKG
You should read this for further help:How to sort a JSON array with PHP

Here is what i came up with but i welcome more efficient solutions
function sort_json($json_array)
{
if (count($json_array) > 1) {
//convert to array
foreach ($json_array as &$json) {
$array[] = json_decode($json);
}
// sort in ascending order by size
usort($array, function($a, $b)
{
return $a->size < $b->size ? -1 : 1; //Compare the sizes
});
//convert back to json
$json_array = array();
foreach ($array as &$a) {
$json_array[] = json_encode($a);
}
}
return $json_array;
}

Use the following code:
function sortBySize($a, $b)
{
if ($a['size'] == $b['size']) return 0;
return ($a['size'] < $b['size']) ? -1 : 1;
}
usort($data, "sortBySize");
How usort work : http://www.w3schools.com/php/func_array_usort.asp

Related

PHP array filter by pre text from value

I have an array like below
Array
(
[0] => country-indonesia
[1] => country-myanmar
[2] => access-is_airport
[3] => heritage-is_seagypsy
)
From that array I want to make separate array only for [country] ,[access], [heritage]
So for that I have to check array value by text before '-'. I am not sure how to do it. so i can't apply code here. I just have the array in PHP
A modified answer, if you want to get the specific types only.
<?php
$arr = [
'country-indonesia',
'country-myanmar',
'access-is_airport',
'heritage-is_seagypsy',
];
$new_array = [];
$types = ['country', 'heritage', 'access'];
foreach ($arr as $element) {
$fac = explode('-', $element);
foreach ($types as $type) {
if ($fac[0] === $type) {
$new_array[$type][] = $fac[1];
}
}
}
$country = $new_array['country'];
$access = $new_array['access'];
$heritage = $new_array['heritage'];
var_dump($new_array);
A simple and easy solution in 3 lines of code using array_walk
<?php
$arr = [
'country-indonesia',
'country-myanmar',
'access-is_airport',
'heritage-is_seagypsy',
];
$new_array = [];
array_walk($arr, function($item) use (&$new_array){
//if(false === strpos($item, '-')) return;
list($key,$value) = explode('-', $item, 2);
$new_array[$key][] = $value;
});
print_r($new_array);
Gives this output:
Array
(
[country] => Array
(
[0] => indonesia
[1] => myanmar
)
[access] => Array
(
[0] => is_airport
)
[heritage] => Array
(
[0] => is_seagypsy
)
)
If you don't want empty and duplicate entries:
<?php
$arr = [
'country-indonesia',
'country-myanmar',
'access-is_airport',
'heritage-is_seagypsy',
];
$new_array = [];
array_walk($arr, function($item) use (&$new_array){
if(false === strpos($item, '-')) return;
list($key,$value) = explode('-', $item, 2);
if(empty($value) || array_key_exists($key, $new_array) && in_array($value, $new_array[$key])) return;
$new_array[$key][] = $value;
});
print_r($new_array);
you can do it by using explode and in_array functions
<?php
$arr = ["country-indonesia","country-myanmar","access-is_airport","heritage-is_seagypsy"];
$newArr = array();
foreach($arr as $k=> $val){
$valArr = explode("-", $val);
if(!in_array($valArr[0], $newArr)){
$newArr[] = $valArr[0];
}
}
print_r($newArr);
?>
live demo
You need PHP's strpos() function.
Just loop through every element of the array and try something like:
if( strpos($array[$i], "heritage") != false )
{
// Found heritage, do something with it
}
(Rough example written from my cellphone while feeding baby, may have typos but it's the basics of what you need)
Read further here: http://php.net/manual/en/function.strpos.php
//first lets set a variable equal to our array for ease in working with i.e
// also create a new empty array to hold our filtered values
$countryArray = array();
$accessArray = array();
$heritageArray = array();
$oldArray = Array(country-indonesia, country-myanmar, access-is_airport, heritage-is_seagypsy);
//Next loop through our array i.e
for($x = 0; $x < count($oldArray); $x++){
// now filter through the array contents
$currentValue = $oldArray[$x];
// check whether the current index has any of the strings in it [country] ,[access], [heritage] using the method : strpos()
if(strpos($currentValue,'country')){
//if this particular value contains the keyword push it into our new country array //using the array_push() function.
array_push($countryArray,$currentValue);
}elseif(strpos($currentValue,'access')){
// else check for the access string in our current value
// once it's found the current value will be pushed to the $accessArray
array_push($accessArray,$currentValue);
}elseif(strpos($currentValue,'heritage')){
// check for the last string value i.e access. If found this too should be pushed to //the new heritage array i.e
array_push($heritageArray,$currentValue);
}else{
// do nothing
}
}
//I believe that should work: cheers hope

replace values of keys in json1 from Json2

I am very very new to php.. actually i am from java domain. But, i have to do some work in php for integration. My scenario is, i have one json array which will have 4 keys for ex:
one json --> {"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}.
I will be getting another JSON which ever edited from admin panel. for example if i updated any key, only that key will coming in the
second JSON --> for ex: {"blog_heading":"def"}
Now, i have to replace the value of second json to first json. example output for above scenario like I am very very new to php.. actually i am from java domain. But, i have to do some work in php for integration. My scenario is, i have one json array which will have 4 keys for ex:
output json --> {"id":7,"active":1,"blogId":"abc","blog_heading":"def"}.
So i am trying as below,
$id = json_decode($data_string);
$id2 = json_encode($post);
$id5 = json_decode($id2);
$id6 = array();
foreach ($id as $key => $value)
{
$log->debug($key . ': ' . $value);
if (array_key_exists($key, $id5->data)) {
$log->debug($key . 'element is in the array');
$log->debug($value . 'element is in the array');
//array_push($id5, "apple", "raspberry");
$id3 = array($key => $value);
$id3[$key] = $value;
$log->debug($id3);
}else{
$log->debug($key . 'element is not in the array');
}
}
$id7 = json_encode($id2);
$log->debug($id7);
id5 data is : $id5
DEBUG - 2017-06-05T02:26:20-04:00 - stdClass Object
(
[meta] => stdClass Object
(
[table] => story
[type] => item
)
[data] => stdClass Object
(
[id] => 7
[active] => 1
[blogId] => abc
[blog_heading] => xyz
)
)
==================
Log of $id :
stdClass Object
(
[active] => 1
[blog_heading] => def
[id] => 7
)
Please suggest me how can i achieve this... Anything i am doing wrong here
Please try that:
$j1 = '{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}';
$j2 = '{"blog_heading":"def"}';
$result = json_encode(
array_merge(
json_decode($j1, true),
json_decode($j2, true)
)
);
<?php
$json1='{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}';
$json2='{"blog_heading":"def"}';
$json1=json_decode($json1);
$json2=json_decode($json2);
foreach ($json1 as $key => $value) {
if($json2->$key){
$json1->$key=$json2->$key;
}
}
$json1=json_encode($json1);
$json2=json_encode($json2);
If you have only one element in array,Do like this
$a = json_decode('{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"}',true);
$b = json_decode('{"blog_heading":"def"}',true);
$a['blog_heading'] = $b['blog_heading'];
print_r($a);
If you have multiple element like this :
$c = json_decode('[{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"},
{"id":8,"active":1,"blogId":"abc","blog_heading":"xyz"}]',true);
$d = json_decode('[{"blog_heading":"def"},{"blog_heading":"hello"}]',true);
$return = array();
for ($i=0; $i < count($c); $i++) {
$c[$i]['blog_heading'] = $d[$i]['blog_heading'];
$return[] = $c[$i];
}
print_r($return);
If you want to replace value by specific id
$c = json_decode('[{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"},
{"id":8,"active":1,"blogId":"abc","blog_heading":"xyz"}]',true);
$d = json_decode('[{"id":7,"blog_heading":"def"},{"id":9,"blog_heading":"hello"}]',true);
$return = array();
for ($i=0; $i < count($c); $i++) {
if($d[$i]['id'] == $c[$i]['id']) {
$c[$i]['blog_heading'] = $d[$i]['blog_heading'];
}
$return[] = $c[$i];
}
print_r($return);
Checking dynamic key value pair :
$c = json_decode('[{"id":7,"active":1,"blogId":"abc","blog_heading":"xyz"},
{"id":8,"active":1,"blogId":"abc","blog_heading":"xyz"}]',true);
$d = json_decode('[{"id":6,"blog_heading":"def"},{"id":9,"blog_heading":"hello"}]',true);
$return = array();
for ($i=0; $i < count($c); $i++) {
$result = array_intersect_key($c[$i], $d[$i]);
foreach ($result as $key => $value) {
$c[$i][$key] = $d[$i][$key];
}
$return[] = $c[$i];
}
print_r($return);
Check demo here

Combine arrays that have same value

I have array like this
$arr=[["a","b"],["b","c"],["d","e"],["f","c"]];
if sub arrays share same value they should be be merged to one array
expected output:
$arr=[["a","b","c","f"],["d","e"]];
I`m trying to avoid doing foreach inside foreach for solving this.
It seems your inner arrays always have 2 items. so nested loops aren't necessary. Here is a solution which I originally wrote in JS but it should work just as good and most efficient in PHP:
$arr=[["a","b"],["b","c"],["d","e"],["f","c"],["h","e"]];
$output = [];
$outputKeys = [];
$counter = 0;
foreach($arr as $V) {
if(!isset($outputKeys[$V[0]]) && !isset($outputKeys[$V[1]])) {
$output[$counter] = [$V[0], $V[1]];
$outputKeys[$V[0]] = &$output[$counter];
$outputKeys[$V[1]] = &$output[$counter];
$counter++;
}
elseif(isset($outputKeys[$V[0]]) && !isset($outputKeys[$V[1]])) {
array_push($outputKeys[$V[0]], $V[1]);
$outputKeys[$V[1]] = &$outputKeys[$V[0]];
}
elseif(!isset($outputKeys[$V[0]]) && isset($outputKeys[$V[1]])) {
array_push($outputKeys[$V[1]], $V[0]);
$outputKeys[$V[0]] = &$outputKeys[$V[1]];
}
}
var_dump($output); // [["a","b","c","f"],["d","e","h"]]
DEMO (click the execute button)
Pointers are your friends. Use them :)
The following algorithm should do what you want. It simply checks through each item and checks if it already exists in the newly created array, and if it does it adds it to that item instead of a new one:
<?php
$arr=[["a","b"],["b","c"],["d","e"],["f","c"]];
$newArr = [];
foreach ($arr as $items) {
$newKey = null;
foreach ($items as $item) {
foreach ($newArr as $newItemsKey => $newItems) {
if (in_array($item, $newItems)) {
$newKey = $newItemsKey;
break 2;
}
}
}
if ($newKey !== null) {
$newArr[$newKey] = array_merge($newArr[$newKey], $items);
} else {
$newArr[] = $items;
}
}
$newArr = array_map('array_unique', $newArr);
print_r($newArr);
Output:
Array
(
[0] => Array
(
[0] => a
[1] => b
[3] => c
[4] => f
)
[1] => Array
(
[0] => d
[1] => e
)
)
DEMO
This is solution I get for now.
$arr=[["a","b","c","f"],["d","e"]];
$sortedArray = sortFunction($arr,0,array());
function sortFunction($old,$index,$new) {
if ($index == sizeof($old)) return $new;
for ($i = 0; $i<sizeof($new); $i++) {
if (count(array_intersect($new[$i],$old[$index]))) {
$new[$i] = array_unique(array_merge($old[$index],$new[$i]), SORT_REGULAR);
return sortFunction($old,$index + 1,$new);
}
}
$new[] = $old[$index];
return sortFunction($old,$index + 1,$new);
}

Recursive Search Function

I have strings that come in the form of "IT/Internet/Web Development/Ajax". I parse it and make a JSON Object like
[{
"name": "IT",
"subcategories":[
{
"name": "Internet",
"subcategories" : [
{
"name": "Web Development",
"subcategories" : [
{
"name":"Ajax"
}]}]}]
I create the JSON object by doing
$input = "IT/Internet/Web Development";
$items = explode("/", $input);
$parent = null;
$firstObject = null;
while (count($items))
{
$object = new StdClass();
$item = array_shift($items);
$object->name = $item;
if (count($items) == 0) {
$object->subcategories=NULL; // I made this null in order to know that this is the last item of the string that comes in
}
if ($parent)
$parent->subcategories = array($object);
else
$firstObject = $object;
$parent = $object;
}
array_push($category_collection, $firstObject); //$category_collection is an array
}
When another string comes in, for example "IT/Internet/Browsers", I want to be able to parse the categories that were created and place "Browsers" in the correct place as a subcategory of Internet, so then my JSON object would look like
[{
"name": "IT",
"subcategories":[
{
"name": "Internet",
"subcategories" : [
{
"name": "Web Development",
"subcategories" : [
{
"name":"Ajax"
}],
{
"name":"Browsers"
}}]}]
I'm having problems writing a recursive function that will just loop the JSON Object to categorize everything in the right place. What I am doing as of right now is
$arrlength = count($category_collection); //count the size of the array
$input = "IT/Internet/Browsers";
$items = explode("/",$input);
$tempVariable = array_shift($items);
$flag = false;
for ($x = 0; $x < $arrlength; $x++) {
//Here I check if the first a category with that name already exists
if ($category_collection[$x]['name'] == $tempVariable) {
$flag = true;
//Now here is where im having problems doing the recursion to check if the subcategory2 already exists and then if subcategory 3 and so on...
}
}
If anybody could guide me on the right direction that would be greatly appreciated
Here is a full function, it should work, you can convert it to json after, if you need:
$categoriesCollection = array();
$input = "IT/Internet/Web Development";
updateCategoriesCollection(explode('/', $input), $categoriesCollection);
$input = "IT/Internet/Browsers";
updateCategoriesCollection(explode('/', $input), $categoriesCollection);
function updateCategoriesCollection(array $categoriesList, array &$categoriesCollection)
{
$name = array_shift($categoriesList);
$category = null;
foreach ($categoriesCollection as $key => $value)
{
if ($value->name == $name)
{
$category = $value;
break;
}
}
if (!$category)
{
$category = new StdClass;
$category->name = $name;
$categoriesCollection[] = $category;
}
if (!empty($categoriesList))
{
if (empty($category->subcategories)) $category->subcategories = array();
updateCategoriesCollection($categoriesList, $category->subcategories);
}
}
var_dump($categoriesCollection);
Output:
Array
(
[0] => stdClass Object
(
[name] => IT
[subcategories] => Array
(
[0] => stdClass Object
(
[name] => Internet
[subcategories] => Array
(
[0] => stdClass Object
(
[name] => Web Development
)
[1] => stdClass Object
(
[name] => Browsers
)
)
)
)
)
)
Try this please (Not tested, maybe some error to correct, i am here to help):
function boolean check_is_good($array, $input)
{
$element = array_shift($input);
for ($x = 0; $x < count($array), $x++) {
if ($array[$x]['name'] == $element){
if ((!isset($array[$x]['subcategories']) && (count($input) == 0))
return (true);
else if ((!isset($array[$x]['subcategories']) && (count($input) != 0))
return (false);
$newArray = $array[$x]['subcategories'];
return (check_is_good($newArray, $input));
}
}
return (false);
}
The function return true is all is in the correct place
You have to pass in parameter1 an array that represent your JSON ($category_collecton in your example)
You have to pass in parameter2 an array with all elements ($items in your example)

php get array's data size

Having this array:
Array
(
[_block1] => Array
(
[list] => Array
(
[sub-list] => Array
(
)
)
[links] => Number
[total] => Number
...
)
[_block2] => Array
(
[#attributes] => Array
(
)
[title] => ...
[data] => Array ()
...
)
[_block3] => Array
(
..
)
)
Those blocks contain data returned by api. Knowing that each api returns data in a different way/structure I need to measure/calculate the data/size inside of each and one of them and then do if data > X or < do something.
Is it possible? I have searched google but I only found count() and that isn't what I need to make this work.
Edit:
Each and of the those blocks contain many other sub blocks, and I was thinking of calculating the data size in bytes, because count wont do the job here.
echo mb_strlen(serialize((array)$arr), '8bit');
If I understood well your question, you need the size of each "block" subarray inside the main array.
You can do something like this:
$sizes = array();
foreach($returnedArray as $key => $content) {
$sizes[$key] = count($content);
}
The $sizes array will be an associative array which the various "block"s as keys and the size of the data as values.
Edit:
after the edit of the question, if the data inside the innermost arrays are strings or integers you can use a function like this:
function getSize($arr) {
$tot = 0;
foreach($arr as $a) {
if (is_array($a)) {
$tot += getSize($a);
}
if (is_string($a)) {
$tot += strlen($a);
}
if (is_int($a)) {
$tot += PHP_INT_SIZE;
}
}
return $tot;
}
assuming to have only ASCII-encoded strings.
To get the size in bytes you can use the below code.
$serialized = serialize($foo);
if (function_exists('mb_strlen')) {
$size = mb_strlen($serialized, '8bit');
} else {
$size = strlen($serialized);
}
I hope it will be helpful.
Do you mean something like this?
$x = 32;
foreach($blocks as $key => $block)
{
if(getArraySize($block) < $x)
{
//Do Something
}else
{
//Do another thing
}
}
//Recursive function
function getArraySize($array)
{
$size = 0;
foreach($array as $element)
{
if(is_array($element))
$size += getArraySize($element);
else
$size += strlen($element);
}
return $size;
}

Categories