I don't want to know about other options then var_dump, because this is for a homework assignment and my teacher wants to me to make a var_dump and then let the different object appear in separate lines, I searched a shitload of sites and I simply cant find anything pls help.
This i the code in "verzenden.php"
echo '<pre>' . var_dump($_GET) . '</pre>' . '<br>';
pre was standing in <> and "" but it wont show up in here
I tried this but it is still the same as var_dump
<form method='get' action='verzend.php'>
<label>Naam: </label><input name='naam' type='text' value=''>
<label>Klas: </label><input name='klas' type='text' value=''>
<label>Nummer: </label><input name='leerlingnummer' type='text' value=''>
<label>Vak: </label><select name='vak'>
<option value='PHP'>PHP</option>
<option value='javascript'>Javascript</option>
<option value='ASP'>ASP</option>
<option value='SQL'>SQL</option>
</select>
<label>Cijfer: </label><input name='cijfer' type='number' value='5'>
<input type='submit' value='verzend' name='verzend'>
</form>
this is what it needs to become
array(6) { ["naam"]=> string(9) "Abu Saebu"
["Klas"]=> string(5) "IO1A4"
["leerlingnummer"]=> string(8) "36353535"
["vak"]=> string(3) "PHP"
["cijfer"]=> string(1) "9"
["verzend"]=> string(7) "verzend"
}
This is what I get
array(6) { ["naam"]=> string(6) "Sjoerd" ["klas"]=> string(5) "IO1D4" ["leerlingnummer"]=> string(6) "332309" ["vak"]=> string(10) "javascript" ["cijfer"]=> string(2) "24" ["verzend"]=> string(7) "verzend" }
Do it in two line instead of a concatenate.
echo '<pre>';
var_dump($_GET);
This will give you the following:
array(6) {
["naam"]=>
string(9) "Abu Saebu"
["Klas"]=>
string(5) "I01A4"
["leerlingnummer"]=>
string(8) "36353535"
["vak"]=>
string(3) "PHP"
["cijfer"]=>
string(1) "9"
["verzend"]=>
string(7) "verzend"
}
If you don't want it to break after the =>, you could use the print_r instead:
Array
(
[naam] => Abu Saebu
[Klas] => I01A4
[leerlingnummer] => 36353535
[vak] => PHP
[cijfer] => 9
[verzend] => verzend
)
However, if you really want to use var_dump, there's a cool extension out there named xdebug that will dump the details in one line like this without the pre tag:
array (size=6)
'naam' => string 'Abu Saebu' (length=9)
'Klas' => string 'I01A4' (length=5)
'leerlingnummer' => string '36353535' (length=8)
'vak' => string 'PHP' (length=3)
'cijfer' => string '9' (length=1)
'verzend' => string 'verzend' (length=7)
More information about the pre tag: http://www.w3schools.com/tags/tag_pre.asp
Related
I want to split the parameters sent through URL.
Here is my text.
"mytext=A & B
Company&anothertext=20&texttwo=SampleText&array[1]=10&array[2]=20"
Expected output:
['mytext'=>'A & B Company', 'anothertext'=> 20, 'texttwo' =>
'SampleText', array[1] => 10, array[2] => 20 ].
I tried with explode('&', $params) and parse_str($url_components['query'], $params);
both giving only A as result. I need as 'A & B Company'. How to achieve this?
I think you're looking for parse_str()?
See: https://3v4l.org/L2UZa
The result is:
array(5) {
["mytext"] => string(2) "A "
["B_Company"] => string(0) ""
["anothertext"]=> string(2) "20"
["texttwo"] => string(10) "SampleText"
["array"] => array(2) {
[1] => string(2) "10"
[2] => string(2) "20"
}
}
This differs slight from what you want because of the &. if you could replace the & with the URL encoded version %26 it would work.
See: https://3v4l.org/OXfsD
The result now is:
array(5) {
["mytext"] => string(2) "A & B_Company"
["anothertext"]=> string(2) "20"
["texttwo"] => string(10) "SampleText"
["array"] => array(2) {
[1] => string(2) "10"
[2] => string(2) "20"
}
}
Basically the & is an anomaly, it shouldn't have been there in the first place. URL query parameters should be made with urlencode(), or something equivalent, which would have replace & by %26.
I have this array:
Array ( ["id"] => 2015020052 ["gs"] => 5 ["ts"] => "THURSDAY 10/15"
["tsc"] => "final" ["bs"] => "FINAL" ["bsc"] => "final"
["atn"] => "Chicago" ["atv"] => "blackhawks" ["ats"] => "1"
["atc"] => "" ["htn"] => "Washington" ["htv"] => "capitals"
["hts"] => "4" ["htc"] => "winner" ["pl"] => true ["rl"] => true
["vl"] => true ["gcl"] => true ["gcll"] => true ["ustv"] => ""
["catv"] => "" )
I am trying to get particular values, like home team and away team and the scores, but I cannot get the values.
I am trying this:
echo "away team is ". $array['atv'];
But i just get
away team is
What am i missing????
var_dump gives me this:
Array vs array(21) { [""id""]=> string(10) "2015020051"
[""gs""]=> string(1) "5" [""ts""]=> string(16) ""THURSDAY 10/15"
[""tsc""]=> string(7) ""final"" [""bs""]=> string(7) ""FINAL""
[""bsc""]=> string(7) ""final"" [""atn""]=> string(8) ""Ottawa""
[""atv""]=> string(10) ""senators"" [""ats""]=> string(3) ""0""
[""atc""]=> string(2) """" [""htn""]=> string(12) ""Pittsburgh""
[""htv""]=> string(10) ""penguins"" [""hts""]=> string(3) ""2""
[""htc""]=> string(8) ""winner"" [""pl""]=> string(4) "true"
[""rl""]=> string(4) "true" [""vl""]=> string(4) "true"
[""gcl""]=> string(4) "true" [""gcll""]=> string(4) "true"
[""ustv""]=> string(2) """" [""catv""]=> string(2) """" }
I was also facing the same problem.
Problem:
The array was retrieved in Database [saved as a JSON encoded variable].
I was not getting the array element by key like $arr['key']
Solution:
Tried everything, no success.
Lastly, tried json_decode() and json_encode().
$arr = json_decode(json_encode($arr), TRUE);
Note that second parameter TRUE is very important. Otherwise, you will get object returned.
And it worked like charm.
You are not doing a correct associative array, this must be like this:
<?php
$array = [
'id' => 2015020052,
'gs' => 5,
'ts' => "THURSDAY 10/15",
'tsc' => "final",
'bs' => "FINAL",
'bsc' => "final",
'atn' => "Chicago",
...
];
Now you can get the value:
echo "away team is ". $array['atv'];
Here is the Demo
I have this kind of string :
$string = "<strong>Blabla1</strong> Blaabla2<br /> Blaabla3 <strong>Blaabla4</strong> Blaabla5 Blaabla6<br /><br /> Blaabla7 <span style='color:#B22222;'>Blaabla8</span> Blaabla9";
I'm trying to explode each word where there is a " " or "<br />" with preg_split .
My conditions :
For each word (Blablax), I need to keep his tags like <strong>, <span>, <em>... but split him after a <br /> or more <br />
I tried this, thanks to another post on stackoverflow :
preg_split('/<br(\s\/)?>\K|\s/',$string,null,PREG_SPLIT_NO_EMPTY);
OUTPUT:
array (size=12)
0 => string '<strong>Blabla1</strong>' (length=24)
1 => string 'Blaabla2<br />' (length=14)
2 => string 'Blaabla3' (length=8)
3 => string '<strong>Blaabla4</strong>' (length=25)
4 => string 'Blaabla5' (length=8)
5 => string 'Blaabla6<br />' (length=14)
6 => string '<br' (length=3)
7 => string '/>' (length=2)
8 => string 'Blaabla7' (length=8)
9 => string '<span' (length=5)
10 => string 'style='color:#B22222;'>Blaabla8</span>' (length=38)
11 => string 'Blaabla9' (length=8)
Everything works except for index 6 and index 7 (see above in OUTPUT) and index 9 and index 10
What I'll exepect :
array (size=12)
0 => string '<strong>Blabla1</strong>' (length=24)
1 => string 'Blaabla2<br />' (length=14)
2 => string 'Blaabla3' (length=8)
3 => string '<strong>Blaabla4</strong>' (length=25)
4 => string 'Blaabla5' (length=8)
5 => string 'Blaabla6<br /><br />' (length=14)
6 => string 'Blaabla7' (length=8)
7 => string '<span style='color:#B22222;'>Blaabla8</span>' (length=45)
8 => string 'Blaabla9' (length=8)
See index 5 and index 7
My regex works if I have just one <br /> but if more than one, there is a mistakes... idem if I have a <span style...>
Thanks !
$string = "<strong>Blabla1</strong> Blaabla2<br /> Blaabla3 <strong>Blaabla4</strong> Blaabla5 Blaabla6<br /><br /> Blaabla7 <span style='color:#B22222;'>Blaabla8</span> Blaabla9";
$matches = preg_split('/(<br.*?>|<span.*>)+\K|\s/sim', $string, null, PREG_SPLIT_NO_EMPTY );
var_dump($matches);
/*
array(9) {
[0]=>
string(24) "<strong>Blabla1</strong>"
[1]=>
string(14) "Blaabla2<br />"
[2]=>
string(8) "Blaabla3"
[3]=>
string(25) "<strong>Blaabla4</strong>"
[4]=>
string(8) "Blaabla5"
[5]=>
string(20) "Blaabla6<br /><br />"
[6]=>
string(8) "Blaabla7"
[7]=>
string(44) "<span style='color:#B22222;'>Blaabla8</span>"
[8]=>
string(8) "Blaabla9"
}
*/
DEMO
Looking at your expected array at index 5 and index 7, you probably want this regex:
preg_split('~(?:</?[a-zA-Z0-9][^>]*+>|\S)++\K|\s~',$string,null,PREG_SPLIT_NO_EMPTY);
Demo on ideone
Output:
array(9) {
[0]=>
string(24) "<strong>Blabla1</strong>"
[1]=>
string(14) "Blaabla2<br />"
[2]=>
string(8) "Blaabla3"
[3]=>
string(25) "<strong>Blaabla4</strong>"
[4]=>
string(8) "Blaabla5"
[5]=>
string(20) "Blaabla6<br /><br />"
[6]=>
string(8) "Blaabla7"
[7]=>
string(44) "<span style='color:#B22222;'>Blaabla8</span>"
[8]=>
string(8) "Blaabla9"
}
The regex attempts to match a full tag, and if a full tag can't be consumed, it will consume one non-space character, then rinse and repeat. This will prevent tags from being split, which gives expected output for index 5 and 7.
I wouldn't recommend doing this with regex, though. I didn't consult the HTML specs when writing the regex, so the regex is very brittle and may break on input in the wild. You might want to learn how to parse HTML properly with one of the libraries listed in this question: How do you parse and process HTML/XML in PHP?
Here is the regex
((?:<br\s*\/?>)+)|(?<!<br)\s+(?!\/?>)
Use this with preg_replace using $1\n as a replacement string, and then you can split by newline to get the array (removing empty ones).
See demo.
I'm trying to parse an array that looks like this:
array(1) {
["StrategischeDoelstellingenPerDepartement"] => array(412) {
[0] => array(5) {
["CodeDepartement"] => string(8) "DEPBRAND"
["NummerHoofdstrategischeDoelstelling"] => string(1) "1"
["Nummer"] => string(2) "27"
["Titel"] => string(22) "DSD 01 - HULPVERLENING"
["IdBudgetronde"] => string(1) "2"
}
[1] => array(5) {
["CodeDepartement"] => string(8) "DEPBRAND"
["NummerHoofdstrategischeDoelstelling"] => string(1) "2"
["Nummer"] => string(2) "28"
["Titel"] => string(24) "DSD 02 - Dienstverlening"
["IdBudgetronde"] => string(1) "2"
}
[2] => array(5) {
["CodeDepartement"] => string(8) "DEPBRAND"
["NummerHoofdstrategischeDoelstelling"] => string(1) "2"
["Nummer"] => string(2) "29"
["Titel"] => string(16) "DSD 03 - KLANTEN"
["IdBudgetronde"] => string(1) "2"
}
...
(The array goes on but it's too big to post it here in its entirety)
I can do a foreach loop on the array like this:
foreach($my_arr->StrategischeDoelstellingenPerDepartement as $row){
echo "i found one <br>";
}
However, I want to do the same thing on other arrays and I want to make the function generic. The first key (StrategischeDoelstellingenPerDepartement in this case) can sometimes change, which is why I'd like to do it generically. I've already tried the following:
foreach($my_arr[0] as $row){
echo "i found one <br>";
}
But then I get the following notice, and no data:
Notice: Undefined offset: 0 in C:\Users\Thomas\Documents\GitHub\Backstage\application\controllers\AdminController.php on line 29
This is probably a silly question, but I'm new to PHP and this seemed like the right way to do it. Obviously, it isn't. Can anyone help me out, please?
Use reset to grab the first element of $my_arr without knowing the key name:
$a = reset($my_arr);
foreach($a as $row){
echo "i found one <br>";
}
Shift the sub-array off the main array and loop over it:
$sub = array_shift($my_arr);
foreach ($sub as $row) {
echo $row['Titel'], "<br>";
}
You are trying to do is object, not array $my_arr->StrategischeDoelstellingenPerDepartement.
You could use isset() to check the index existence:
if(isset($my_arr['StrategischeDoelstellingenPerDepartement'])){
foreach($my_arr['StrategischeDoelstellingenPerDepartement'] as $row){
echo "i found one <br>";
}
}
Or, you could use array_values() to ignore the array keys and to make it an index array:
$my_new_arr = array_values($my_arr);
foreach($my_new_arr as $row){
echo "i found one <br>";
}
Use current ref : http://in3.php.net/manual/en/function.current.php
$a = current($my_arr);
foreach($a as $row){
echo "i found one <br>";
}
I'm trying to print an ID from MySQL, the field loads into an array and is visible via print_r but I can't echo it or transfer it to another variable ... what am I missing?
if ( $_POST['section'] == "freelance" ) {
$field_name = "promoter";
} else {
$field_name = "connector";
}
echo $row[$field_name.'_login_ID']
As requested the results of var_dump($row)
array(13) {
["connector_login_id"] => string(2) "14"
["connector_type"] => string(10) "non-profit"
["unique_code"] => string(9) "test-t001"
["update_code"] => string(1) "N"
["md5ID"] => string(0) ""
["username"] => string(6) "bugger"
["connectorEmail"] => string(17) "gzigner#gmail.com"
["password"] => string(32) "098f6bcd4621d373cade4e832627b4f6"
["connectorPass"] => string(4) "test"
["active"] => string(1) "Y"
["modified"] => string(19) "2009-08-21 15:37:22"
["lastlogin"] => string(19) "0000-00-00 00:00:00"
["md5email" ]=> string(32) "051cba58da33fac6b2d18af5182079f4"
}
$row[$field_name.'_login_ID'] <-- "ID"
array(13) {
["connector_login_id"] <-- "id"
Seems like a simple typo to me.
Alternatively, are you sure $field_name gets set to 'connector', since 'promoter_login_id' doesn't exist in this array.
This is purely speculation without your code, but it's probable that the field you are trying to echo contains a hyphen, e.g. "mytable-id", considering that it does indeed show when you use print_r() to print out the entire array. If this is the case you would need to use {'mytable-id'} to get/echo it's value:
echo($dataArray->MyTable->{'mytable-id'});
*Edit: I don't know if your code is copy and pasted, but the value you are trying to print is:
echo $row[$field_name.'_login_ID'];
instead of:
echo $row[$field_name.'_login_id'];
PHP is case-sensitive. You could also try this:
$field_name = $field_name.'_login_id';
echo $row[$field_name];
or
$field_name = $field_name.'_login_id';
echo $row['$field_name'];