Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个PHP foreach循环中的$value是一个数组?_Php_Jquery_Arrays_Foreach - Fatal编程技术网

为什么这个PHP foreach循环中的$value是一个数组?

为什么这个PHP foreach循环中的$value是一个数组?,php,jquery,arrays,foreach,Php,Jquery,Arrays,Foreach,在jQuery代码中,我创建了如下对象: var fields = [ ['title','New Title'], ['description', 'New Description'], ['stuff', ['one','two','three']] ]; var objectsArray=[ { fields

在jQuery代码中,我创建了如下对象:

var fields = [
              ['title','New Title'],
              ['description', 'New Description'],
              ['stuff', ['one','two','three']]
             ];
var objectsArray=[
                  {
                   fields:fields
                  }
                 ];

var saveObject = {
                  command: 'test',
                  callback:'testResopnse',
                  objects: objectsArray
                 }
saveDataAsParseObjects(saveObject)

function saveDataAsParseObjects(saveObj){
        $.ajax({
              type: "POST",
              dataType: "json",
              url: "php/parseFunctions.php",
              data: {data:saveObj},
              success: function(response) { 
                 console.log(response);
              },
              error: function(response) {
                 console.log(response);
              }
        });
};
var fields = {
    title: 'New Title',
    description: 'New Description',
    stuff: ['one','two','three']
};
然后通过ajax将其发送到如下PHP页面:

var fields = [
              ['title','New Title'],
              ['description', 'New Description'],
              ['stuff', ['one','two','three']]
             ];
var objectsArray=[
                  {
                   fields:fields
                  }
                 ];

var saveObject = {
                  command: 'test',
                  callback:'testResopnse',
                  objects: objectsArray
                 }
saveDataAsParseObjects(saveObject)

function saveDataAsParseObjects(saveObj){
        $.ajax({
              type: "POST",
              dataType: "json",
              url: "php/parseFunctions.php",
              data: {data:saveObj},
              success: function(response) { 
                 console.log(response);
              },
              error: function(response) {
                 console.log(response);
              }
        });
};
var fields = {
    title: 'New Title',
    description: 'New Description',
    stuff: ['one','two','three']
};
在我的PHP页面中,我正在执行以下操作:

$data= $_POST['data'];

if($data['command'] == 'test'){
    testStuff($data);
}
function testStuff($data){
    $objects = $data['objects'];
    foreach($objects as $object){
        $fields = $object['fields'];
        foreach($fields as $column => $value){

            echo is_array($value) ? 'Array, ' : 'not an Array, ';
        }
    }
}
考虑到jQuery页面上的原始
字段
数组,我希望
testStuff()
返回:

“不是数组,不是数组,数组,”

但相反,它返回:

“数组,数组,数组”

为什么
$value
是这个foreach循环中的一个数组,而我希望它是一个字符串?


foreach($column=>$value形式的字段)

您在大多数嵌套foreach中迭代此集合:

var fields = [
    ['title','New Title'],
    ['description', 'New Description'],
    ['stuff', ['one','two','three']]
];
因此,每个
$value
也是一个数组,例如
['title','newtitle']
。您应该再对其进行一次迭代,或者将字段更改为如下所示的对象:

var fields = [
              ['title','New Title'],
              ['description', 'New Description'],
              ['stuff', ['one','two','three']]
             ];
var objectsArray=[
                  {
                   fields:fields
                  }
                 ];

var saveObject = {
                  command: 'test',
                  callback:'testResopnse',
                  objects: objectsArray
                 }
saveDataAsParseObjects(saveObject)

function saveDataAsParseObjects(saveObj){
        $.ajax({
              type: "POST",
              dataType: "json",
              url: "php/parseFunctions.php",
              data: {data:saveObj},
              success: function(response) { 
                 console.log(response);
              },
              error: function(response) {
                 console.log(response);
              }
        });
};
var fields = {
    title: 'New Title',
    description: 'New Description',
    stuff: ['one','two','three']
};

您需要再循环一次,因为您的
字段
数组是一个数组数组(以这个伪数组为例):

您只需再重复一次循环:

$fields = $object['fields'];
foreach($fields as $column => $value){
    foreach($value as $key => $obj) {
        echo is_array($obj) ? 'Array, ' : 'not an Array, ';
    }
}

因为javascript变量“fields”是数组的数组


当循环遍历$fields时,循环遍历这个变量(也称为objectsArray[0].fields)

,因为每个$value都是fields数组的元素。该数组的每个元素都是另一个数组

所以

是标题和新标题的数组

如果在php中调试或使用var_dump($value),您将看到输出是

array (size=2)
    0 => string 'title' (length=5)
    1 => string 'New Title' (length=9)

array (size=2)
    0 => string 'description' (length=11)
    1 => string 'New Description' (length=15)

array (size=2)
  0 => string 'stuff' (length=5)
  1 => 
array (size=3)
  0 => string 'one' (length=3)
  1 => string 'two' (length=3)
  2 => string 'three' (length=5)

因为
['stuff',['one','two','three'].
?@Daren是的,我希望第三次迭代是一个数组,但为什么前两次迭代是数组?你在尝试使用fields数组。它有三个要素。这3个元素是数组。为什么会不同?实际上我需要的是
echo is_array($value[1])数组':'不是数组'但是你对问题的解释很正确,再次感谢!