i have data in my mysql database "Service,House Worker" (without ""), when i access my data from the database i found (Service,House Worker) as it is, when i try to convert with (var_dump(explode(',',($CheckBoxAnswer)));) it then its return following :
array(1) { [0]=> string(26) "Service,House Worker" }
but i want something similar:
array(1)
(
[0] => string(7) "Service"
[1] => string(13) "House Worker"
)
$CheckBoxAnswer is contain data i pulled from mysql.
i tried with var_dump(explode(',',($CheckBoxAnswer)));
but its not working
Try this:
var_dump(explode(',',($CheckBoxAnswer[0])));
You are tring to explode an array into an another array. You need to specify the string instead.
First you need to trim $CheckBoxAnswer
Because by using var_dump giving string length 26 instead of 20
Then after use string replace
str_replace('/','',$CheckBoxAnswer);
After that try to use var_dump(explode(',',($CheckBoxAnswer)));
Related
I'm studying php and I'm trying to figure out how to get the string "Juve_Milan" from this var_dump($_POST) :
array(5) {
["Juve_Milan"] => array(2) {
[0] => string(1)
"5" [1] => string(1)
"1"
}["Inter_Roma"] => array(2) {
[0] => string(1)
"4" [1] => string(1)
"4"
}["Napoli_Lazio"] => array(2) {
[0] => string(1)
"2" [1] => string(1)
"5"
}["name"] => string(0)
"" ["submit"] => string(5)
"Invia"
}
I could get all of them with:
foreach ($_POST as $param_name => $param_val) {
echo "<tr><td>".$param_name."</td><td>".$param_val[0]."-".$param_val[1]."</td></tr>";
}
But i want to get them exactly one by one, for example, if i want to get the string "Juve_Milan" or "inter_Roma" how can i do?
Without looping, how can i get the string value: "Juve_milan" or "Inter_Roma"? Becouse with the loop i can access them this way : $_POST as $param_name => $param_val
But i want to get them without loop, my first attempt was something like $_POST[0][0] but its wrong...
There are numbers of php array functions you can use.
You can use
array shift
to remove an element from an array and display it .e.g
$club = ['juve_millan', 'inter_roma', 'napoli_lazio'];
$juve = array_shift($club);
echo $juve;// 'juve_millan'
but note that the array is shortened by one element i.e. 'juve_millan' is no more in the array and also note that using
array_shift
over larger array is fairly slow
Array Slice function
PHP
array_slice()
function is used to extract a slice of an array e.g
$club = ['juve_millan', 'inter_roma', 'napoli_lazio'];
if I want to display
inter_roma
or assigned it to a variable, then I can do this
$roma = array_slice($club, 1, 1);// The first parameter is the array to slice, the second parameter is where to begin(which is one since most programming language array index start from 0 meaning juve_millan is zero, while inter_roma is 1, napoli_lazio is 2) and the length is 1 since i want to return a single element.
I hope you understand
You could iterate over keys of the main array like this:
foreach($_POST as $param_name => $param_val) {
echo "<tr><td>".$param_name."</td></tr>";
}
This will return each Juve_Milan, Inter_Roma etc. one by one. Although it's part of the code you already have, I believe this will return values you want only.
I am getting from url value something like below:
$param = "{year:2019,month:5,id:3}"
I am not being able to convert it to array.
Can anybody please help me
Any time you have a string like this it is possible that values can cause problems due to containing values you don't expect (like commas or colons). So to just add to the confusion and because it was just an interesting experiment I came up with the idea of translating the string to a URL encoded string (with & and =) and then parsing it as though it was a parameter string...
parse_str(strtr($param, ["{" => "", "}" => "", ":" => "=", "," => "&"]), $params);
gives the $params array as...
Array
(
[year] => 2019
[month] => 5
[id] => 3
)
I think that needs to be parsed manually.
First explode on comma without {} to separate each of the key value pairs then loop and explode again to separate key from values.
$param = explode(",", substr($param, 1, -1));
foreach($param as $v){
$temp = explode(":", $v);
$res[$temp[0]] = $temp[1];
}
var_dump($res);
/*
array(3) {
["year"]=>
string(4) "2019"
["month"]=>
string(1) "5"
["id"]=>
string(1) "3"
}*/
https://3v4l.org/naIJE
Your json isn't well formatted. Try this:
$param = '{"year":2019,"month":5,"id":3}';
var_dump(json_decode($param, true));
I have two arrays, or so I thought. One passes a Javascript object to php via a POST, the other gets data from a JS API which returns another object. I want to join both of these arrays. Here's the sample data and how it is obtained.
Name array which I get from API:-
array(5) { [0]=> string(1) "D" [1]=> string(1) "a" [2]=> string(1) "v" [3]=> string(1) "i" [4]=> string(1) "d" }
User array which is POST'd to my php script:-
array (
'userID' => '12345',
'time' => 'Monday 26th June 2017 22:12:37 AM',
)
Now I use the following to (try) and get both of these pieces of data into the same array to log to a file.
$nameoriginal = file_get_contents("/api");
$namejson = json_decode($name);
$user = var_export($_POST, true);
$detailstolog = $namejson + $user;
file_put_contents('/logs/names.log', $detailstolog);
However, I get a php error which states the first argument is not a valid array (i.e. $name is not valid). Why is this? What can I do to 'make' it an array?
I think the name 'array' is actually a string, hence the error. How do I make this an array, and is $array1 + $array2 the best way to do this?
I am trying to create something like:-
array (
'name' => 'david',
'userID' => '12345',
'time' => 'Monday 26th June 2017 22:12:37 AM',
)
Thanks for any help you can provide!
Copy the $_POST array to a new variable, then add the name to it.
$detailstolog = $_POST;
$name = implode('', $namejson);
$detailstolog['name'] = $name;
Sorry, but I don't understand where is JSON involved here, you're dealing with arrays, hence no need to decode anything from JSON.
The first array seems to be the word "David" split into chars. Then you just need to implode it and add it to your detailstolog array as the first element:
$detailstolog['name'] = implode("", $nameoriginal); // David
$detailstolog += $_POST;
I have this code for an array, and the problem is that it shows only two last items of the array.
$svi= array(
$WMN1,
$LNG1,
$SSWN1,
$DT1,
$OET1,
$OW1,
$NT1
);
$imena_sajtova = array(
'Prvi WMN',
'Drugi LNG',
'Treci SSWN',
'Cetvrti DT',
'Peti OET',
'Sesti OW',
'Sedmi NT'
);
$novi_svi =array_combine($svi, $imena_sajtova);
echo '<pre>'; print_r($novi_svi); echo '</pre>';
And the result of this code is:
Array
(
[2] => Sedmi NT
[1] => Sesti OW
)
What could be a problem? Thanks!
var_dump($svi) displays this:
array(7) { [0]=> int(2) [1]=> int(1) [2]=> int(1) [3]=> int(1) [4]=> int(2) [5]=> int(1) [6]=> int(2) }
This $svi array has results from some functions:
$WMN1=RSS_Brojac($WMN);
$LNG1=RSS_Brojac($LNG);
$SSWN1=RSS_Brojac($SSWN);
$DT1=RSS_Brojac($DT);
$OET1=RSS_Brojac($OET);
$OW1=RSS_Brojac($OW);
$NT1=RSS_Brojac($NT);
I've changed order of arrays in the array_combine, and it works!
$novi_svi =array_combine($imena_sajtova, $svi);
arsort($novi_svi);
echo '<pre>'; print_r($novi_svi); echo '</pre>';
And the output of this code is:
Array
(
[Prvi WMN] => 2
[Sedmi NT] => 2
[Peti OET] => 2
[Sesti OW] => 1
[Cetvrti DT] => 1
[Drugi LNG] => 1
[Treci SSWN] => 1
)
I'have sorted them from high to low, that's what I actually wanted to do with this array.
Now, how to separate the results, to have them in separate divs inside html?
So then, I'll be able to change their style and create some kind of table with these results.
The results are output from some RSS feeds, which I use to count number of published news.
Thank you for your time!
Well there you go, the exact reason why you only retrieve two values after the array combine is because you've only supplied two unique integers, 1,2 as the indexes of the arrays to be combined. You need at least 7 unique values for your 7 strings.
Here's an example using your code and it doesn't work
And...Here's an example using 7 unique indexes, that does work
Typecast the values in the first array before merge. Can be achieved with array_map(). I assume you know the desired types in the variables.
I would like to push an associate array into another array but I an not sure how to go about it. At the minute I have the following:
$rate_info = array(
"hotel_rating" => $hotel->{'hotelRating'},
"room_rate" => $hotel->{'RoomRateDetailsList'}->{'RoomRateDetails'}->{'RateInfo'}->{'ChargeableRateInfo'}->{'#total'},
"currency" => $hotel->{'RoomRateDetailsList'}->{'RoomRateDetails'}->{'RateInfo'}->{'ChargeableRateInfo'}->{'#currencyCode'},
"deep_link" => $hotel->{'deepLink'}
);
array_push($hotel_array[$hotel->{'name'}]["offers"], "expedia" => $rate_info );
"Offers" is an array , all I want to do is add an key value with an array within in. Any ideas? All I seem to be getting at the minute is parse errors.
UPDATE
This is the output of the array so far
["offers"]=>
array(2) {
["LateRooms"]=>
array(4) {
["hotel_rating"]=>
int(4)
["room_rate"]=>
string(6) "225.06"
["currency"]=>
string(3) "USD"
}
[0]=>
string(4) "test"
}
As you can see instad of [0] I would like ["site"]=>array()
Thanks
Oliver
I'd do this for the array assignment:
$hotel_array[$hotel->name]['offers']['expedia'] = $rate_info;
Ensure your warnings are enabled, so you know arrays (and subarrays) have been set up before you use them.
Did you first do this?
$hotel_array[$hotel->{'name'}] = array();
And then you can do:
array_push($hotel_array[$hotel->{'name'}]["offers"], "expedia" => $rate_info );