gpt4 book ai didi

javascript - PHP复制对象数组的一个字段到一个简单的数组

转载 作者:行者123 更新时间:2023-11-30 16:28:13 25 4
gpt4 key购买 nike

我有一个包含多个字段的 PHP 对象数组,我需要提取所有相等的字段并将它们存储到单独的数组中,因为我需要将它们传递给 bash 脚本,我更喜欢有单独的数组,因为 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

然后我在 Javascript 中创建了 Object Array,例如:

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']) . "'";

关于javascript - PHP复制对象数组的一个字段到一个简单的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33780262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com