Problems creating dynamic associative array - php

I want to create dynamic associative array from two array's
one array is be used ($l_arr) for key and other array is used for value ($r_arr) when i display $map in output i can see there is associative array created but when i print echo $map['key'] output is blank please help me guyz. here is the code and output,
<?php
$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
for($i=0;$i<$n;$i++)
{
$arr_temp = fgets($handle);
$l_arr[$i]= preg_replace("/[0-9,.]/", "", $arr_temp);
$r_arr[$i]=preg_replace("/[^0-9,.]/", "", $arr_temp);
}
for($i=0;$i<$n;$i++)
{
$arr_temp = fgets($handle);
$op[$i]=$arr_temp;
}
for($i=0;$i<$n;$i++)
{
$map[$l_arr[$i]]=$r_arr[$i];
}
print_r($map);
echo "value of sam is".$map['sam'];
?>
and output is
Array
(
[sam
] => 99912222
[tom
] => 11122222
[harry
] => 12299933
)
value of sam is

As you can probably see, there are whitespaces in your output - look at new lines after each array index. You need to trim() your preg_replace() here:
$l_arr[$i] = trim(preg_replace("/[0-9,.]/", "", $arr_temp));
$r_arr[$i] = trim(preg_replace("/[^0-9,.]/", "", $arr_temp));

Related

Textarea lines to array using php

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

Dimensional array

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,

To create array from 1 php variable Why out put not same array?

To create array from 1 php variable Why out put not same array ?
in this code i create array using php variable
<?PHP
$xxx = "'Free', 'Include', 'Offer', 'Provide'";
$xxx_array = array($xxx);
echo '<pre>'; print_r($xxx_array); echo '</pre>';
?>
and echo is
Array
(
[0] => 'Free', 'Include', 'Offer', 'Provide'
)
how to echo like this
Array
(
[0] => Free
[1] => Include
[2] => Offer
[3] => Provide
)
<?php
$xxx = "'Free', 'Include', 'Offer', 'Provide'";
// Split by ","
$separatedValues = explode(',', $xxx);
// Remove the single quotation marks
for($i = 0; $i < count($separatedValues); ++$i) {
$separatedValues[$i] = str_replace("'", '', $separatedValues[$i]);
}
var_dump($separatedValues);
?>
It is all about the explode() (http://de.php.net/explode) function.
Read up on the explode() function. The below code accomplishes what you ask.
<?PHP
$xxx = "'Free', 'Include', 'Offer', 'Provide'";
$xxx_array = array(explode(",", $xxx);
echo '<pre>';
print_r($xxx_array);
echo '</pre>';
?>
If you want to mimic actually creating the array (and not having the values quoted within the array), use this:
$xxx_array = array_map( function( $el) { return trim( $el, "' "); }, explode(',', $xxx));
This trims the ' and spaces from the beginning and ends of the elements after converting the string to an array.

php split array string into data

Below script is shown search string in a text file, my text file has contents like:
60, 1, 1, 188, pdgje5566
60, 1, 1, 188, pdgje5565
if(!empty($extTxId)){
$searchfor = $extTxId;
$matches = array();
$handle = #fopen($file, "r");
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
if(strpos($buffer, $searchfor) !== FALSE)
$matches[] = $buffer;
}
fclose($handle);
}
print_r($matches);
it output as
Array ( [0] => 60, 1, 1, 188, ppje5566 )
however i want to explode the data which is separate by comma(,) and store into variable, is that anyway to do it?
thanks.
i want to explode the data which is separate by comma(,)
Use explode().
If this is a CSV file (or some other delimited file), I suggest looking at fgetcsv(). It is effectively fgets() and explode(), which seems to be what you need.
Since you have spaces between each comma, you can trim each element.
// Iterate through each item int the array (you can do $matches[0] if you just want the first)
foreach ($matches as $match)
{
$output_array = array_map('trim',explode(',',$match));
print_r($output_array);
}
Output:
Array
(
[0] => 60
[1] => 1
[2] => 1
[3] => 188
[4] => pdgje5565
)
Example: http://codepad.org/aSyVX5Bj
Try like
foreach(Array[0] as $element)
{
echo $element;
}
If you know the exact structure of the input you're getting, you could do something like:
$string = '60, 1, 1, 188, pdgje5565';
// Using trim just to be sure
list($varA, $varB, $varC, $varD, $varE) = explode(', ', trim($string));
This way you would get the results as follows:
$varA = 60;
$varB = 1;
$varC = 1;
$varD = 188;
$varE = 'pdgje5565';
The main advantage here is you would get decent variables instead of array with abstract indexes, but it is only good if you're sure about you're structure being constant.

what is the best way to split string data that is delimited in php

Looking for php help, best practice. What is a good way to break these lines? I was looking into explode or is a regex better? and then maybe display them into a table? thanks for your input.
input file:
PRE:abc:KEY1:null:KEY2:/myproject/data/dat_abc_2010120810.gz1
PRE:def:KEY1:sdsu:KEY2:mail_abc.dat.2010120810.gz1
expected output or web page for display:
PRE KEY1 KEY2
=== ==== ======================================
abc null /myproject/data/dat_abc_2010120810.gz1
def sdsu mail_abc.dat.2010120810.gz1
If you have a file like that I would do it in two steps if I were you...
1st step
Use file() to get an array representing the file.
2nd step
Now you can use explode() to get all the different columns and output them.
Quick example:
<?php
$output = "";
$file = file("data.txt");
foreach ($file as $line)
{
$cols = explode (":", $line);
$output .= "{$cols[0]} {$cols[1]}";
}
?>
Hope this helps.
explode will work just fine:
$fp = fopen('myfile.txt', 'r');
while ($line = fgets($fp))
$parts = explode(':', $line);
$array = array();
for ($i=0; $i<count($parts); $i+=2) {
$array[$parts[$i]] = isset($parts[$i+1]) ? $parts[$i+1] : 'null';
}
print_r($array);
}
Will output:
Array
(
[PRE] => abc
[KEY1] => null
[KEY2] => /myproject/data/dat_abc_2010120810.gz1
)
Array
(
[PRE] => def
[KEY1] => sdsu
[KEY2] => mail_abc.dat.2010120810.gz1
)

Categories