This question already has answers here:
Generate json string from multidimensional array data [duplicate]
(5 answers)
Closed 9 months ago.
I need help in how to remove the last comma from this looped var, please.
$image_meta .= "{\"id\":\"".$img_id."\",\"client\":\"".$img_desc."\",\"desc\":\"Test\"},";
FireFox doesn't seem to mind it, but IE does.
If there is any way to even get rid of the .= and loop my data in another way, i would be most thankful.
We would need to see the rest of the script.
But from what I understand of your situation, when you echo $image_meta (after the loop I suppose) you could do one of the two:
echo rtrim($image_meta,',');
or
echo substr($image_meta,0,-1);
You can do like this:
$ar_image_meta = array();
for/foreach() // your loop
{
$ar_image_meta[] = '{"id":"'.$img_id.'","client":"'.$img_desc.'","desc":"Test"}';
}
$image_meta = implode(", ", $ar_image_meta);
If your goal is to "convert" a PHP variable to a Javascript one, have a look at json_encode().
look at implode:
http://php.net/manual/en/function.implode.php
Just to clarify, I'm guessing your doing something similar to this:
$image_meta = '';
foreach($blahs as $blah){
$image_meta .= "{\"id\":\"".$img_id."\",\"client\":\"".$img_desc."\",\"desc\":\"Test\"},";
}
Something like this should work:
$image_meta_arr = array();
foreach($blahs as $blah){
array_push($image_meta, "{\"id\":\"".$img_id."\",\"client\":\"".$img_desc."\",\"desc\":\"Test\"}";
}
$image_meta = implode(',', $image_meta_arr);
Related
This question already has answers here:
How to parse a CSV file using PHP [duplicate]
(6 answers)
Closed 2 years ago.
I am currently working on php page creating, and there is one requirement that I need to work on but I am stuck.
There is accounts.txt which has n rows of : date,time,id,status and 4 columns (date,time,id,status).
And basically, I need to split them by "\n" and then "," in order to make each a value for table with row and column equal to the # of adminlog.txt file.
'''
$data = file_get_contents($file_path.'/adminlog.txt');
$data = explode(",",$data);
$adminlog = array();
for ($i = 0; $i < sizeof($data); $i ++) {
array_push($adminlog,$data[$i]);
}
print($adminlog);
'''
Above was the code that I was trying to put in php in order to separate them by comma, but when I tried printing out $adminlog on my page, it would just show "Array". Is there actually way to separate the dataset neatly by "comma" and actually use the separated data and use it to create a table containing all information via loop?
Thanks so much in advance for y'all's enthusiasm and passion!
You can use print_r or var_dump or loop throught the array and print the values
$arr = array('Foo', 'Bar');
foreach($arr as $value) {
echo "$value\n";
}
This question already has answers here:
How to loop through an associative array and get the key?
(12 answers)
Closed 4 years ago.
i'm making a project in PHP and i have a problem. My problem is that i don't know how to get a variable's name in $_GET foreach loop.
My code looks like this:
foreach($_GET as $get) {
$v_name = //I want to be this variable the $get variable's name
}
So example: if someone make a post request like this: http://www.example.com/?something=example
In this case i want to get the "something".
Thanks everyone who will help me - Jumpak
You need to print the key of array $_GET
You should read basics of foreach loop.
foreach($_GET as $key=>$get) {
// ^^^ // You can get the index key from Array
//$v_name = //I want to be this variable the $get variable's name
echo $key;
}
This question already has answers here:
get a single value of array items in php
(3 answers)
Closed 7 years ago.
I am having a bit of trouble getting a single item shown/echoed in a foreach loop. What I mean I have an array (filled from database).
return array(
'FriendName' => $FriendName,
'FriendImage' => $FriendImage
);
What I want is to show only FriendName while in a loop.
I have tried and this is an example:
foreach($UserFriends as $item) {
echo $item['FriendName'];
}
But it shows NULL and not the data I expect.
No need for a loop if you just need one value, you can use this instead:
echo $UserFriends['FriendName']
Learn more about php Arrays
Its been better to use it directly instead of looping an array if its not multidimensional array you can simply get it using
$UserFriends['FriendName']
you can checked using if condition like
foreach($UserFriends as $key => $item) {
if($key == 'FriendName'){
echo $item;
}
}
your array is not multidimensional so you can access vlaues like that.
or else you can get a value directly like
echo $userFriends['FriendName'];
I hope this is working for you
This question already has answers here:
How to modify an array's values by a foreach loop?
(2 answers)
Closed 8 years ago.
This is driving me nuts. I've searched for solutions but can't figure out what's wrong.
The situation... I have an SQL query running in WordPress, as follows:
SELECT distinct guid
FROM $wpdb->posts
WHERE post_status = 'inherit'
AND guid is not null
Dead simple. It returns a single column which contains a list of all the attachment media files currently stored on the system (well, not all of them, but it'll do as an explanation).
In a WP plugin function, I run the query:
$media_library_files = $wpdb->get_col($get_all_media,0);
That returns an array (I don't want an object) with values like:
[0] => http://mysitename.com/wp-content/uploads/2013/05/thumb_littlefile_blah.jpg
Then I want to process each one so that there's just the filename left. The problem is that, when I run a str_replace or pretty much any other string function on the contents, it doesn't work. For example:
$horrid_bit = 'http://mysitename.com/wp-content/uploads/2013/05/';
foreach($media_library_files as $item) {
$item = str_replace($horrid_bit,'',$item);
}
When I print_r the array after that, there's no visible change - every line is exactly the same as it was before.
I've tried using a (string) to cast $item, I've tried defining variables to do that, then working the str_replace on them, I've tried... loads of different things.
I have a feeling I'm missing something really simple, but I just can't see it. Is it because the column is varchar in the original table? Or something else?
Any help appreciated. Thanks!
PHP is not my 'native languague', but it seems like you're not modifying the values in the array. Did you try to put the modified string items (=minus the path) in a new array and use that one?
$horrid_bit = 'http://mysitename.com/wp-content/uploads/2013/05/';
$new_array = array();
foreach($media_library_files as $item) {
$new_item = str_replace($horrid_bit,'',$item);
$new_array.push($new_item);
}
//...use the items in the $new_array
Also, you might wanna just read the whole path string as an array (split on '/')and take the last element to get to the file.
$horrid_bit = 'http://mysitename.com/wp-content/uploads/2013/05/';
foreach($media_library_files as $key => $value) {
$media_library_files[$key] = str_replace($horrid_bit,'', $value);
}
But as Michael pointed out if you are not doing it this way for a specific purpose using basename would be better here, so you don't have to worry about the folder changing each month/year.
foreach($media_library_files as $key => $value) {
$media_library_files[$key] = basename($value);
}
You are not actually changing the value of $item in your array, you are instead creating a new variable, which is then overwritten every time your loop iterates.
It's also a good idea to check that the array is not empty before attempting a foreach() loop on it.
Finally, I've replaced str_replace() with basename() (as suggested in the comments under your question by #MichaelBerkowski).
if(!empty($media_library_files)) : foreach($media_library_files as $item) :
$media_library_files[$item] = basename($item);
endforeach;
endif;
str_replace() can handle arrays. No need for your foreach loop.
$horrid_bit = 'http://mysitename.com/wp-content/uploads/2013/05/';
$media_library_files = str_replace( $horrid_bit,'',$media_library_files );
On the other hand, since it seems you want to remove the path and get only the filename:
foreach($media_library_files as $key => $item) {
$media_library_files[$key] = basename($item);
}
This will change all entries to the filename only. The way you have it now you will have to change the $horrid_bit every month since the uploads are organized in year/month/ folders.
Hello My question is that i will have a textarea which i want to paste the following
[{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}},{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}}]
is there any way i can run a loop which allows me to manipulate the data individually so i can update this to the database. so the first time i run the loop is get this:
{"1173627548":{"methv":"dont know","q1":"-","q2":"-","q3":"U","q4":"-","comm":""}}
if possible i want to strip out the {'s and :'s and "s so i am left with the commas and the data. For example:
1173627548,methv,dont know.. etc
sorry if i am unclear with my question in any way.
$myhugejson = 'blah...';
$array = json_decode($myhugejson, true);
foreach($array as $sub) {
print_r($sub);
echo $thing = $sub[0]['methv'];
echo $second = $sub[0]['q1'];
}
In each iteration, $sub should contain your list.
All of this is assuming you're using JSON, which I think that is.
To reference i