How can I turn an array to csv file in php - php

One more beginner question:
how can I turn an array (actually it's just a text) like that:
Array
(
[0] => Array
(
[0] => nsor#cg.ukrtel.net
[1] => p2007#rambler.ru
[2] => pan20072009#yandex.ru
[3] => gf#ukr.net
[4] => tmkp#ma.odessa.ua
[5] => export#soh.by
[6] => advert#soh.by
)
)
into a .csv file by means of PHP? This file should be without things like this "[0] =>", only addresses.

Assuming your array is in a variable called $arr, you could use
echo implode("\r\n",$arr);
And you might want to prefix it with
header('Content-type: text-csv; charset="UTF-8"');
or similar.

Look at the first example at http://php.net/manual/en/function.fputcsv.php
Use your array instead of the array in the example.

Related

Remove related element from multidimensional array in php

I have an array of image data like this:
[other-image] => Array
(
[img] => Array
(
[0] => 1526973657.jpg
[1] => 1526973661.jpg
[2] => 1526973665.jpg
)
[path] => Array
(
[0] => ../post-upload/1/
[1] => ../post-upload/1/
[2] => ../post-upload/1/
)
[type] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[thumb] => Array
(
[0] => thumb_1526973661.jpg
[1] => thumb_1526973665.jpg
[2] => thumb_1526973668.jpg
)
)
Now I want to delete an image and it's all related data from sub arrays. (path, type, thumb data)
This is how I tried it in php:
$delkey = '1526973657.jpg';
if(in_array($delkey, $_SESSION['other-image']['img'])){
$imgkey = array_search($delkey, $_SESSION['other-image']['img']);
if($imgkey) unset($_SESSION['other-image']['img'][$imgkey]);
}
But problem is I can't delete related data from other arrays.
Can anybody tell me how to do this?
Thank you.
You should use !==false after array_search() because it may return first index i.e. 0 in some cases, so your condition will not executed. And regarding delete related data from other arrays, you have to unset other data related to that key.
if($imgkey!==false){
unset($_SESSION['other-image']['img'][$imgkey]);
unset($_SESSION['other-image']['path'][$imgkey]);
unset($_SESSION['other-image']['type'][$imgkey]);
unset($_SESSION['other-image']['thumb'][$imgkey]);
}
Is the related data has same key with img?
If they are same, I think you only need to add some codes to delete other data like the way was used to delete img.
if($imgkey) unset($_SESSION['other-image']['path'][$imgkey]);
if($imgkey) unset($_SESSION['other-image']['type'][$imgkey]);
if($imgkey) unset($_SESSION['other-image']['thumb'][$imgkey]);
If the keys in img sub-array are related with the same key(index) in sub-arrays(path, type and thumb, you can also unset those keys. e.g.
$delkey = '1526973657.jpg';
if(in_array($delkey, $_SESSION['other-image']['img'])){
$imgkey = array_search($delkey, $_SESSION['other-image']['img']);
if($imgkey){
unset($_SESSION['other-image']['img'][$imgkey]);
unset($_SESSION['other-image']['path'][$imgkey]);
unset($_SESSION['other-image']['type'][$imgkey]);
unset($_SESSION['other-image']['thumb'][$imgkey]);
}
}

How to print arrays inside array by Laravel blade?

My function returns an array to a variable in laravel blade page like this :
Array
(
[id] => 1
[name] => Enterpise
[children] => Array
(
[0] => Array
(
[id] => 2
[name] => name1
[children] => Array
(
[0] => Array
(
[id] => 5
[name] => name2
[children] =>
)
[1] => Array
(
[id] => 6
[name] => name3
[children] => Array
(
[0] => Array
(
[id] => 11
[name] => name4
[children] =>
)
.....
Now I want to print these values inside my page by using something like foreach. Consider that their children's numbers are not specified, it is impossible to do that inside of my function. It is important to me to be done in blade. How can I do that?
In your blade files you can use any php functions like print_r().
Example:
{{ print_r($array) }}
As this is very old issue but I found json directive very useful.
<pre>
<code>
#json($array);
</code>
</pre>
You can find the way for your problem over here:
https://laracasts.com/discuss/channels/general-discussion/passing-arrays-to-blade-and-iterating
Or you can use:
{{ echo json_encode($yourarray) }}
but this will print your array as json encoded string.
You can use PHP directive
#php
echo "<pre>";
print_r($array);
echo "</pre>";
#endphp
I always use this clean and simple solution that outputs your array in a nicely formatted way, using JSON:
<pre><code>{{ json_encode($array, JSON_PRETTY_PRINT) }}</code></pre>
You can drop the JSON_PRETTY_PRINT if you want it displayed on a single line:
<pre><code>{{ json_encode($array) }}</code></pre>
the best output and less code has print_r() :
<pre>
{{print_r($array)}}
</pre>

Searching an array for a string pattern and store it in a variable

Array
(
[0] => LAMPION
[1] => BANBU
[2] => DT-T300-FNS
[3] => T65
[4] => DT-299-FNS
[5] => T30
)
I have an array looking like this. The problem is the data stored in the array is not consistent so i have to search the array for this pattern "xx-xxx-xxx" and store it in a variable. is there any way i can do that? really need hlp
Yes.
$matches = preg_grep('/^.{2}-.{3}-.{3}\z/', $array);
If you want the first, just add [0] (you'll need a temporary variable first for < PHP 5.4).

sort files in a directory based on partial file name in php

I am using glob() php function to find the files in a directory.
I am getting the following array as result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[6] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-5.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
)
What I want is "To sort the files on the basis of partial text of file names". As in the above array, when you may realize I was supposed to expect the result to be sorted as the ...-0.jpeg, ...-1.jpeg, ...-2.jpeg not ...-0.jpeg, ...-1.jpeg, ...-10.jpeg.
I am expecting the following result:
Array
(
[0] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-0.jpeg
[1] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-1.jpeg
[2] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-2.jpeg
[3] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-3.jpeg
[4] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-4.jpeg
[5] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-6.jpeg
[7] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-7.jpeg
[8] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-8.jpeg
[9] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-9.jpeg
[10] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-10.jpeg
[11] => D:\xampp\htdocs\myproject\app\webroot\batch\temp\file_2012-08-09-11.jpeg
)
Kindly help, how I can do it using PHP with/without regular expression?
Use natsort function that sort an array using a "natural order" algorithm
As you have all strings in an array, simply use php's built-in function sort().
It would work like this:
sort($array);
Since all files are in the same directory, this should solve the incorrect order you have. If it doesn't, you can also try this:
//Get rid of directory
foreach($array as $key=>$value){
$value=str_replace('D:\xampp\htdocs\myproject\app\webroot\batch\temp\\','',$value); //Watch out, the last backslash has to be escaped with another backslash, otherwise the string will not end
}
//Sort only the filenames
$sort($array);
//Prepend directory again
foreach($array as $key=>$value){
$value='D:\xampp\htdocs\myproject\app\webroot\batch\temp\\'.$value;
}

Can I turn an array in memory into the code representation of that array?

I'm not even entirely sure how to ask the question, but assuming I have an array in memory like this:
Array (
[0] => Array
(
[0] => Array
(
[0] => 18451
[1] => MDX
)
[1] => Array
(
[0] => 18450
[1] => NSC
)
[2] => Array
(
[0] => 18446
[1] => RL
)
)
)
Is there some existing functionality to turn it into the code version of that array? I have a # of arrays I need to do this for, nested to various degrees. So I imagine I want output something like
$arrayname[] = array(array('18451','MDX'),array('18450','NSC'),array('18446','RL'));
I can write something to do it, but I'd rather not recreate the wheel if there's an existing way to do it.
This may be all you need:
http://us.php.net/var_export
manually loop through the array recursively and create a string like how you want.
var_export could help you there i think. The other option would be looping through it manually :(

Categories