此处将为大家介绍关于硬PHP坚果:如何将项目插入一个关联数组?的详细内容,此外,我们还将为您介绍关于c#–如何按顺序将项目插入列表?、PHPmysqli–从准备好的语句返回一个关联数组、php–关联数
此处将为大家介绍关于硬PHP坚果:如何将项目插入一个关联数组?的详细内容,此外,我们还将为您介绍关于c# – 如何按顺序将项目插入列表?、PHP mysqli – 从准备好的语句返回一个关联数组、php – 关联数组的数组,向关联数组添加新元素、php – 动态关联数组?的有用信息。
本文目录一览:- 硬PHP坚果:如何将项目插入一个关联数组?
- c# – 如何按顺序将项目插入列表?
- PHP mysqli – 从准备好的语句返回一个关联数组
- php – 关联数组的数组,向关联数组添加新元素
- php – 动态关联数组?
硬PHP坚果:如何将项目插入一个关联数组?
是的,这是一个棘手的问题;一个数组(没有副本),而不是任何奇数数组.让我解释一下,让我们从这里开始;
$a = array ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5, 'six' => 6 ) ;
假装这个阵列很长,超过一百长.我一步一步地循环它,但在某些时候(让我们弥补这发生在第二个项目)会发生一些事情.也许数据很时髦.然而,我们需要添加一些项目以供以后处理,然后继续循环,而不会丢失当前位置.基本上,我想做这样的事情;
echo current ( $a ) ; // 'two'
array_insert ( $a, 'four', 'new_item', 100 ) ;
echo current ( $a ) ; // 'two'
array_insert的定义是($array,$key_where_insert_happens,$new_key,$new_value);当然$new_key和$new_value应该包装在一个数组包装器中,但这就是现在的重点.以上是我在上面的代码运行后想要看到的内容;
print_r ( $a ) ; // array ( 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'new_item' => 100, 'five' => 5, 'six' => 6 ) ;
echo current ( $a ) ; // 'two'
每当你使用array_splice,array_slice,array_push或大多数其他数组fiddling函数时,你基本上都会创建一个数组的副本,然后你可以将它复制回来,但这会破坏对原始数组和位置的引用,并且我上面的循环打破了.我可以使用直接引用(即$a [‘new_item’] =’what;)或将它放在最后,但这些都不会将项插入到给定位置.
任何接受者?如何直接在关联数组中进行真正的插入(在其他地方处理)?到目前为止我唯一的解决方案是;
>记录位置(当前())
>做splice / insert(array_slice)
>用new覆盖旧数组($old = $new)
>搜索新位置(首先重置()然后循环找到它[!!!!!!])
当然,有一种更好,更简单,更优雅的方式来做一些目前笨拙,沉重和拙劣蹒跚的事情?为什么没有可以快速帮助解决的array_set_position($key)函数,或者是直接在同一个数组(或两者)上工作的array_insert?
解决方法:
也许我没有正确理解你,但你有没有看过array_splice()
?
这answer可能也会引起你的兴趣.
会这样的吗?
function array_insert($input, $key, $value)
{
if (($key = array_search($key, array_keys($input))) !== false)
{
return array_splice($input, $key, 1, $value);
}
return $input;
}
这是我能想到的最好的:
$a = array
(
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
'six' => 6,
);
ph()->Dump(next($a)); // 2
array_insert($a, 'four', array('new_item' => 100));
ph()->Dump(current($a)); // 2
function array_insert(&$array, $key, $data)
{
$k = key($array);
if (array_key_exists($key, $array) === true)
{
$key = array_search($key, array_keys($array)) + 1;
$array = array_slice($array, null, $key, true) + $data + array_slice($array, $key, null, true);
while ($k != key($array))
{
next($array);
}
}
}
ph()->Dump($a);
/*
Array
(
[one] => 1
[two] => 2
[three] => 3
[four] => 4
[new_item] => 100
[five] => 5
[six] => 6
)
*/
我不认为可以设置数组内部指针without looping.
c# – 如何按顺序将项目插入列表?
我有一个DateTimeOffset对象的列表,我想按顺序插入新的列表.
List<DateTimeOffset> TimeList = ... // determine the order before insert or add the new item
对不起,需要更新我的问题.
List<customizedClass> ItemList = ... //customizedClass contains DateTimeOffset object and other strings,int,etc. ItemList.sort(); // this won't work until set data comparison with DateTimeOffset ItemList.OrderBy(); // this won't work until set data comparison with DateTimeOffset
有没有人可以帮我把DateTimeOffset作为.OrderBy()的参数?
我也试过
ItemList = from s in ItemList orderby s.PublishDate descending // .PublishDate is type DateTime select s;
但是,它返回此错误消息,
Cannot implicitly convert type ‘System.Linq.IOrderedEnumerable’ to ‘System.Collections.Gerneric.List’. An explicit conversion exist (are you missing a cast?)
解决方法
ItemList = (from s in ItemList orderby s.PublishDate descending select s).ToList();
或者将排序的列表分配给另一个变量
var sortedList = from s in ....
PHP mysqli – 从准备好的语句返回一个关联数组
这是我的结构到目前为止:
$query = $c->stmt_init(); $query->prepare("SELECT e._id,e.description,e.eventDate,e.eventTime,e.address,e.locationDescription,i.guestId,r.guestId IS NOT NULL AS 'RSVP-ed' FROM eventList AS e JOIN inviteList AS i ON e._id = i.eventId LEFT JOIN rsvpList AS r ON r.eventId = e._id AND i.guestId = r.guestId JOIN guestList AS g ON g._id = i.guestId WHERE g.groupName = ?"); $query->bind_param('s',$groupName); if ($result = $query->execute()){ $a = $result->fetch_array(MysqLI_ASSOC); // this doesn't work :/ } else{ error_log ("Didn't work"); }
如你所见,我有很多列被传回,所以我不需要将它们绑定到一个变量.
除此之外,最终目标是将json编码的关联数组传回给我的应用程序的其余部分.
我已经在PHP文档和堆栈交换中查找了这个问题,我发现了一些建议,但是我似乎无法让他们工作.有人可以借吗?
$query = $c->stmt_init(); $query->prepare("SELECT e._id,$groupName); if ($query->execute()){ $result = $query->get_result(); $a = $result->fetch_array(MysqLI_ASSOC); // this does work :) } else{ error_log ("Didn't work"); }
php – 关联数组的数组,向关联数组添加新元素
array(2) { [0]=> array(2) { ["id"]=> string(2) "34" ["total"]=> string(6) "122337" },[1]=> array(2) { ["id"]=> string(2) "43" ["total"]=> string(6) "232337" } }
我想为每个子数组添加一个新的键值,例如,它会像这样结束:
array(2) { [0]=> array(2) { ["id"]=> string(2) "34" ["total"]=> string(6) "122337" ["newkey"]=> string(6) "hihihi" },[1]=> array(2) { ["id"]=> string(2) "43" ["total"]=> string(6) "232337" ["newkey"]=> string(6) "hihihi" } }
我该怎么办?
我试过像这样的foreach:
foreach($exterior_array as $inside_array) { $inside_array['newkey'] = "hihihi"; }
但是一旦进入foreach,值就不会被保存.
解决方法
foreach($exterior_array as $inside_array) { $inside_array['newkey'] = "hihihi"; }
But once I get inside the foreach,the values are not saved.
那是因为你正在通过$inside_array处理数组的副本.您可以通过使$inside_array成为原始值的别名来访问要更改的“orignal”值;使用参考:
foreach($exterior_array as &$inside_array) { ^- set the reference $inside_array['newkey'] = "hihihi"; } unset($inside_array); ^^^^^^^^^^^^^^^^^^^^^- remove the reference
与http://php.net/foreach比较
php – 动态关联数组?
我有阵列返回
$header_html = array(1=>array('width'=>40,
'sort_case'=>23,
'title'=>'AxA'),
2=>array('width'=>50,
'sort_case'=>7,
'title'=>'B2B'),
3=>array('width'=>100,
'sort_case'=>12,
'title'=>'C12')
);
我想获得依赖于$header_array = array(‘AxA’,’B2B’,’C12′)的新数组
举些例子:
if have $header_array=array('C12','B2B','AxA').
新的$header_html将是:
$header_html = array(
1=>array('width'=>100,
'sort_case'=>12,
'title'=>'C12'),
2=>array('width'=>50,
'sort_case'=>7,
'title'=>'B2B'),
3=>array('width'=>40,
'sort_case'=>23,
'title'=>'AxA')
);
等等…
有人知道怎么做吗?
解决方法:
您可以使用usort使用自定义比较函数对数组进行排序:
function cmp($a, $b) {
// Sort via $a['title'] and $b['title']
}
usort($header_html, 'cmp');
诀窍是提供一个比较功能,可以做你想要的.要简单地按标题排序,您可以使用:
function cmp($a, $b) {
if ($a['title'] == $b['title'])
return 0;
// usually return -1 if $a < $b, but we're sorting backwards
return ($a['title'] < $b['title'] ? 1 : -1;
}
今天关于硬PHP坚果:如何将项目插入一个关联数组?的分享就到这里,希望大家有所收获,若想了解更多关于c# – 如何按顺序将项目插入列表?、PHP mysqli – 从准备好的语句返回一个关联数组、php – 关联数组的数组,向关联数组添加新元素、php – 动态关联数组?等相关知识,可以在本站进行查询。
本文标签: