Hi i'm using Laravel 5 and i have an array with 5 element
array(5) { [0]=> string(25) "Ruby on rails" [1]=> string(14)
"Web Develop" [2]=> string(12) "Asp.net" [3]=> string(3) "Php" [4]=>
string(4) "Java" }
And i use this function to create friendly url
foreach ($tag as $value)
{
$href = str_slug($value, "-");
}
And i get this
string(18) "Ruby-on-rails" string(10) "Web-Develop" string(9)
"asp-net" string(3) "php" string(4) "java"
So how i can foreach those string i get and pass into view to make friendly url.Thanks for help
If I understand it the right way, you are trying to merge all your tags together to one URL. The problem is, that you create a slug for each single tag and not for all tags.
The output you gave us seems to be an array, so you could use the php function implode to stick them all together. For example:
$slug = implode('-', $yourArrayWithSingleSlugs )
This would put a - between each single slug in your array and combine them to a string.
Related
I am looping through GET values and only getting 13 values, not matter what URL I submit. Also, it is not getting the values in sequential order...
When I loop through I only get 13 values, as well as when I use a var_dump on $_GET itself; even though there are many more values to retrieve.
Here is the URL:
website.com/Questionaire.php?SurveyName=TV%20Quiz&SurveyType=MultipleChoice&Q1=Choose%20a%20character%20off%20of%20Happy%20Days?&A1=Benny%20the%20bull&A2=The%20Fonz&A3=Jack%20Cracker&Q3=Favorite%20Friends%20character?&A1=Ross&A2=Monica&A4=Joey&A5=Rachel&A6=Chandler&A7=Phoebe&Q8=Favorite%20Nickelodeon%20show?&A1=Hey%20Arnold!&A2=Rugrats&A8=Ahhhh!%20Real%20Montsters
Here are my results:
SurveyName: TV Quiz
SurveyType: MultipleChoice
Q1: Choose a character off of Happy Days?
A1: Hey Arnold!
A2: Rugrats
A3: Jack Cracker
Q3: Favorite Friends character?
A4: Joey
A5: Rachel
A6: Chandler
A7: Phoebe
Q8: Favorite Nickelodeon show?
A8: Ahhhh! Real Montsters
As you can see the results are not in sequential order and some values are even missing.
var_dump($_GET);
foreach ($_GET as $key => $value) {
echo $key.": ".$value."<br/>\n";
}
I expect these results:
SurveyName=TV Quiz
SurveyType=MultipleChoice
Q1=Choose a character off of Happy Days?
A1=Benny the bull //<- missed
A2=The Fonz //<- missed
A3=Jack Cracker
Q3=Favorite Friends character?
A1=Ross //<- missed
A2=Monica //<- missed
A4=Joey
A5=Rachel
A6=Chandler
A7=Phoebe
Q8=Favorite Nickelodeon show?
A1=Hey Arnold!
A2=Rugrats
A8=Ahhhh! Real Montsters
You cannot have identical parameter names in your query string, otherwise the last value will overwrite the previous ones. You need to have unique answer names or you will lose data. You can imagine PHP adding the parameters to $_GET with the following pseudo-code:
foreach($param as $key=>$val) {
$_GET[$key] = $val;
}
Because of this, parameters appear in the order in which they first show up in the request. So the query string ?A=1&B=2&A=3&C=4 will have A appear first, then B, and finally C. The last value for an identical parameter is the one used, so we get the following $_GET result:
array(
'A'=>3,
'B'=>2,
'C'=>4
);
Consider adding the question identifier as a prefix for each answer. For example, instead of A1 do Q1A1 and Q2A1. This will ensure that your data is not overwritten.
I would suggest using array notation for query string parameter names so that order is maintained. Something like:
?SurveyName=TV Quiz
&SurveyType=MultipleChoice
&Q[1]=Choose a character off of Happy Days?
&A[1][1]=Benny the bull
&A[1][2]=The Fonz
&A[1][3]=Jack Cracker
&Q[3]=Favorite Friends character?
&A[3][1]=Ross
&A[3][2]=Monica
&A[3][4]=Joey
&A[3][5]=Rachel
&A[3][6]=Chandler
&A[3][7]=Phoebe
&Q[8]=Favorite Nickelodeon show?
&A[8][1]=Hey Arnold!
&A[8][2]=Rugrats
&A[8][8]=Ahhhh! Real Montsters
When you name query string parameter like that, PHP will parse them into arrays:
array(4) {
["SurveyName"]=>
string(7) "TV Quiz"
["SurveyType"]=>
string(14) "MultipleChoice"
["Q"]=>
array(3) {
[1]=>
string(37) "Choose a character off of Happy Days?"
[3]=>
string(27) "Favorite Friends character?"
[8]=>
string(26) "Favorite Nickelodeon show?"
}
["A"]=>
array(3) {
[1]=>
array(3) {
[1]=>
string(14) "Benny the bull"
[2]=>
string(8) "The Fonz"
[3]=>
string(12) "Jack Cracker"
}
[3]=>
array(6) {
[1]=>
string(4) "Ross"
[2]=>
string(6) "Monica"
[4]=>
string(4) "Joey"
[5]=>
string(6) "Rachel"
[6]=>
string(8) "Chandler"
[7]=>
string(6) "Phoebe"
}
[8]=>
array(3) {
[1]=>
string(11) "Hey Arnold!"
[2]=>
string(7) "Rugrats"
[8]=>
string(21) "Ahhhh! Real Montsters"
}
}
}
I have finally managed to retrive data from mongoDB within PHP. How ever I am not able to retrieve single elements from this array looking. I can only vardump() the cursor. How is it possible to print single elements from this array that seems to be made up of objects?
object(stdClass)#11 (7) { ["_id"]=> object(MongoDB\BSON\ObjectID)#9 (1) { ["oid"]=> string(24) "5a4a2cf55ff0f310cbf1c3a4" } ["Category"]=> string(9) "Allgemein" ["DateAdded"]=> object(MongoDB\BSON\UTCDateTime)#10 (1) { ["milliseconds"]=> string(13) "1514810613331" } ["Name"]=> string(4) "Welt" ["Website"]=> string(11) "www.welt.de" ["Active"]=> bool(true) ["Country"]=> string(2) "DE" }
I couldnt find anything on goolgle or PHP/mongodb documentation. Why cant I just do $array["_id"]? And how can I retrieve _id for example?
The resource is an object of stdClass. So you need to use:
echo $array->_id;
In case, if you want to use arrays, use get_object_vars() function. That way:
$array = get_object_vars($array);
echo $array["_id"];
And then you can use objects as arrays.
I have written an xml-parser to read an XML-file. The XML-file is not mine so I can't change the structure. Things work great till I got to this special point. I want to read a value but I don't have a key to access this value.
I marked the values (in red) in the screenshot below which I want to access.
When I dump the parent element (the PRAT->VALUE) I get this in return:
object(SimpleXMLElement)#31 (3) { ["#attributes"]=> array(5) { ["nr"]=> string(1) "1" ["unit"]=> string(3) "bar" ["unit_id"]=> string(4) "3103" ["vo"]=> string(0) "" ["vo_id"]=> string(0) "" } [0]=> string(2) "20" [1]=> string(1) "2" }
As seen, at the end of the dump the values that I want to access are presented. I tried to access it like an array but that doesn't work. The values are not part of the attributes.
use (String) keyword in front of it.
eg.
echo (String) PRAT->VALUE;
I'm trying to get the values of a sub array out in a foreach loop.
I looked at this example as it seemed really relevant to my problem, but unfortunately I've not been able to get it to work, and I'm hoping someone here can pick up where I've gone wrong.
PHP foreach not working with sub-array
I have an array called $booked and inside that array I have a sub array $booked['30_booked'].
Within that second array there are multiple values, such as title, name, address etc.
My code currently looks like this:
foreach($booked['30_booking'] as $new_var){
var_dump('</br>', $booked['30_booking']);
var_dump('</br>', $new_var);
var_dump($new_var['title'], $new_var->title, $booked['30_booking']->title); exit();
}
I've output the data as you can see above in var_dump statements to try and get one of these methods to work.
Unfortunately nothing within $new_var is pulling out the title, but $booked['30_booking']->title
Have I not put it into the foreach statement correctly?
All help appreciated - thanks!
EDIT:
Main array output snippet:
array(6) { ["30_booked"]=> object(stdClass)#21 (34) { ["id"]=> string(2) "30" ["title"]=> string(2) "Ms" ["firstname"]=> string(5) "FIRST NAME" ["surname"]=> string(9) "LAST NAME" ["address"]=> string(6) "- -- -" ["postcode"]=> string(7) "FAK E99" ["country"]=> string(14) "United Kingdom" ["phone"]=> string(11) "01221111111" ["alt_phone"]=> string(0) "" ["email"]=> string(25) "fake#fake.co.uk" ["notes"]=> string(8) "FAKE DEAL" } }
EDIT 2:
Sub Array $booked['30_booking'] snippet:
object(stdClass)#21 (34) { ["id"]=> string(2) "30" ["title"]=> string(2) "Ms" ["firstname"]=> string(5) "FIRST NAME" ["surname"]=> string(9) "LAST NAME" ["address"]=> string(6) "- -- -" ["postcode"]=> string(7) "FAK E99" ["country"]=> string(14) "United Kingdom" ["phone"]=> string(11) "01221111111" ["alt_phone"]=> string(0) "" ["email"]=> string(25) "fake#fake.co.uk" ["notes"]=> string(8) "FAKE DEAL" }
EDIT 3:
My var_dump of the $new_var by itself is bringing back the value of the id - but when I try and get the second value out the sub array "title" it doesn't return anything.
FINAL FIX:
Thanks to Kita I realised I was returning a std class object and not a second array, something that I stupidly missed the first time round. Because of that I can't actually foreach on the object.
Which led me to this post which will help me fix the issue:
PHP foreach array with stdClass Object
Thank you very much for all your help!!!
You expected an array inside $booked['30_booking'] but in fact there was a stdClass object.
array(6) {
["30_booked"]=> object(stdClass)#21 (34) {
["id"]=> string(2) "30"
["title"]=> string(2) "Ms"
["firstname"]=> string(5) "FIRST NAME"
["surname"]=> string(9) "LAST NAME"
["address"]=> string(6) "- -- -"
["postcode"]=> string(7) "FAK E99"
["country"]=> string(14) "United Kingdom"
["phone"]=> string(11) "01221111111"
["alt_phone"]=> string(0) ""
["email"]=> string(25) "fake#fake.co.uk"
["notes"]=> string(8) "FAKE DEAL"
}
//I assume you have left out the other array elements from the Main array snippet.
}
Getting stdClass instead of array usually happens when you parse a JSON string with json_decode() without the second parameter.
// without second parameter
var_dump( json_decode( '{"id":"30"}' ) );
object(stdClass)#1 (1) {
["id"]=>
string(2) "30"
}
// 'true' as second parameter
var_dump( json_decode( '{"id":"30"}', true ) );
array(1) {
["id"]=>
string(2) "30"
}
Above examples are hosted at: http://ideone.com/GNMRlD
Since stdClass object itself does not provide iteration functionality, using it with foreach will yield errors.
You can convert stdClass into array with functions such as http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/ .
For eg. if your array looks like below. foreach which I used will be working
$new_array = array(array('total'=>array('title'=>'test','text'=>'text')));
foreach ($new_array as $val)
{
print_r($val['total']['title']); // Use this for array
print_r($val->total->title); // Use this for object
}
if your array looks like below. Use the below foreach.
$new_array = array(array('total'=>array(array('title'=>'test','text'=>'text'),array('title'=>'test1','text'=>'text1'))));
foreach ($new_array as $val)
{
foreach($val['total'] as $new_val)
{
print_r($new_val['title']);
}
}
You can try this, I hope that helps you
foreach($booked as $new_var){
var_dump('</br>', $new_var);
var_dump('</br>',$new_var['30_booking']->title);
}
I think in your foreach there is mistake of word 30_booking
since in your main array out put display it shows 30_booked
check that also
I hope I can make this question clear enough.
I'm looking to put a list of arrays inside one master array, dynamically, so that it looks like this:
masterarray {
array1
{ [0]=>VAL1 [1]=>VAL2 }
array2
{ [0]=>VAL1 [1]=>VAL2 }
array3
{ [0]=>VAL1 [1]=>VAL2 }
}
I've tried, but I could only get it to look like this:
array(1) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(2) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
array(3) { [0]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [1]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } [2]=> array(2) { [0]=> string(1) "1" [1]=> string(13) "CODE" } }
And that's definitely not what I'm aiming for. Nothing seems contained. I need the format specified above.
I'm using the explode function on a string pulled from a file to make this table of arrays (I think you call it that)
Here is the code I'm using that's not working.
$variabledebugging = file("FILE.TXT");//LOOK IN THIS FILE FOR THE NUMBER AND SET IT TO A VAR.
$i=0;
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t",$variabledebugging[$i]);
var_dump($variabledebuggingtbl);
$i++;
}
I've tried a couple of different variations, but that's the one I'm using now.
To be clear, that file being pulled (each line as a value in an array) has 2 things written to each line, separated by a tab character, so that's the system I'm going on.
Thank you! I'm sure this is a simple task, I just can't think it through.
Oh and while I'm at is there a way to make debugging more readable?
You ARE getting the right result. The reason it seems wrong is that you are running var_dump inside the loop. And why don't you use the $placeholder variable?
$variabledebugging = file("FILE.TXT");
foreach($variabledebugging as $placeholder){
$variabledebuggingtbl[] = explode("\t", $placeholder);
}
var_dump($variabledebuggingtbl);
I'm not sure what you mean by "making debugging more readable", but if you want some linebreaks and indentation you should just look in the generated HTML code. var_dump do add spacing to make it readable but it is ignored by the web browser. If you don't want to read the HTML source, just add your var_dump to a <pre> element.