Display exploded array [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is a very noob question. The previous developer has coded the lines as below
$a = array("30"=>"ok","40"=>"yes");
$b = "hi";
$c = $a."|".$b;
$d = explode("|",$c);
print_r($d[0]);
How can I display the array array("30"=>"ok","40"=>"yes")? print_r($d[0]); seems to print just array

This prints "array" instead of the actual array values is because this line:
$c = $a."|".$b;
What you're doing is saying:
$c = [array] + [string] + [string];
which will force array to be transformed into a string, which is just "array"
if you really want a | separated string of array indices, you could theoretically do this:
$c = implode("|",$a)."|".$b;
But the real best solution here would be to add something to the array before exploding the array:
$a['50'] = 'hi';
$d = explode("|", $c);

Related

preg match for known value plus 7 charecters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone help me get the value of IFC/USD 0.00001031 from the webpage http://infinitecoin.com.
I need to grab the value after IFC/USD..
The way I thought to do it was to:
$file_string = file_get_contents('http://infinitecoin.com');
preg_match('/IFC/USD plus next 11 charecters', $file_string, $title);
$value = $title[1];
echo $title_out ;
But im not sure how to ask PHP to find IFC/USD then return the next 11 characters after that.
If I could accomplish this, my task would be solved..
Any help would be great.
Thank you
Jason
$output = array();
preg_match("/(?<=IFC\/USD\s)[\d\.]+/", 'IFC/USD 0.00001015', $output);
$output will look like this:
Array
(
[0] => 0.00001015
)

i need Apostrophe in a variable result MYSQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I receive data in this format like:
C00001,C00302,C00303,C00287,C00286,C00285,C00017
in a variable but i need in this shape:
'C00001','C00302','C00303','C00287','C00286','C00285','C00017'
i am new in mysql kindly any help
You can explode the data, modify it, then re-implode it.
$data = 'C00001,C00302,C00303,C00287,C00286,C00285,C00017';
$arr = explode(',', $data);
$new_arr = array_map(function($a){
return "'{$a}'";
}, $arr);
$new_data = implode(',', $new_arr);

send values separated by commas and uses as variables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a variable posting to another page and when echoed out, it looks like
Acme, Upgrade Site, Kansas
I want to separate those by the comma and assign 3 variable like
$Company = ‘Acme’;
$Action = ‘Upgrade Site’;
$Location = ‘Kansas’;”
$str= 'Acme, Upgrade Site, Kansas' ;
$arr = explode(',',$str);
$Company = $arr[0];
$Action = $arr[1];
$Location = $arr[2];
try
So the best bet here I think is probably to use explode:
// Assume $inputString is your input
$explodedInput = explode(",", $inputString);
// Now, $explodedInput[0] will be Company,
// $explodedInput[1] will be "Upgrade Site", etc...
$Company = $explodedInput[0] //...

A bit of an explanation on the array_diff_uassoc function in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Sorry for another noob question, but... Can someone please explain to me what the function myfunction is actually doing. I understand it's checking if the variables $a and $b are identical and than it's suppose to return 0 if they're identical but the next return is confusing. I see their using the ternary operators.
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"green","d"=>"blue");
$a3=array("e"=>"yellow","a"=>"red","d"=>"blue");
$result=array_diff_uassoc($a1,$a2,$a3,"myfunction");
print_r($result);
the print_r returns
Array ( [c] => blue )
but how did we get here...
As stated in the documentation of array_diff_uassoc, it returns the entries from first argument that are unique compared to other arguments. And the last argument is the name of the function it uses to check whether item is unique or not.
So because only $a1 contains 'c'=>'blue' it is returned.

Break string into fields [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string input using PHP that look like this:
Log,,t1,12.12:t2,20.23:t3,30.00:t4,50.20:11.23
I want to save it into MySQL
field:reading
t1:12.12
t2:20.23
t3:30.00
t4:50.20
Temp:11.23
Can anyone give me an direction?
Begin you have to parse to rows and columns
$string = 't1,12.12:t2,20.23:t3,30.00:t4,50.20:11.23';
$rows = explode(':', $string);
$table = array();
foreach(rows as $row) {
$table[] = explode(',', $row);
}
You can use regex below:
if (preg_match_all('/[a-z]+\d+,\d+\.\d+|\d+\.\d+/i', $string, $matches) {
var_dump($matches[0]);
}
The regex can be a bit simpler if each field will called "t+number":
if (preg_match_all('/t\d+,\d+\.\d+|\d+\.\d+/i', $string, $matches) {
var_dump($matches[0]);
}

Categories