I am on creating a website, I need to process CSV files that my clients will bring from outside.
I have a question about my tables when processing CSV.
I wish I could treat the array of this mantle:
$Data[1];
$Data[2];
$Data[3];
Today the array comes out of this mangle:
$Data[line1];
But line 1 can vary and that's its the problem, I do not know how to display it dimensionally.
import.php
$result = $this->csvreader->parse_file('assets/csv/test.csv');
$data['csvData'] = $result;
$this->load->view('user/import',$data);
result.php
foreach($csvData as $field)
{
echo $field['unknowLine'];
}
I do not know if I am clear, but if you have a solution for me thanks in advance
using array_values()
array_values — Return all the values of an array
example:
$array = array('line1' => 'line', 'line2' => 'line2');
$data = array_values($array);
print_r($data);
// Output
// Array ( [0] => line [1] => line2 )
echo $data[0]; // Output : line
You can make a loop like this, this is pretty straightforward;
foreach($array as $key => $value) { echo $array[$key]; }
Hope this helps!
Regards,
Related
I have a php variable that contain value of textarea as below.
Name:Jay
Email:jayviru#demo.com
Contact:9876541230
Now I want this lines to in array as below.
Array
(
[Name] =>Jay
[Email] =>jayviru#demo.com
[Contact] =>9876541230
)
I tried below,but won't worked:-
$test=explode("<br />", $text);
print_r($test);
you can try this code using php built in PHP_EOL but there is little problem about array index so i am fixed it
<?php
$text = 'Name:Jay
Email:jayviru#demo.com
Contact:9876541230';
$array_data = explode(PHP_EOL, $text);
$final_data = array();
foreach ($array_data as $data){
$format_data = explode(':',$data);
$final_data[trim($format_data[0])] = trim($format_data[1]);
}
echo "<pre>";
print_r($final_data);
and output is :
Array
(
[Name] => Jay
[Email] => jayviru#demo.com
[Contact] => 9876541230
)
Easiest way to do :-
$textarea_array = array_map('trim',explode("\n", $textarea_value)); // to remove extra spaces from each value of array
print_r($textarea_array);
$final_array = array();
foreach($textarea_array as $textarea_arr){
$exploded_array = explode(':',$textarea_arr);
$final_array[trim($exploded_array[0])] = trim($exploded_array[1]);
}
print_r($final_array);
Output:- https://eval.in/846556
This also works for me.
$convert_to_array = explode('<br/>', $my_string);
for($i=0; $i < count($convert_to_array ); $i++)
{
$key_value = explode(':', $convert_to_array [$i]);
$end_array[$key_value [0]] = $key_value [1];
}
print_r($end_array); ?>
I think you need create 3 input:text, and parse them, because if you write down this value in text area can make a mistake, when write key. Otherwise split a string into an array, and after create new array where key will be odd value and the values will be even values old array
Thanks for helping as always.
My code generates arrays in the following format
${'readyformerge' . $b} = $temparraytwo;
Which results in the array names of
$readyformerge1
$readyformerge2
$readyformerge3
etc...
Which works well and we know the value that $b holds is the amount of arrays we need to merge. However I can't quite see how to do this when we won't know prior to running the script how many arrays will be created.
Fundamentally I would like to use the following to grab the result but as you see I can only do this for the amount of results I THINk it will return NOT the actual number of results. Any help?
$outputmultidimensionalevent = array_merge_recursive(${'readyformerge' . $b},${'readyformerge' . $b});
print_r($outputmultidimensionalevent); echo '<br/>';
Your problem is the result of bad design.
Instead of:
${'readyformerge' . $b} = $temparraytwo;
You should do something like this:
$readyformerge[$b] = $temparraytwo;
And then:
$merged = array();
foreach ($readyformerge as $one) {
$merged = array_merge($merged, $one);
}
Thanks, That's pushed me in the right direction.
So currently it's now setup like this:
$z=1;
$readyformergemulti = array();
while ($z <= $i){
array_push($readyformergemulti,${'readyformerge' . $z});
$z++;
}
foreach ($readyformergemulti as $one) {
print_r($one);
echo '<br/>';
$merged = array_merge_recursive($merged, $one);
}
print_r($readyformergemulti); echo '<br/>';
print_r($merged); echo '<br/>';
But unfortunately $merged returns nothing. If you look at the following the first 4 lines are the $readyformerge arrays and the 5th line is the desired result:
Array ( [house] => 2797 )
Array ( [house] => 2829 )
Array ( [house] => 2736 )
Array ( [electronica] => 2763 [house] => 2763 )
Array ( [electronica] => Array (2763) [house] => Array (2763,2797,2892,2736 ) )
Sorry to be a pain, and I KNOW everyone needs to see more code, but with thousands of lines it gets hard to display!
If you can help again that would be great!
Excuse me if this question was already solved. I've searched trough the site and couldn't find an answer.
I'm trying to build a bi-dimensional array from a string. The string has this structure:
$workers="name1:age1/name2:age2/name3:age3"
The idea is to explode the array into "persons" using "/" as separator, and then using ":" to explode each "person" into an array that would contain "name" and "age".
I know the basics about the explode function:
$array=explode("separator","$string");
But I have no idea how to face this to make it bidimensional. Any help would be appreciated.
Something like the following should work. The goal is to first split the data into smaller chunks, and then step through each chunk and further subdivide it as needed.
$row = 0;
foreach (explode("/", $workers) as $substring) {
$col = 0;
foreach (explode(":", $substring) as $value) {
$array[$row][$col] = $value;
$col++;
}
$row++;
}
$array = array();
$workers = explode('/', "name1:age1/name2:age2/name3:age3");
foreach ($workers as $worker) {
$worker = explode(':', $worker);
$array[$worker[0]] = $worker[1];
}
Try this code:
<?php
$new_arr=array();
$workers="name1:age1/name2:age2/name3:age3";
$arr=explode('/',$workers);
foreach($arr as $value){
$new_arr[]=explode(':',$value);
}
?>
The quick solution is
$results = [];
$data = explode("/", $workers);
foreach ($data as $row) {
$line = explode(":", $row);
$results[] = [$line[0], $line[1]];
}
You could also use array_walk with a custom function which does the second level split for you.
This is another approach, not multidimensional:
parse_str(str_replace(array(':','/'), array('=','&'), $workers), $array);
print_r($array);
Shorter in PHP >= 5.4.0:
parse_str(str_replace([':','/'], ['=','&'], $workers), $array);
print_r($array);
yet another approach, since you didn't really give an example of what you mean by "bidimensional" ...
$workers="name1:age1/name2:age2/name3:age3";
parse_str(rtrim(preg_replace('~name(\d+):([^/]+)/?~','name[$1]=$2&',$workers),'&'),$names);
output:
Array
(
[name] => Array
(
[1] => age1
[2] => age2
[3] => age3
)
)
I have 2 arrays - the first one is output first in full. The 2nd one may have some values that were already used/output with the first array. I want to "clean up" the 2nd array so that I can output its data without worrying about showing duplicates. Just to be sure I have the terminology right & don't have some sort of "array within an array", this is how I access each one:
1st Array
$firstResponse = $sth->fetchAll();
foreach ($firstResponse as $firstResponseItem) {
echo $firstResponseItem['samecolumnname']; // Don't care if it's in 2nd array
}
2nd Array
while( $secondResponseRow = $dbRequest->fetch_assoc() ){
$secondResponseArray = array($secondResponseRow);
foreach ($secondResponseArray as $secondResponseItem){
echo $secondResponseItem['samecolumnname']; //This can't match anything above
}
}
Thanks!
For example:
$response_names = array();
$firstResponse = $sth->fetchAll();
foreach ($firstResponse as $firstResponseItem)
$response_names[] = $firstResponseItem['samecolumnname'];
while( $secondResponseRow = $dbRequest->fetch_assoc() ){
$secondResponseArray = array($secondResponseRow);
foreach ($secondResponseArray as $secondResponseItem) {
if (!in_array($secondResponseItem['samecolumnname'], $response_names))
$response_names[] = $secondResponseItem['samecolumnname'];
}
}
array_walk($response_names, function($value) { echo $value . '<br />' });
If I understand what you're looking to do and the arrays are in the same scope, this should work.
$secondResponseArray = array($secondResponseRow);
$secondResponseArray = array_diff($secondResponseArray, $firstResponse);
//secondResponseArray now contains only unique items
foreach ($secondResponseArray as $secondResponseItem){
echo $secondResponseItem['samecolumnname'];
}
If you know that the keys of duplicate values will be the same you could use array_diff_assoc to get the items of the first array that aren't in the other supplied arrays.
This code
<?php
$a = array('abc', 'def', 'ghi', 'adg');
$b = array('abc', 'hfs', 'toast', 'adgi');
$r = array_diff_assoc($b, $a);
print_r($a);
print_r($r);
produces the following output
[kernel#~]php so_1.php
Array
(
[0] => abc
[1] => def
[2] => ghi
[3] => adg
)
Array
(
[1] => hfs
[2] => toast
[3] => adgi
)
[kernel#~]
I found many question and answer about this, but not match what i need.
i think it's similar/same to this question but don't know why not working for this case. So please try before judging duplicates, thank you.
array source
$avar = array(
0 => array(1,2,3,4,5,6,7,8,9),
1 => array(10,11,12,13,14,15,16,17,7,8,9,10),
23 => array(21,22,23,4,5,6,7,11,12,13,14,15,21));
desired result
$avar = array(
0 => array(1,2,3,4,5,6,7,8,9),
1 => array(10,11,12,13,14,15,16,17),
23 => array(21,22,23));
PHP script
<?php
function super_unique($array)
{
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));
foreach ($result as $key => $value)
{
if ( is_array($value) )
{
$result[$key] = super_unique($value);
}
}
return $result;
}
$result = super_unique($avar);
echo "<pre>";
print_r($result);
?>
similar question with answer but not solve my case:
How to remove duplicate values from a multi-dimensional array in PHP
PHP remove duplicate values from multidimensional array
Thank you all
$seen = array();
foreach($avar as &$entry){
$entry = array_unique(array_diff($entry,$seen));
$seen = array_merge($entry,$seen);
}
unset($entry);
var_dump($avar);