Maximum of items a "dump()" on an array will show? - php

When I dump() a large array after a certain amount of items in the array it will only display something like this: 108 => {#249 …6}. Where ...6 is the number of items in the (sub)array.
As in the code provided you can see that the switching point of this particular array is at key 107. All keys before show all the data and all keys after only show something similar to "{#249 …6}".
Is there a way so display all values of a large array?
106 => {#251 ▼
+"average": 11097598.72
+"date": "2018-06-16"
+"highest": 13999997.93
+"lowest": 6743999.48
+"order_count": 5
+"volume": 5
}
107 => {#250 ▼
+"average": 15222222.0
…5
}
108 => {#249 …6}
109 => {#248 …6}
110 => {#247 …6}

Related

Laravel whereNotIn plus whereIn doesn't equal total count

How is it possible that whereNotIn() plus whereIn() doesn't equal total count?
Running this:
$updatedBreeds = [
86,
113,
// etc ....
];
DB::enableQueryLog();
dump(Breed::count());
dump(Breed::whereIn('id', $updatedBreeds)->count());
dump(Breed::whereNotIn('id', $updatedBreeds)->count());
dd(DB::getQueryLog());
Returns this:
159
39
0
Am I missing something here? The whereNotIn() call should return 120 results.
Apparently, one of the values in the array was null. Which strangely enough led to this behavior.
Here is a dump on $updatedBreeds:
[
0 => 86
1 => 113
- 2 => null // When I removed this value, the whereNotIn() worked
2 => 44
3 => 8
4 => 54
5 => 54
// ...
]

Calculate Age for each user from array of date using Laravel

i'm new to laravel i want to know if there are methode to calculate age from array so my content looks like this:
Collection {#231 ▼
#items: array:6 [▼
0 => "1928-11-18"
1 => "1938-06-15"
2 => "1939-03-30"
3 => "1941-11-08"
4 => "1940-04-29"
5 => "1987-06-24"
]
}
How to properly transform the array so that it contains only age like this
Collection {#231 ▼
#items: array:6 [▼
0 => 90
1 => 80
2 => 79
3 => 77
4 => 87
5 => 31
]
}
You can also use map method on collection
$collection = collect([0 => "1928-11-18",
1 => "1938-06-15",
2 => "1939-03-30",
3 => "1941-11-08",
4 => "1940-04-29",
5 => "1987-06-24"]);
$age = $collection->map(function ($item, $key) {
return Carbon::parse($item)->diff(Carbon::now())->format('%y');
});
return $age->all();
This will give you,
[
"89",
"79",
"79",
"76",
"78",
"30"
]
You can iterate trough a Collection with foreach() then convert to Age using Carbon ->age:
foreach ($collection as $key => $value) {
$collection[$key] = Carbon::parse($collection[$key])->age;
}
This will give you a Collection as what you do is editing the Collection, instead of creating a new array.
Use Carbon's ->age or diffInYears and collection transform (or map):
$dates->transform(function ($date) {
return \Carbon\Carbon::parse($date)->age;
});
Note: someone born on 1928-11-18 is 89 years old, not 90.

How to take all the words in one normal array from the database in the Laravel framework with a minimum waste of time?

I want to take all the words from the database into default array. I have more than 50,000 words in the database, and most likely this number is up to a million. Therefore, I want this operation did not take much time. I tried such ways in which not a word is put into the usual array. That is, the words are passed to the associative array:
$words = DB::table('words')->pluck('word');
dump($words);
Result:
Collection {#197 ▼
#items: array:12 [▼
0 => "тоҷик"
1 => "ӯзбек"
2 => "қирғиз"
3 => "эрон"
4 => "япон"
5 => "англис"
6 => "тоҷик"
7 => "ӯзбек"
8 => "қирғиз"
9 => "эрон"
10 => "япон"
11 => "англис"
]
}
Second method:
$words = DB::select("SELECT `word` FROM `words`");
dump($words);
Result:
array:12 [▼
0 => {#210 ▼
+"word": "тоҷик"
}
1 => {#207 ▼
+"word": "ӯзбек"
}
2 => {#209 ▼
+"word": "қирғиз"
}
3 => {#206 ▼
+"word": "эрон"
}
4 => {#208 ▼
+"word": "япон"
}
5 => {#205 ▼
+"word": "англис"
}
6 => {#204 ▼
+"word": "тоҷик"
}
7 => {#203 ▼
+"word": "ӯзбек"
}
8 => {#202 ▼
+"word": "қирғиз"
}
9 => {#200 ▼
+"word": "эрон"
}
10 => {#213 ▼
+"word": "япон"
}
11 => {#214 ▼
+"word": "англис"
}
]
And I want to take all the words in this form on the usual array:
array:12 [▼
0 => "тоҷик"
1 => "ӯзбек"
2 => "қирғиз"
3 => "эрон"
4 => "япон"
5 => "англис"
6 => "тоҷик"
7 => "ӯзбек"
8 => "қирғиз"
9 => "эрон"
10 => "япон"
11 => "англис"
]
I need this to get the difference between the two arrays. The input array is an upbeat array so I need to take all the words from the bass to add to the copious array. In general, is there any function like array_diff() in the Laravel framework itself?
$diff = array_diff($input_array, $words_from_db);
And add the difference to the database:
foreach ($diff as $value) {
DB::table('words')->insert(['word'=>$value]);
}
How correct is my method of adding a large number of words (data) to the database? Can I need to optimize if of course it's possible?
In the general i found this function from Laravel:
public function diff($items)
{
return new static(array_diff($this->items, $this->getArrayableItems($items)));
}
But how I can use it for get diff 2 arrays? Input array will be default array and second array words from db
Thank you all in advance for answering and forgive me for apologizing for grammatical errors in the question.
You can accomplish inserting unique words into the database entirely inside of MySQL, this should always be faster than pulling all the words into memory, diffing them and sending in the unique values.
To do this you need to add a unique index on your words column. (ref: https://dev.mysql.com/doc/refman/5.7/en/create-index.html)
CREATE UNIQUE INDEX `idx_word` ON `db`.`words` (word);
Then on your insert you need to add ON DUPLICATE KEY (Laravel query builder does not support on duplicate key - can use raw to insert) ref: https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html, https://laravel.com/docs/5.5/queries#raw-expressions
INSERT INTO word VALUES ('foo'),('bar') ON DUPLICATE KEY word = VALUES(word);
I would use on duplicate key rather than insert ignore. I think insert ignore may hide other potential issues unrelated to duplicate key.
You can get diff two array in Laravel 5.5 so:
$words = DB::table('words')->pluck('word')->toArray(); // Collection converted to simple array
And you can use default function in php for get diff arrays:
$result = array_diff($input_default_array, $words);

create unique ids from multidimensional array

I have an array with small example below
Array (2)
system => Array (1)
system_id => 3
proc => Array (4)
proc_id => 5
proc_or => 1
proc_owner => 7751
results => Array (7)
0 => Array (5)
process_id => 101
process => 1335
process_name => xa
process_owner => xo
rating => 67.554
1 => Array (6)
process_id => 122
process => 1335
process_name => xa
process_owner => xo
rating => 33.554
proc_rel => xf
2 => Array (5)
process_id => 101
process => 1227
process_name => xd
process_owner => xa
rating => 123.78
3 => Array (8)
process_id => 101
process => 1291
process_name => xa
process_owner => xo
rating => 64.241
proc_rel => xf
proc_rel_id => 1474
proc_rel_owner => xm
I need to be able to organise this so that it contains only unique process_id values, so results 2 and 3 would be removed and the rating for that remaining unique id is the sum of all ratings for the records with that id so the results 0 rating id would become the sum of results 0 2 and 3.
There are thousands of records so its not practyical to do it without some sort of automated loop
I was thinking of maybe creating a new array and as its being processed
If the process_id is not in new array add it and all related data
If the process id is already in the array just add(+) the value of rating to the existing value.
I have tried to adapt a couple of loops that ive found but they dont seem to work properly.
Not sure if this is the way to go and Im not sure how to do it anyway so any suggestions greatly appreciated.
If the data set is big, then better try to adjust your database query or change file structure when its generated. If it isnt possible then the only rational way is to create new array:
$newArray = ();
foreach($array['proc']['results'] as $result) {
if (isset($newArray[$result['process_id']]) {
$newArray[$result['process_id']]['rating'] += $result['rating'];
} else {
$newArray[$result['process_id']] = $result;
}
}

Filter two multidimensional arrays based on key matches, but each array has different structure - Output to new merged array

I am trying to extract only array items that match on specific fields and then I need to merge the two into one array only containing the data I need.
I have two arrays, and their structure is like below. Only product_count s where CATEGORY_ID of Array #1 match the Key of Array #2 should be the output.
Array #1: - Many of Array #1 CATEGORY_IDs will not exist in Array #2 key field.
0 => Array (4)
0 => 3
CATEGORY_ID => 10
1 => 1
product_count => 8
1 => Array (4)
0 => 4
CATEGORY_ID => 111
1 => 6
product_count => 109
...
Array #2:
10 => Category Name 1
110 => Category Name 2
8 => Category Name 3
109 => Category Name 4
111 => Category Name 5
3 => Category Name 6
132 => Category Name 7
...
Final Output should look something like: and I might be going about this all wrong, so I am open to any suggestions..
10 => [0] => Category Name 1
[1] => 8 // product_count
111 => [0] => Category Name 5
[1] => 109 // product_count
...
I am running a foreach() to extract product counts per category. As you can see my two arrays reflect this by the data.
You'd have to do a foreach loop, I believe:
$new_array = array();
foreach($array1 as $part) {
$new_array[$part["CATEGORY_ID"]] = array( $array2[$part["CATEGORY_ID"]], $part["product_count"] );
}
I think I got the gist of what you want. I don't think there is an actual PHP function to do what you want, either.

Categories