Php 按值排序数组
我有这个帖子Php 按值排序数组,php,sql,Php,Sql,我有这个帖子 Array ( [imgurl_3] => http://localhost/wordpress/wp-content/uploads/2014/03/05-239x300.jpg [imgtekst_3] => Write a text for the slide [imgurl_4] => http://localhost/wordpress/wp-content/uploads/2014/03/img_2184-300x225.jpg [imgtekst
Array (
[imgurl_3] => http://localhost/wordpress/wp-content/uploads/2014/03/05-239x300.jpg
[imgtekst_3] => Write a text for the slide
[imgurl_4] => http://localhost/wordpress/wp-content/uploads/2014/03/img_2184-300x225.jpg
[imgtekst_4] => Write a text for the slide
[update_gallery] => Save changes )
imgurl和imgtekst末尾的数字是动态的。所以
我想将imgurl_3和imgtekst_3配对,依此类推。更新到数据库中
有任何智能PHP函数用于此吗
谢谢您可以使用for
循环
并选择相关条目:
for($i = 0; $i < $whatever; $i++) {
if(array_key_exists('imgurl_' . $i, $array)) {
$value = $array['imgurl_' . $i];
$value2 = $array['imgtekst_' . $i];
}
$sqlQuery = '<UPDATE using $value and $value2>';
}
for($i=0;$i<$whatever;$i++){
if(array_key_存在('imgurl_.$i,$array)){
$value=$array['imgurl.'$i];
$value2=$array['imgtekst.'$i];
}
$sqlQuery='';
}
这与排序有什么关系?你能再简单一点吗?你想配对什么?价值观,关键点?只有文件名?排序只是我使用的一个术语,我没有更好的词汇,但下面的答案解决了我的问题,如果$array['imgurl_0']代码>丢失了吗?和<代码>''代码>单引号?@Debflav更新内容显然只是一个伪代码占位符。@Debflav更新了答案。由于问题中缺乏细节,我假设没有“缺失”值,所以我读得太快了。只是被单引号之间的变量震惊了。无论如何,这似乎是一种未经反思的反应。业主必须给出更多解释,才能给出合理的答案。
$array = [
'imgurl_3' => 'http://localhost/wordpress/wp-content/uploads/2014/03/05-239x300.jpg',
'imgtekst_3' => 'Write a text for the slide',
'imgurl_4' => 'http://localhost/wordpress/wp-content/uploads/2014/03/img_2184-300x225.jpg',
'imgtekst_4' => 'Write a text for the slide',
'update_gallery' => 'Save changes'
];
$output = [];
foreach ($array as $key => $item) {
$intKey = filter_var($key, FILTER_SANITIZE_NUMBER_INT);
if ($intKey) {
$key = preg_replace('/_\d/', '', $key);
$output[$intKey][$key] = $item;
}
}
print_r($output);