Convert PHP Array to String? - php

I'm working with this PHP Array and I'm trying to convert it into a string:
$args=shortcode_atts( array(
'setting' => 'value',
'setting' => 'value',
'setting' => 'value',
), $atts);
The result should look like this:
' "setting":"value","setting":"value" '
I'm not sure how to loop through this? I've also noticed a lot of docs online that include the comma at the end of the last array item - is this ok or should I be in the habit of not including the comma?

I believe that you are looking for json encoded data:
$json = json_encode($args);

Related

Multidimensional Array Not Echoing As Expected

I am setting this array manually:
$schools = array(
'Indiana University'=>array(
'initials'=>'IU',
'color'=>'red',
'directory'=>'indiana'
)
);
But it won't echo "IU" when I use:
echo $schools[0][0];
It does show correctly when I do:
print_r($schools);
I'm sure I'm messing up something dumb, but I have no idea what and I've been staring at it for hours. This array is actually part of a larger array with multiple universities, but when I trim it down to just this, it doesn't work.
PHP arrays support two types of keys - numerical and strings.
If you just push a value onto an array, it will use numerical keys by default. E.g.
$schools[] = 'Indiana University';
echo $schools[0]; // Indiana University
However, when you use string keys, you access the array values using the string key. E.g.
$schools = array(
'Indiana University' => array(
'initials' => 'IU',
'color' => 'red',
'directory' => 'indiana'
)
);
echo $schools['Indiana University']['initials']; // UI

Why is \r\n appearing when I JSON encode an array

I'm creating some arrays and turning them into JSON strings and I noticed something strange--some of the strings,
when I JSON encode them, are getting \r\n added onto the front and end of the of the strings.
The strings I'm encoding are pulled from HTML elements.
$arr = array(
'licStat' => $rows2[13]->nodeValue,
'expDate' => dateReplace($data[5]->nodeValue),
'dicAct' => $rows2[11]->nodeValue
);
echo json_encode($arr);
Expected output:
{"licStat":"Expired","expDate":"1999-12-20","dicAct":"Yes"}
Actual output:
{"licStat":"\r\n Expired\r\n ","expDate":"1999-12-20","dicAct":"\r\n Yes\r\n "}
It seems $rows2[13]->nodeValue and $rows2[11]->nodeValue have carry return and line feeds in them.
You can use trim() to get rid of them:
$arr = array(
'licStat' => trim($rows2[13]->nodeValue),
'expDate' => dateReplace($data[5]->nodeValue),
'dicAct' => trim($rows2[11]->nodeValue)
);
echo json_encode($arr);

php array to json array (using sendgird)

I would like to use a php array in this example rather than have to hard code the array. How would I use a php to replace this json example?
Here the working code with the json array:
$json_string = array(
'to' => array(
'example1#sendgrid.com', 'example2#sendgrid.com'
),
'category' => 'test_category'
);
But I need to replace the 'to' values with my own php array. I tried this but it doesn't work:
$myEmails[] = array('emailOne#gmail.com','email2#gmail.com');
$json_string = array(
'to' => $myEmails, /// DOES NOT WORK
'category' => 'test_category'
);
What code for the json could I use to add my own php array values here? In short I am trying to send multiple emails using sendgrid but I thought this might work but its not.
Just get rid of the square braces [] after $myEmails, and everything should work:
<?php
$myEmails = array('emailOne#gmail.com','email2#gmail.com');
$json_string = array(
'to' => $myEmails, /// DOES NOT WORK
'category' => 'test_category'
);
var_dump($json_string);
?>

How to use json and php

I'm new to JSON, but I have experience in PHP. Can someone explain to me how JSON works, especially with PHP, and EASY way would be nice.
EX: I have a php array like:
array(
array('id' => 1, 'img' => "http.img1.png", 'title' => 'ice cream'),
array('id' => 2, 'img' => "http.img2.png", 'title' => 'silly snail'),
array('id' => 3, 'img' => "http.img3.png", 'title' => 'big bear'),
array('id' => 4, 'img' => "http.img4.png", 'title' => 'Funny cat'),
);
is this fine, or should I alter this array? I want to convert this to a JSON Object. In the php array should there be a parent, and do I have to assign array elements as children, or can each php obj be it's own JSON obj? Thank you!
Just run json_encode on the variable that you want to turn into a json string.
$something = array("test" => array("value", "another value", 4));
echo json_encode($something)
This will produce
{"test":["value","another value",4]}
Also, putting that string into $something = json_decode("{"test":["value","another value",4]}"); will produce back the same array that was passed into json_encode.
Note that JSON is not a programming language; it is a way to represent objects. http://json.org has a complete visual representation of how to use it. JSON's main components are Arrays (surrounded by []) and Objects (surrounded by {}). Arrays are lists of comma separated values (see json.org for how to tell it the types...its pretty simple) while objects are key:value pairs separated by commas between each pair where they key is a string surrounded by quotation marks. Above I created an Object with a key called "test" whose value was an Array with two strings and a number in it.
Use json_encode() for encoding the array, get the array back by using json_decode().

Problem inserting string into array

i'm trying to insert an implode generated string to an array that then later be used for json implementation
the implode generated string is look like this
'id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3]
i would like to used it in this code
$this->_JsonArr[]=array($Generated string);
to achieve something like this
$this->_JsonArr[]=array('id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3]);
instead i got something like this
$this->_JsonArr[]=array(" 'id' => $this->_SqlResult[0],'UserId' => $this->_SqlResult[1],'Msg' => $this->_SqlResult[2],'MsgStamp' => $this->_SqlResult[3]");
seem like generated string is treated as one element as key and value pair.
obviously i can get expected output from mysql because of this, can anybody help me with this
Why do you need to implode anything? Just pass the array:
$this->_JsonArr[] = your-non-imploded-array-here;
I think a full solution to what you want to do is something like this (i.e., the third code box in your question):
$row = array(
'id' => $this->_SqlResult[0],
'UserId' => $this->_SqlResult[1],
'Msg' => $this->_SqlResult[2],
'MsgStamp' => $this->_SqlResult[3]
);
$this->_JsonArr[] = $row;
$this->_JsonArr[]=array($Generated
string);
Looks like you want use arrays keys and values, but as I see you put into array plain string with expectation that array parse your plain string in format: keys => values.
You can try create array like below:
$this->_JsonArr[ $Generated_key ] = array( $Generated_value );
(Please correct me if I wrong understand your question).

Categories