Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been trying to solve this problem for the better part of two days with no success. I am trying combine/add to a json array that is stored in a .json file on my server using php.

This is a short version of what I'm trying to combine.

box.json:

[{"date":"25.4.2013 10:40:10"},{"comment":"some text"},{"comment":"some more text"}]

posted json:

[{"date":"25.4.2013 10:45:15"},{"comment":"another quote"},{"comment":"quote"}]

This is what I need.

[{"date":"25.4.2013 10:40:10"},{"comment":"some text"},{"comment":"some more text"},
{"date":"25.4.2013 10:45:15"},{"comment":"another quote"},{"comment":"quote"}]

This is what I get. (an array inside an array)

[{"date":"25.4.2013 10:40:10"},{"comment":"some text"},{"comment":"some more text"},
[{"date":"25.4.2013 10:45:15"},{"comment":"another quote"},{"comment":"quote"}]]

This is my code:

<?php
$sentArray = $_POST['json'];
$boxArray = file_get_contents('ajax/box.json');
$sentdata = json_decode($sentArray);
$getdata = json_decode($boxArray);
$sentdata[] = $getdata;   /* I also tried array_push($sentdata, $getdata); */
$json = json_encode($sentdata);
$fsize = filesize('ajax/box.json');
if ($fsize <= 5000){
    if (json_encode($json) != null) { /* sanity check */
    $file = fopen('ajax/box.json' ,'w+');
    fwrite($file, $json);
    fclose($file);
}else{
    /*rest of code*/
}
?>

Please help my sanity is starting to come in to question.

share|improve this question
add comment

3 Answers

up vote 0 down vote accepted

Instead of this:

$sentdata[] = $getdata;   /* I also tried array_push($sentdata, $getdata); */

Try:

$combinedData = array_merge($sentData, $getData);
$json = json_encode($combinedData);

By using array_merge you're combining the arrays into one instead of adding one array as a value into the other.

Note that I changed the name of your resulting data - try to avoid variables with the same name and different capitalization, it will make things much easier to understand (for you and for future developers supporting your code).

Cheers

share|improve this answer
    
I need to add values, It should allow for duplicates. And you are right my my naming practices are messy. I'm a designer learning to develop. Thanks for the advice. –  unsalted Apr 25 '13 at 19:37
    
Nick, it will allow for duplicates since you are using Objects as values and not scalars. Let me know if it's unclear how to use this. –  Madbreaks Apr 25 '13 at 19:45
    
I tried running array merge awhile ago and it didn't work (I must have done something wrong), but it appears you are correct it works now. Thanks! –  unsalted Apr 25 '13 at 19:53
    
Glad to hear it Nick, good luck ramping up on development! –  Madbreaks Apr 25 '13 at 19:58
add comment

here is your problem

$sentdata[] = $getdata; 

use foreach

foreach($getdata as $value)
    $sentdata[] = $value;

UPDATE: but i think you need this for $sentdata not $getdata

foreach($senttdata as $value)
    $getdata[] = $value;

then put $getdata to your file.

share|improve this answer
    
brilliant! That did it! –  unsalted Apr 25 '13 at 19:22
    
Iteration is not the best answer here - depending on the size of the data set this could become very expensive. –  Madbreaks Apr 25 '13 at 19:23
    
@Madbreaks is there a any other way ? what is array_merge exactly do ? just write a name , and your code is best ? what is this function exactly do ? –  CooPer Apr 25 '13 at 19:34
    
I explained what array_merge does. I also provided a link to the official PHP docs. Is that not sufficient? –  Madbreaks Apr 25 '13 at 19:38
    
data set will write over at 5kb, would it be a problem? –  unsalted Apr 25 '13 at 19:39
show 1 more comment
$box = json_decode(file_get_contents('ajax/box.json'));
$posted = json_decode($_POST['json']);
$merge = array_merge ((array)$box,(array)$posted);

Casting (array) prevent error if $box or $posted become null or false, it will be an empty array

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.