Getting array out of something in php - php

I don't even know what should I call this type of data but I need to get array out of it.
Code
$folderss = $ssh->exec($command);
$folders = explode(',', $folderss);
DD result
array:1 [▼
0 => """
DO_NOT_UPLOAD_HERE\n
domains\n
public_html\n
"""
]
What I need
array:3 [▼
0 => "DO_NOT_UPLOAD_HERE",
1 => "domains",
2 => "public_html",
]
any idea?
Update
I changed my code to:
$folders = preg_split("/[\s,]+/", $folderss);
Now I'm getting:
array:4 [▼
0 => "DO_NOT_UPLOAD_HERE"
1 => "domains"
2 => "public_html"
3 => ""
]
I have 1 extra row there, how to remove it?
Update 2
If I use
$folders = explode("\n", $folderss);
same result as using preg_split happens (extra line)
array:4 [▼
0 => "DO_NOT_UPLOAD_HERE"
1 => "domains"
2 => "public_html"
3 => ""
]

Try this to remove empty values from array
$folders = preg_split("/[\s,]+/", $folderss);
$folders=array_filter($folders);

Replace this line:
$folders = explode(',', $folderss);
by:
$folders = explode('\n', trim($folderss, '"');

You can use array_filter to remove the empty values from an array
$folders = array_filter(preg_split("/[\s,]+/", $folderss));

Related

convert array of arrays into comma separated list with unique values - PHP

I have an array that looks like below:
$var[] = ^ array:4 [▼
0 => array:1 [▼
0 => "apple"
]
1 => array:1 [▼
0 => "apple"
1 => "grape"
]
2 => array:1 [▼
0 => "orange"
]
3 => array:1 [▼
0 => "banana"
]
]
Is there a way to convert this array into comma separated list with unique values as below?
$var = "apple,grape,orange,banana"
I tried implode(', ',$var) but it's not working as expected. Any help pls?
Update: My sub-array may have more than 1 element. It's dynamic.
This produces the results given the expected input. Basically array_column get's the first item from each item of the array (0 based). Then array_unique on that will simply get the values without duplicates.
$arr = [
['apple'],
['apple'],
['banana'],
['pie'],
];
print_r(array_unique(array_column($arr, 0)));
EDIT: since there might be more than one item in the inner arrays, there's no shame in doing a non one-liner solution:
$result = [];
foreach ($arr as $row) {
foreach ($row as $item) {
if (array_search($item, $result) === false) {
$result[] = $item;
}
}
}
print_r($result);
There's also a one-liner solution:
$result= array_unique(call_user_func_array('array_merge', $arr));
cf. https://stackoverflow.com/a/14972714/13720553

Undefined index error while key exists in array

I'm pulling my hair on a proble which seems very simple, but I can't find a solution.
I have a simple $row array here :
array:2 [▼
"reference" => "ABCDEF"
"quantity" => "10"
]
I'm trying to parse it and retrieve quantities per reference using :
$line = [
'ref' => strtoupper($row["reference"]),
'quantity' => $row["quantity"]
];
I'm looping through array of lines using this code:
foreach ($rows as $row) {
$line = [
'ref' => strtoupper($row['reference']),
'quantity' => $row['quantity']
];
}
As a test, my main array $rows has 2 lines :
^ array:3 [▼
0 => array:2 [▼
0 => "ABCDEF"
1 => "10"
]
1 => array:2 [▼
0 => "WXCVBN"
1 => "3"
]
2 => array:1 [▼
0 => null
]
]
However, I'm getting the following error :
Undefined index: reference
Strangely enough, if I comment out the
'ref' => strtoupper($row["reference"]),
line, I can get the "quantity" value with no issue...
I know the key is in there, since the debug of the $row object gives the above result.
It must be dead simple... but I can't find the solution...
If anyone could please help ?
So apparently the $row variable a row from a bigger array that is used in a foreach loop.
This might be the solution to your problem.
$array = [
[ "reference" => "ABC", "quantity" => "10"],
[ "reference" => "ABC", "quantity" => "10"],
[ "reference" => "ABC", "quantity" => "10"],
];
$line[] = '';
foreach($array as $row)
{
$line['ref'] = $row['reference'];
$line['quantity'] = $row['quantity'];
}
In this example $array is your bigger array, i use it to test the example.
After that i create a empty array $line to append the "new" data.
Could you try this?
Edit:
After taking a look at your loop and the array i noticed that your array doesnt have a reference key. Can you try strtoupper(row[0])?
Viewing your code seems that you convert $row array to $line without rewrite key in new array.
your code
foreach ($rows as $row) {
$line = [
'ref' => strtoupper($row['reference']),
'quantity' => $row['quantity']
];
}
With your rewrite you can access $line data by index, not by key
array:3 [▼
0 => array:2 [▼
0 => "ABCDEF"
1 => "10"
]
...
]
My solution
If you want to access your $line data by key, you need to rewrite your loop as:
foreach($rows as $rowData) {
foreach($rowData as $rowKey => $rowValue) {
$data = [
'ref' => $rowKey => strtoupper$($rowValue),
'quantity' => $row['quantity']
];
}
$line[] = $data;
}

Unable to add 2 associative arrays

I am trying to add 2 associative arrays, but the new array only comes with an index for the second array.
This is what I am doing
$array = ['name' => 'address', 'value' => 'us'];
$arr = ['name' => 'joe', 'value' => 'doe'];
$arr[] = $array;
This is the result
array:3 [▼
"name" => "joe"
"value" => "doe"
0 => array:2 [▶]
]
I am expecting something like this
array:2 [▼
0 => array:2 [▶]
1 => array:2 [▶]
]
As you can see, there is no index for the first array, thus making the count 3 instead of 2. Please how do I fix this?
Just create another array and add the 2 arrays to it:
$newAr = [];
$newAr[] = $array;
$newAr[] = $arr;
var_dump($newAr);

array group by and count php [duplicate]

This question already has answers here:
How to count array with group by
(2 answers)
Closed 4 months ago.
I have 2 log files which i read and get the contents as array.
The array which i get is like this:
2 => array:9 [▼
"app" => "a.log"
"context" => "local"
"level" => "error"
"level_class" => "danger"
"level_img" => "exclamation-triangle"
"date" => "2018-10-25 21:01:04"
"text" => "Class 'Arcanedev\Support\Collection' not found
]
3 => array:9 [▼
"app" => "b.log"
"context" => "local"
"level" => "error"
"level_class" => "danger"
"level_img" => "exclamation-triangle"
"date" => "2018-10-26 16:49:07"
I need to get the count of all the error levels for each file.
For example:
a.log => "count of error level for a.log",
b.log => "count of error levels for b.log"
I have tried:
foreach($names as $filename){
$logs = $this->getLogs($this->filepath.$filename);
$result = array();
$result[ $filename ] = $logs;
foreach($logs as $log){
//$result[ $log['level'] ][] = $log;
$result[ $filename ][] = $log;
$counts = array_map('count', $result);
}
}
which gives me this result:
array:1 [▼
"a.log" => 20
]
Any help is appreciated.
Thank you in advance. :)
i solved the issue myself. I was tried another approach and it seemed to work.
Here is the solution:
foreach($names as $filename){
$logs = $this->getLogs($this->filepath.$filename);
$result = array();
foreach($logs as $log){
$result[ $log['app'] ][] = $log;
$counts = array_map('count', $result);
}
}
and the result:
array:2 [▼
"b-laravel.log" => 3
"laravel.log" => 7
]
Thank You.
Something like
print_r(array_count_values(array_column($array, 'app')));
I would test it but you didn't provide an array i can use. In theory it should work.

Misunderstanding of array_values

I've a problem to unerstand correctly array_values, when I do:
$array[] = 'data1'; // I want [0 => 'data1']
unset($array[0]); // I want []
$array = array_values($array); // I want [] but keys resetted
$array[] = 'data2'; // I want [0 => 'data2']
$array[] = 'data3'; // I want [0 => 'data2', 1 => 'data3']
dump($array);
I've the result:
array:2 [▼
1 => "data2"
2 => "data3"
]
But I'll want:
array:2 [▼
0 => "data2"
1 => "data3"
]
Maybe someone can explain it to me ? Because I'm a little lost :-/
For exemple, if I've an array with 10 values in, remove the 3rd value, and do an array_values on, it works well.
But if I remove the last value from an array, then when I do a array_value, the next value I add, always have id 1 and not 0.
This behaviour has been already reported as a bug: https://bugs.php.net/bug.php?id=75433 and (apparently as the result of this post) also: https://bugs.php.net/bug.php?id=75653

Categories