在Laravel中搜索数组

时间:2018-01-24 21:46:05

标签: php laravel-5

我得到了两个数组并将它们合并,然后在name下查找某个值,但似乎无法使其正常工作

$temp = [
    ['id' => 3, 'name' => 'Taylor'],
    ['id' => 3, 'name' => 'Abigail'],
];

 $temp1 = [
     ['id' => 3, 'name' => 'Taylor'],
     ['id' => 3, 'name' => 'Taylor'],
];

$ggg = array_merge($temp,$temp1);

4 个答案:

答案 0 :(得分:0)

使用print_r($ ggg)打印生成的数组。参考名称部分,您应该使用$ ggg [0] [' name']。不确定合并2个数组的最终目标是什么,你想要一个包含所有4个元素的数组吗?你这样做的方式最终只有2个元素,$ temp1的索引0将覆盖$ temp的索引0,与索引1相同。

答案 1 :(得分:0)

我试过了,也许这会对你有帮助。

@echo off
setlocal enabledelayedexpansion
set "source=G:\BLM1\BLMW-0001"
set "destination=D:\BLM"
set /a count=0
for /F "tokens=*" %%A IN ('dir /S /B /A-D %source%\*.tiff') do for /F "tokens=*" %%B in ('dir /B "%%A"') do if exist "%destination%\%%B" (
    set /a count=!count!+1
    copy "%%A" "%destination%\%%~nB_!count!%%~xB"
  ) else copy "%%A" "%destination%\%%B"
endlocal

答案 2 :(得分:0)

我想你想要下面的东西。既然你把它标记为Laravel我已经使用laravel特定代码完成了它,但也可以使用本机PHP。

$temp = [
    ['id' => 3, 'name' => 'Taylor'],
    ['id' => 3, 'name' => 'Abigail'],
];

$temp1 = [
    ['id' => 3, 'name' => 'Taylor'],
    ['id' => 3, 'name' => 'Taylor'],
];

$item = collect($temp)->merge($temp1)->first(function ($item) {
    return $item['name'] == 'Taylor';
});

答案 3 :(得分:0)

使用集合的contains()方法。架构:

collect($array1)               // Create a collection from the first array
    ->merge($array2)           // merge it with the second array
    ->contains($key, $value);  // returns boolean

您的代码:

$temp = [
    ['id' => 3, 'name' => 'Taylor'],
    ['id' => 3, 'name' => 'Abigail'],
];

$temp1 = [
     ['id' => 3, 'name' => 'Taylor'],
     ['id' => 3, 'name' => 'Taylor'],
];

collect($temp)->merge($temp1)->contains('name', 'Taylor');  // true
collect($temp)->merge($temp1)->contains('name', 'Jane');  // false

如果您想获得符合条件的项目,请使用where()

$result = collect($temp)->merge($temp1)->where('name', 'Taylor');

这将返回:

Collection {#465 ▼
  #items: array:3 [▼
    0 => array:2 [▼
      "id" => 3
      "name" => "Taylor"
    ]
    2 => array:2 [▼
      "id" => 3
      "name" => "Taylor"
    ]
    3 => array:2 [▼
      "id" => 3
      "name" => "Taylor"
    ]
  ]
}
相关问题