0

I must receive in PHP from Ajax/Javascript an array like below:

$search = $_POST['query'];
$search = array(
    'category1' => array('include' => array(93, 52),'exclude' => array(72)),
    'category2' => array('include' => array(93, 52)),
    'category3' => array('exclude' => array(72)),
    'category4' => array()
);

In my javascript page, the array is built by user actions before send it. The user select categories and items to build it.

For example, my final javascript array must look like below :

my_array = '{
        "category1":{
            "include":["93","52"],
            "exclude":["72"]
        },
        "category2":{
            "include":["93","52"]
        },
        "category3":{
            "exclude":["72"]
        },
        "category4":[]
        }';

i try many query.push but it doesn't work when many categories are selected.

I am unable to:

  • Add item in new category when a category already exists in my global array
  • Remove a specific item from specified category because the identifier is a variable:

(example: remove '52' in "category2":{"include":["93","52"]},

Description:

  • A category item id can be placed in 'include' section OR 'exclude' section of specified category (but not in both)

  • An item (tag) under category can have 3 status (classes):

    • btn-default: item no selected, item id must be remove of specified array
    • btn-danger: item excluded, item id must be present in 'exclude' list of specified array
    • btn-primary: intem included, item id must be present in 'include' list of specified array

Here is the jsfiddle link: JSFIDDLE

thanks.

3
  • 1
    Can you post your AJAX code please? Commented May 9, 2017 at 16:25
  • Sounds like you are much better off sending the array in json format if possible. Commented May 9, 2017 at 16:31
  • I have no ajax yet, i must prepare datas to send before.. I will send this array in POST to my PHP script. I think my question is not clear.. I can clarify Commented May 9, 2017 at 16:32

5 Answers 5

1

In Javascript, this is an array: [] and this is an object: {}. So this is how I would create my array in JS:

var my_array = [
  {'category1':{'include':[1,2,3], 'exclude':[5]}},
  {'category2':{'include':[3,4,5], 'exclude':[6,7,8]}},
  {'category3':{'include':[1,2], 'exclude':[7]}}
  ];

Here is a fiddle demonstrating how to push a new value into the array: https://jsfiddle.net/h15emr8j/

If you want to push a value into a specific part of the array, here's how you do it:

my_array[0].category1.exclude.push(100);
Sign up to request clarification or add additional context in comments.

9 Comments

OK, and how to push a simple value, example '100' to obtain: {'category3':{'include':[1,2], 'exclude':[7,100]}}
@mytom simply use my_array.push(value)
@mytom look at my answer. I updated it with a fiddle so you can see how to push new values into the array
OK, i have try this solution and see another problem. I use a variable (category) to store category value, and this variable is not interpreted as value. You can see that here: link. Do you know how to correct that?
@mytom you have to push category into the array. I updated the code: jsfiddle.net/t02donb0/4
|
0

The simplest solution could be something like this.

const jsonArray = [{
  category: {
    include: ["93", "52"],
    exclude: ["72"]
  }
  ..
}];

3 Comments

Yes but my problem is to add a value to this array.. I don't know to do that. Example when add '100' to obtain: exclude: ["72","100"]
then you have to do this for example let value: string = 100; jsonArray[0].category.exclude.push(value);
To clarify my problems, i made a new fiddle. Can you look at JSFIDDLE. ?
0

you can follow below logic to achieve what you want :

var my_array={}; // creating empty array object

my_array.category1={}; // creating new filed in my_array

var include1 = new Array(); //creating include array for category 1
include1.push("93"); // push include values
include1.push("52");

var exclude1 = new Array(); // creating exclude array for category1, if you want
exclude1.push("72"); // push exclude values


my_array.category1.include=include1; // finally add it to category1
my_array.category1.exclude=exclude1;

alert(JSON.stringify(my_array)); // convert your object to JSON string

Now if you want to add new value to include and exclude of category1 then you simply need to push those values in their respective arrays.

2 Comments

OK thanks, but I use a variable (category) to store category value, and this variable is not interpreted as value. You can see that here: link. Do you know how to correct that?
To clarify my problems, i made a new fiddle. Can you look at JSFIDDLE.
0

JS code

my_array = {"category1": {"include": ["93", "52"], "exclude": ["72"]}, "category2": {"include": ["93", "52"]}, "category3": {"exclude": ["72"]}, "category4":[]};
console.log(my_array);
$.post("array.php", {query: my_array}, function( data ) {
    console.log(data);
}, "text");

PHP code

<?php
    $search = $_POST['query'];
    print_r($search["category1"]["exclude"]);

Comments

0

So a few good things to learn here. Initializing arrays in javasript:

var arr = ['include','exclude']; 
arr['include'] = []; 
arr['exclude'] = []; 

Next we need use ajax to send our array to the php script:

var jsonString = JSON.stringify(arr);

$.ajax({
url: 'myscript.php',
type: 'POST',
data:{
      arr: jsonString
     },
success: function(result){
//continue with results
},
error: function(result){
//continue with results
}
});

Finally on the php side:

$arr = json_decode($_POST['arr']);

//code to execute

echo json_encode($arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.