这段代码运行后,我收到一个错误.我查找了可能的解决方案,但似乎所有内容都格式正确.
$searched = 'searched';
$count = '59';
$prop = Array();
$i = 0;
while ($i++ <= 4) {
array_push($prop[$i] = Array(
'text' => $searched,
'href' => 'http://mysite.com/?search=' . str_replace(' ', '+', $searched)
));
}
array_push($prop['Total Searches'] = $count);
Run Code Online (Sandbox Code Playgroud)
我在while循环中收到此错误5次,在while循环下收到array_push 1次.
Warning: Wrong parameter count for array_push()
Run Code Online (Sandbox Code Playgroud)
代码工作正常!但它仍然会调出错误.那么我应该压制错误吗?
如何=> $key在数组中添加值,例如:
$images = array();
array_push($images, $_FILES['file']['tmp_name'] => $_FILES['file']['name']);
Run Code Online (Sandbox Code Playgroud)
所以数组就像:
array('temporary_file_name' => 'file_name.zip');
Run Code Online (Sandbox Code Playgroud)
但我的IDE说这是无效的,不会起作用.
$dizi = array(
"tr" => "Turkey",
"uk" => "United Kingdom",
"us" => "United States"
);
Run Code Online (Sandbox Code Playgroud)
我想添加"br"=>"brasil"到数组的末尾.
谢谢.
我有一个从数据库查询构建的数组.根据数组中的值posuition,我需要为它分配另一个字符串.
我认为foreach循环中的if语句将是前进的方向,但我遇到了一些麻烦.
以下是我的代码......
$test = array(
array("test", 1),
array("test2", 2),
array("test4", 4),
array("test5", 5),
array("test3", 3),
array("test6", 6)
);
foreach($test as $t) {
if($t[1]==1){
array_push($t, "hello World");
}
}
print_r$test);
Run Code Online (Sandbox Code Playgroud)
除了array_push之外,其他所有接缝都可以工作.如果我在循环之后的print_r($ test)没有添加任何内容.
我在这里做了一些非常愚蠢的事吗?......
这是我得到的,如果我print_r($ test)
Array
(
[0] => Array
(
[0] => test
[1] => 1
)
[1] => Array
(
[0] => test2
[1] => 2
)
[2] => Array
(
[0] => test4
[1] => 4
)
[3] => Array
(
[0] => test5
[1] => 5 …Run Code Online (Sandbox Code Playgroud) 我试图循环一个json对象,并使用键将选定的值推送到数组.
$json = ' { "from": 1, "to": 2, "total": 2, "currentPage": 1, "totalPages": 1, "queryTime": "0.004", "totalTime": "0.020", "partial": false, "canonicalUrl": "/v1/products(offers.type=deal_of_the_day)?format=json&apiKey=946n9vuhdkgeyz2qx2dpxd54","products": [ { "sku": 5998225, "productId": 1219180376135, "name": "LG - 1.1 Cu. Ft. Mid-Size Microwave - Black", "source": "bestbuy", "type": "HardGood", "startDate": "2014-07-06","new": false, "active": true, "lowPriceGuarantee": true, "activeUpdateDate": "2014-11-03T19:43:46", "regularPrice": 124.99, "salePrice": 99.99, "onSale": true},{ "sku": 2634897, "productId": 1218343205770, "name": "Rocketfish In-Wall HDMI Cable", "source": "bestbuy", "type": "HardGood", "startDate": "2011-08-14", "new": false, "active": true,"lowPriceGuarantee": false, "activeUpdateDate": "2014-11-03T18:03:02", …Run Code Online (Sandbox Code Playgroud) 我不知道如何向现有数组添加键和值.我的阵列是这样的.最初我尝试添加使用,array_push()但它添加不是因为我需要它.
在给出'var_dump'后我给出了输出.
array (size=6)
0 =>
array (size=3)
'id' => int 7
'title' => string 'Pongal' (length=6)
'start' => string '2016-05-16' (length=10)
1 =>
array (size=3)
'id' => int 8
'title' => string 'big day' (length=7)
'start' => string '2016-05-04' (length=10)
2 =>
array (size=3)
'id' => int 9
'title' => string 'marriage day' (length=12)
'start' => string '2016-05-19' (length=10)
3 =>
array (size=3)
'id' => int 10
'title' => string 'Karthiks bday' (length=14)
'start' => string '2016-06-11' …Run Code Online (Sandbox Code Playgroud) 我试图更新数组中的值(如果找到),如果没有,则向其中添加一个新数组。
这是我一直在尝试的一些代码:
var cartItems = {};
var items = []
cartItems.items = items;
$('.proAdd').click(function(){
var name = $(this).attr("data-name");
var price = parseFloat($(this).attr("data-price"));
var quantity = parseInt($(this).attr("data-quantity"));
var item = {"name": name,"price": price,"quantity": quantity}
items.forEach(function(item) {
if (item.name === name) {
item.quantity = 2
return;
}else{
cartItems.items.push(item);
}
});
Run Code Online (Sandbox Code Playgroud)
在此版本中,注释被推送。如果我取出 else 分支,那么它会更新它,但也会推送它。我为此创建了一个小提琴。
也尝试过这个,但它说 x.quantity 未定义:
var index = items.findIndex(x => x.name==name)
if (index != -1){
x.quantity = 2
}
else {
cartItems.items.push(item);
}
Run Code Online (Sandbox Code Playgroud) 我试图将$ _POST值分成不同的数组来检查它的值.我试图将$ key和$值保留为关联数组.现在,只有array_push工作,但不是array_merge.Array_merge为我返回一个空数组.有人可以帮忙吗?
$reg_word = $reg_alpha = $reg_paragraph = array();
foreach ($_POST as $key=>$value){
if (in_array($key, $alpha_numbers)){
array_push($reg_word, $value);
} else
if (in_array($key, $alpha)){
array_push($reg_alpha, $value);
} else
if (in_array($key, $paragraph)){
array_push($reg_paragraph, $value);
}
}
Run Code Online (Sandbox Code Playgroud)
显示器
Array ( [0] => St. John [1] => M [2] => kjkjk )
Array ( [0] => HKG )
Array ( [0] => kjkj )
Run Code Online (Sandbox Code Playgroud)
我想要它显示Array( Location => St. John)等
$files = array();
function listFolderFiles($dir){
$ffs = scandir($dir);
echo '<ol>';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){
if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
else echo '<li>'.$ff;
array_push($files, array('file' => $dir.'/'.$ff, 'hash' => hash_file('md5', $dir.'/'.$ff)));
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles('/var/www');
var_dump($files);
Run Code Online (Sandbox Code Playgroud)
为什么我在这里得到空输出?
array(0) { };
Run Code Online (Sandbox Code Playgroud) 我对集合进行了分组,并使用 each() 函数对其进行迭代。在每个函数中,我想将元素添加到某个数组中。但它不起作用。知道吗?
$dataSet1 = [];
$appointments = [
['department' => 'finance', 'product' => 'Chair'],
['department' => 'marketing', 'product' => 'Bookcase'],
['department' => 'finance', 'product' => 'Desk'],
];
$groupData = collect($appointments)->groupBy('department');
$groupData->each(function ($item, $key) {
Log::info($key); //Show correct output in log
array_push($dataSet1, $key); //ERROR
array_push($dataSet1, 'A');//ERROR
});
Run Code Online (Sandbox Code Playgroud)
Laravel 版本:8.35.1
array-push ×10
php ×8
arrays ×5
array-merge ×1
collections ×1
each ×1
foreach ×1
forms ×1
function ×1
javascript ×1
jquery ×1
json ×1
key-value ×1
laravel ×1
output ×1