我想使用 JSON 对象中返回的值在 jQuery 中创建一个关联数组。JSON 对象是动态创建的:
[{"name":"key1","value":"value1"},{"name":"key2","value":"value2"},{"name":"key3","value":"value3"},{"name":"key4","value":"value4"}]
Run Code Online (Sandbox Code Playgroud)
我想使用 JSON 返回的值创建此格式的关联数组:
aResult = {key1 : 'value1', key2 : 'value2', key3 : 'value3', key4 : 'value4'};
Run Code Online (Sandbox Code Playgroud)
目前,当我迭代 JSON 对象时,我可以在控制台中看到所需的数组结构
$.each(jData, function(k, v) {
if (v.name.toLowerCase().indexOf("answer") >= 0) {
name = v.name;
value = v.value;
console.log(name + ' : ' + value); //returns the structure I wish
};
});
Run Code Online (Sandbox Code Playgroud)
但是当我在循环中添加这段代码来创建数组时
var aResult = {name:value}
Run Code Online (Sandbox Code Playgroud)
它返回[object Object]
我缺少什么?我该如何前进?任何帮助表示赞赏。
我想剖析这样的数组:
[
"ID",
"UUID",
"pushNotifications.sent",
"campaigns.boundDate",
"campaigns.endDate",
"campaigns.pushMessages.sentDate",
"pushNotifications.tapped"
]
Run Code Online (Sandbox Code Playgroud)
改成这样的格式:
{
"ID" : 1,
"UUID" : 1,
"pushNotifications" :
{
"sent" : 1,
"tapped" : 1
},
"campaigns" :
{
"boundDate" : 1,
"endDate" : 1,
"pushMessages" :
{
"endDate" : 1
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我能够以类似键路径的方式在关联数组上设置一个值,那就太好了:
//To achieve this:
$dissected['campaigns']['pushMessages']['sentDate'] = 1;
//By something like this:
$keypath = 'campaigns.pushMessages.sentDate';
$dissected{$keypath} = 1;
Run Code Online (Sandbox Code Playgroud)
如何在 PHP 中做到这一点?
我已经在谷歌上搜索了几个小时,但我无法从中得到任何线索。我有一个像这样的多维关联数组:
$mArray = array(
array("m" => "0"),
array("m" => "1"),
array("m" => "1")
);
Run Code Online (Sandbox Code Playgroud)
我想使用 PHP GET 请求创建数组:
mywebsite.com/file.php?.......what do I put here?.....
Run Code Online (Sandbox Code Playgroud) foo()
{
mapfile -t arr <<< "${1}"
for i in "${!arr[@]}"
do
if [ -z "${arr[$i]}" ]
then
unset arr[$i]
fi
done
}
Run Code Online (Sandbox Code Playgroud)
当我传递一个包含一些内容的变量(基本上是一个大字符串)时,我想:
示例(为了紧凑性,不包括空行)
google https://www.google.com
yahoo https://www.yahoo.com
microsoft https://www.microsoft.com
Run Code Online (Sandbox Code Playgroud)
数组应该看起来像
[ google ] == https://www.google.com
[ yahoo ] == https://www.yahoo.com
[ microsoft ] == https://www.microsoft.com
Run Code Online (Sandbox Code Playgroud)
对于这两点,我在 bash 手册中没有找到任何好的解决方案,foo您看到的函数是一种创建数组的 hack,只有在此之后它才会遍历整个数组并删除字符串为 null 的条目。
因此,第 2 点得到了一个解决方案,可能是一个低效的解决方案,但它有效,但第 1 点仍然没有好的解决方案,read据我所知,替代解决方案是在迭代时创建一个数组。
你知道如何改善这一点吗?
在这里,我们以完全相同的方式初始化两个关联数组,但arr_A在顶层初始化,而在函数内部初始化:arr_Barr_Aarr_Bfoo
#!/bin/bash
declare -A arr_A
arr_A[bar]=42
function foo() {
declare -A arr_B
arr_B[bar]=58
echo "content of arr_B inside foo = ${arr_B[@]}"
}
foo
echo "content of arr_A on top level = ${arr_A[@]}"
echo "content of arr_B on top level = ${arr_B[@]}"
Run Code Online (Sandbox Code Playgroud)
预期输出:
content of arr_B inside foo = 58
content of arr_A on top level = 42
content of arr_B on top level = 58
Run Code Online (Sandbox Code Playgroud)
实际输出:
content of arr_B inside foo = 58
content …Run Code Online (Sandbox Code Playgroud) 我有一组值,我按照它们发生的顺序推入数组
$valsArray = array();
//I process each value from a file (code removed for simplicity)
//and then add into the array
$valsArray[] = $val;
Run Code Online (Sandbox Code Playgroud)
如何将值转换为关联数组,而不是$key of associative array仅在不存在的情况下插入值(as ).如果确实存在,则将count($value of associative array)递增1.我试图找到一种更有效的处理这些值的方法,与我现在正在做的相比.
我需要从POST的关联数组构建更新查询
POST包含多个关联数组和几个键/值对
我可以理清要使用哪些数组,但我仍然坚持构建更新查询字符串
array1 (
i => 1 // This is used for each WHERE clause and is the row id
a => 2
b => 3
c => 4
)
array2 (
i => 2
a => 2
b => 3
c => 4
)
Run Code Online (Sandbox Code Playgroud)
我需要:"UPDATE表SET a ='2',b ='3',c ='4'WHERE id ='array [i]'"
我已经尝试了各种各样的foreach,而内部方法拼凑在一起从其他帖子拼凑而来,但没有一个处理这个特定的问题.任何帮助,将不胜感激.谢谢
我习惯于处理我的关联数组PHP样式,我会有类似下面的内容
array[day][time] = count
Run Code Online (Sandbox Code Playgroud)
这使我可以轻松计算出现的次数和易于使用的结构.在我的例子中,我正在从数据库中读取结果集,并且将这样的东西用于工作将非常有帮助
我如何在Coldfusion中做同样的事情?
我有以下代码
my %ages = ();
$a1 = "Michael Caine";
$a2 = "Dirty Den";
$a3 = "Angie";
$a4 = "Willy";
$a5 = "The Queen Mother";
$ages{$a1} = 39;
$ages{$a2} = 34;
$ages{$a3} = 27;
$ages{$a4} = "21 in dog years";
$ages{$a5} = 108;
print $age->{$a1};
Run Code Online (Sandbox Code Playgroud)
但这不是印刷品.我不想使用键或值.如何使用'$age->{$a1}'类型语法访问哈希的内容?
我是新来的cakephp.我需要发送两个变量view.在codeigniter中它很容易
$data['var1'] = 'One';
$data['var2'] = 'Two';
$this->load->view('myview',$data);
Run Code Online (Sandbox Code Playgroud)
现在Cakephp,我有一个名为函数名的控制器,我search()在其中发送一个关联数组view.
$gal_providers = $this->GalProvider->getListByCategory(3,$location_id,false,9);
$this->set("gal_providers",$gal_providers);
Run Code Online (Sandbox Code Playgroud)
但我需要将变量发送$location_id到视图.我该怎么发送?
我一起读了文章Using set()和compact(),但是我没有得到我想要的解决方案.
arrays ×5
php ×4
bash ×2
cakephp ×1
coldfusion ×1
controller ×1
get ×1
implode ×1
javascript ×1
jquery ×1
key ×1
perl ×1
view ×1