Laravel make array of arrays - arrays

How get an array of many array ? foreach line 0-1-2-3(4-5 same object actually), actually objects.
How make it for get:
[ 1[] 2[] 3[4[]5[]] ]
Php code (laravel):
$array = array();
foreach($users as $u)
{
$x = User::where('xxx', 'xxx')->toArray();
array_push($array, $x);
}
return $array;

Related

Convert Laravel array collection to array

I was updating may images array which is found in following format:
[
"d756d3d2b9dac72449a6a6926534558a.jpg",
"b607aa5b2fd58dd860bfb55619389982.jpg",
"f937c8fddbe66ab03c563f16d5cfa50c.jpg"
]
So to delete a single image d756d3d2b9dac72449a6a6926534558a.jpg from this array column I used the filter method like below:
$collection = collect($imageColumn)->filter(
function ($valueTobeDeleted) use ($array) {
return !in_array($valueTobeDeleted, $array);
}
);
so after filter I wan to update my database with the new collection, but the new collection is in a map format like below
array:2 [
1 => "b607aa5b2fd58dd860bfb55619389982.jpg"
2 => "f937c8fddbe66ab03c563f16d5cfa50c.jpg"
]
the problem is i don't need those keys 1, and 2 my expectation was
array:2 [
"b607aa5b2fd58dd860bfb55619389982.jpg"
"f937c8fddbe66ab03c563f16d5cfa50c.jpg"
]
How can I achieve my expectation? How can I convert them to array?
Because you are using a Collection, you can do ->values() at the end so you convert all keys into integer keys and beginning from 0, like a normal non-associative array:
$collection = collect($imageColumn)->filter(
function ($valueTobeDeleted) use ($array) {
return !in_array($valueTobeDeleted, $array);
}
)
->values();
In order to re-index an array, you can use the array_values PHP function:
$arr = array_values($arr);
This will fix the keys.
Use array_values to get a an array of that associatve array.
$collection = collect($imageColumn)->
$filtered = filter(function ($valueTobeDeleted) use ($array) {
return !in_array($valueTobeDeleted, $array);
});
$filtered = array_values($filtered);

PERL - Remove certain data form array after a symbol

my code looks like this
foreach ($id->{deleteids}) {
my #separated = split('_', $_);
push #rids, $separated[0];
}
on using Data::Dumper on $id->{deleteids} i get this
$VAR1 = [
'43-173739_cdfvgbvvd',
'43-173738_sddsvfdvfd',
'43-173737_sfvdfvdfvdf',
'43-173736_svdvdfvdfvdfvfdvfd'
];
My expected output of #rids that i want
$VAR1 = [
'43-173739',
'43-173738',
'43-173737',
'43-173736'
];
but on using Data::Dumper on #rids i always get
$VAR1 = 'ARRAY(0x3210010)';
Array references have different way to be called with, And as Zdim pointed you need to use a reference \#rids to dump array using Dumper
use strict;
use warnings;
use Data::Dumper;
my $id = { # creating a similar array like yours in a hash ref
deleteids => [
'43-173739_cdfvgbvvd',
'43-173738_sddsvfdvfd',
'43-173737_sfvdfvdfvdf',
'43-173736_svdvdfvdfvdfvfdvfd'
]
};
print Dumper($id->{deleteids});
my #rids;
foreach (#{$id->{deleteids}}) { # correct way to use array ref
my #separated = split('_', $_);
push #rids, $separated[0];
}
print Dumper(\#rids); # how to dump array using Dumper
Output:
# $id hash ref
$VAR1 = [
'43-173739_cdfvgbvvd',
'43-173738_sddsvfdvfd',
'43-173737_sfvdfvdfvdf',
'43-173736_svdvdfvdfvdfvfdvfd'
];
# #rids
$VAR1 = [
'43-173739',
'43-173738',
'43-173737',
'43-173736'
];
Following piece of code should do what you expect (remove undesired part)
foreach ($id->{deleteids}) {
s/_.*//;
push #rids, $_;
}

PHP - Search in specific array value

I want to Search in Array value by in_array Function and For loop. My code:
$input = "a";
$arrays = array("cdf","abs","tgf");
$counter = count($arrays);
for ($i=0; $i<$counter; $i++){
if(in_array($input,$arrays) !== true){
echo "Found <br>";
} else {
echo "Not Found";
}
}
Output:
Not Found
Found
Not Found
But, if(in_array($input,$arrays[$i]) !== true) not working.
The reason in_array("a", "cdf"), which is what in_array($input, $arrays[$i]) could become, isn't working is because "cdf" isn't an array.
Are you trying to find array elements in $arrays that contain the letter a?
In that case you should search array elements with strpos() to determine if a string contains another string. You can also use foreach instead of for if iterating over the array is all you want to do.
$input = "a";
$arrays = array("cdf","abs","tgf");
foreach ($arrays as $key => $value)
{
if (strpos($value, $input) !== false)
echo "Found in $key<br>";
else
echo "Not Found<br>";
}

How to Group array then iterate

I have trouble with my project.
First, I have a data like this : https://jsoneditoronline.org/?id=c6d15407962e4d1b986435ad3c283b4e
Then I group the data using this:
private function _group_by($array, $key) {
$new = [];
foreach ($array as $value) {
$new[$value[$key]][] = $value;
}
return $new;
}
$key = 'water_id'
After that the following result is like this: https://jsoneditoronline.org/?id=72991d45938c49a38900703feb3a60e7
From result above I cant iterate the data (I want to iterate since beginning). So, I want to able iterate the data, it is something wrong with my group by array function ? If you understand what I want, please help.
You can use collections to manipulate arrays or Traversable objects.
Grouping in CakePHP's Collection
use Cake\Collection\Collection;
private function _group_by($array, $key) {
$collection = new Collection($array);
return $collection->groupBy('water_id')->toArray();
}
You tagged this as being a Laravel App, right? Check out Collections: https://laravel.com/docs/5.6/collections.
$result = null;
collect($array)->groupBy($keyName)->each(function ($group, $key) use (&$result) {
foreach ($group as $element => $index) {
//Do something to $result.
}
});
Depending on your circumstances, map or transform might be more appropriate than each.

perl hash of arrays

I am trying to access elements of an array which is part of a hash.
for my $idx ( 0 .. $#vss ) {
push (#{$vsnhash->{$vss[$idx]}}, $vsports[$idx]);
}
print Dumper(\%$vsnhash);
($VAR1 = {
'name2' => [
'8001',
'8002'
],
'name1' => [
'8000'
]
};
I an able to access the keys with a foreach loop:
foreach my $key ( keys %$vsnhash ) {
print "$key\n";
}
How do I access the array of port numbers ('8001' , '8002') within the hash?
Thank you for the help!
while (my ($k, $v) = each %$vsnhash) {
print "$k: #$v\n";
}
foreach my $key ( keys %$vsnhash ) {
print "$key\n";
foreach my $port (#{$vsnhash->{key}}){
print "Port $port\n";
}
}
$vsnhash{name2}->[0]; #8001
$vsnhash{name2}->[1]; #8002
$vsnhash{name1}->[0]; #8000
Code wise:
foreach my $key (sort keys %vsnhash) {
foreach my $index (0..$#{$key}) {
print "\$vsnhash{$key}->[$index] = " . $vsnhash{$key}->[$index] . "\n";
}
}
The $#{$key} means the last entry in the array #{$key}. Remember that $key is a reference to an array while #{$key} is the array itself.

Resources