4 Replies - 395 Views - Last Post: 19 July 2013 - 02:10 AM Rate Topic: -----

#1 kukuviza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 08-August 12

Trouble with array/json

Posted 15 July 2013 - 08:49 AM

Hello, I am using a json string to visualize some items.
My json structure is like this
 
  [{"data":1373925600,
  "titolo":"prova",
  "testobreve":"xxx"},
 {"data":1373925600,
  "titolo":"prova",
  "testobreve":"xxx"}
  ]




I am able to insert items, delete or modify them. All goes smoothly until I try to sort this array.
I have to sort it by the key "data" and I use this function
function aasort (&$array, $key, $ascdesc) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
		echo  $sorter[$ii]."  ";
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
	
    $array=$ret;
    if ($ascdesc == "desc")
	$array = array_reverse($array);
	
		
}


the sorting procedure works good but then when I encode the produced string into a json I get this:
 
  {"1":{"data":1373925600,
  "titolo":"prova",
  "testobreve":"xxx"},
  {"0":{"data":1373925600,
  "titolo":"prova",
  "testobreve":"xxx"}
 ]


with the index before the items. Is it possible to get rid of those indexes?

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with array/json

#2 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,829
  • Joined: 30-April 10

Re: Trouble with array/json

Posted 15 July 2013 - 08:55 AM

Have you tried json_decode? if you set the second parameter to true it will give you an associative array of the data.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Trouble with array/json

Posted 15 July 2013 - 09:21 AM

Are you sorting by the data field? Maybe this?

<?php

function obj_sort_asc($a, $b)
{
	return $a->data - $b->data;
}

function obj_sort_desc($a, $b)
{
	return $b->data - $a->data;
}

$forSort = '[{"data":1383925600,"titolo":"prova","testobreve":"xxx"},{"data":1373925600,"titolo":"prova","testobreve":"xxx"}]';

print('Before: ' . $forSort) ."\n";

$a = json_decode($forSort);
usort($a, 'obj_sort_asc');

print('After (asc): ' . json_encode($a)) . "\n";

print('Before: ' . $forSort) . "\n";

$b = json_decode($forSort);
usort($b, 'obj_sort_desc');

print('After (desc): ' . json_encode($b)) . "\n";


 php -f jsonsort.php 
Before: [{"data":1383925600,"titolo":"prova","testobreve":"xxx"},{"data":1373925600,"titolo":"prova","testobreve":"xxx"}]
After (asc): [{"data":1373925600,"titolo":"prova","testobreve":"xxx"},{"data":1383925600,"titolo":"prova","testobreve":"xxx"}]
Before: [{"data":1383925600,"titolo":"prova","testobreve":"xxx"},{"data":1373925600,"titolo":"prova","testobreve":"xxx"}]
After (desc): [{"data":1383925600,"titolo":"prova","testobreve":"xxx"},{"data":1373925600,"titolo":"prova","testobreve":"xxx"}]

Was This Post Helpful? 0
  • +
  • -

#4 kukuviza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 08-August 12

Re: Trouble with array/json

Posted 15 July 2013 - 11:08 AM

Yes, exactly, I am trying to sort by the field "data". I tried your code, I don't get the expected result though. The field "data" should represent a date. It is a string converted with strtotime.
Apparently, using desc function I get 1405375200 (that is July 15 2014) after 1387062000 (that is Dec 15 2013). I don't know where the problem is. This is my code:

$eventi=json_decode($string);


$data = $_POST['data'];
$titolo = $_POST['titolo'];
$testobreve = $_POST['testobreve'];

$data= strtotime($data);

$array = array(
    "data" => $data,
    "titolo" => $titolo,
    "testobreve" => $testobreve
);

array_unshift($eventi, $array);

function obj_sort_asc($a, $B)/>/>
{
	return $a->data - $b->data;
}

function obj_sort_desc($a, $B)/>/>
{
	return $b->data - $a->data;
}

usort($eventi,"obj_sort_desc");

$encoded=json_encode($eventi);
file_put_contents('eventi.json', $encoded);



This post has been edited by kukuviza: 15 July 2013 - 11:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 kukuviza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 08-August 12

Re: Trouble with array/json

Posted 19 July 2013 - 02:10 AM

I am still stuck. Could someone give me a hint of where I am getting things wrong? Any help is appreciated...thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1