我有一个包含多个字段的PHP
Object Array
,我需要提取所有相等的字段并将它们存储到individual arrays
中,因为我需要将它们传递给bash script
,我更喜欢单独的数组,因为bash
不是面向对象的,对吗?
以下是我要做的:
<?php
$data = json_decode($_POST['data']);
$text_array = "'" . implode ("'n", $data->text) . "'";
$time_text = "'" . implode ("'n", $data->time_text) . "'";
$gender = "'" . implode ("'n", $data->gender) . "'";
$pitch = "'" . implode ("'n", $data->pitch) . "'";
$response = shell_exec("./test_bash.sh $pitch $gender $timetext $text_array 2>&1");
echo "$response";
?>
数据从javascript
通过ajax
传递到PHP
。原来的Object Array
有这样的结构:
text
time_text
gender
pitch
我在CCD_ 11中创建了CCD_
function dataClass(text, time_txt, gender, pitch, mood) {
this.text = text;
this.time_text = time_txt;
this.gender = gender;
this.pitch = pitch;
this.mood = mood;
}
for(var i = 0; i < length - 1; i++){
data.push(new dataToSynth(subtitles_trans[i].text, subtitles_trans[i].end - subtitles_trans[i].start, genere, pitch));
}
如何在PHP中将对象数组字段复制到单个数组?
您不需要使用json_decode。在您的情况下,"$_POST['data']"是一个数组,而不是一个对象。试试这个代码:
$data = $_POST['data'];
$text_array = "'" . implode ("'n", $data['text']) . "'";
$time_text = "'" . implode ("'n", $data['time_text']) . "'";
$gender = "'" . implode ("'n", $data['gender']) . "'";
$pitch = "'" . implode ("'n", $data['pitch']) . "'";
您可以将对象值强制转换为数组,只需将字符串强制转换为int即可。
<?php
class a{
public $a="a";
public $b="b";
public $c="c";
}
$a= new a();
$b= (array)$a;
var_dump($a);
/*object(a)#1 (3) {
["a"]=>
string(1) "a"
["b"]=>
string(1) "b"
["c"]=>
string(1) "c"
}*/
var_dump($b);
/*array(3) {
["a"]=>
string(1) "a"
["b"]=>
string(1) "b"
["c"]=>
string(1) "c"
}*/
所以应该是这样的:
<?php
$data =(array) json_decode($_POST['data']);
$text_array = "'" . implode ("'n", $data['text']) . "'";
$time_text = "'" . implode ("'n", $data['time_text']) . "'";
$gender = "'" . implode ("'n", $data['gender']) . "'";
$pitch = "'" . implode ("'n", $data['pitch']) . "'";
$response = shell_exec("./test_bash.sh $pitch $gender $timetext $text_array 2>&1");
echo "$response";
?>