I have to extract strings from a xml file. One particular value has been generated using json enconding.
Here is a exemple of what I can find:
<plus_details>
[["Neuf"],["Petite copropri\u00e9t\u00e9"],["Vue mer"]]
</plus_details>
I would like to extract the strings and display them inline and separated by commas, like this :
Neuf, Petite copropriété, Vue mer
I tried using json_decode function, but the only thing I can display is:
array(3) {
[0]=>
array(1) {
[0]=>
string(4) “Neuf”
}
[1]=>
array(1) {
[0]=>
string(20) “Petite copropriété”
}
[2]=>
array(1) {
[0]=>
string(7) “Vue mer”
}
}
Any help would be appreciated. Thanks.
Simple use a loop to go through your data. When you json_decode the string you provided us, you will end-up with an array like this :
Array
(
[0] => Array
(
[0] => Neuf
)
[1] => Array
(
[0] => Petite copropriété
)
[2] => Array
(
[0] => Vue mer
)
)
So in order to get your data you need to loop your array.
foreach(json_decode($json) as $data){
echo $data[0];
echo '<br>';
}
The output of the above code is:
Neuf
Petite copropriété
Vue mer
Related
Here's the idea: a user enters his ZIP code.
Based on the inserted ZIP code, I get an array of ZIP codes (distance ordered).
Next I want to order an existing array of ZIP codes based on the distance ordered array.
So basically I have two arrays:
Array which should be ordered
array(2) {
[0]=>
string(4) "2018"
[1]=>
string(4) "2500"
}
Distance ordered array
array(247) {
[0]=>
string(4) "2000"
[1]=>
string(4) "2500"
[2]=>
string(4) "2050"
[2]=>
string(4) "2018"
In this example, my array (number 1) should be ordered like so: [0] => 2500, [1] => 2018
How can I manage this?
You could use array_intersect() to get only the values of the second array that are also in the first array. And as the function preserves the keys - and so the order -, you only have to renumber them.
$a1=array(2018,2500);
$a2=array(2000,2500,2050,2018);
$a3=array_intersect( $a2 , $a1 );
echo print_r($a3,true);
Result:
Array (
[1] => 2500
[3] => 2018 )
Array ( [0] => UK [1] => France [2] => USA ) in these array get only values
like array(UK, France, USA) am trying like below,
$expression=array_values(array('0' => 'UK', '1' => 'France', '2' => 'USA'));
var_dump($expression);
OUTPUT PLAN:
array(3) { [0]=> string(2) "UK" [1]=> string(6) "France" [2]=> string(3) "USA" }
Can i get my desired output?
Please read answer carefully.
$arr = array('UK', 'France', 'USA'); // It has 0 ,1 ,2 keys but you cannot see in the code`
But in browser you can see it.
array(3) { [0]=> string(2) "UK" [1]=> string(6) "France" [2]=> string(3) "USA" }
Just use loop to print each country
foreach($arr as $country){
echo $country."<br>";
}
You can understand it by below loop
foreach($arr as $k=>$country){
echo "$k => $country"."<br>"; // Here $k is key like 0,1,2..
}
If you have array like below:-
$arr = array('uk'=>'UK', 'france'=>'France', 'usa'=>'USA') // It has uk ,france ,usa keys
now array_values($arr) will give you output as below
array(3) { [0]=> string(2) "UK" [1]=> string(6) "France" [2]=> string(3) "USA" }
It will remove all keys and regenerate index of key from 0.
Refer below links to understand PHP array:-
Link1
Link2
Link3
Hope it will help you :)
You could just do:
$out = array();
foreach($old_array as $new_value) { //Where $old_array is the array you want to convert
array_push($out, $new_value);
}
All PHP arrays have an internal index whether you attach one or not. So in your example even if you created an array using $countries = array("USA", "UK", "France") it would still output with indexes.
You can however ignore the indexes when your looping through it and only work with the values using a foreach() loop.
An example of such a loop would be...
foreach($expression as $index => $country) {
echo($country . "<br />");
};
The above example will print each country on its own line. You can adapt it to suit whatever looping you need.
As a side note be aware that the index will commence from 0 so item 1 in the array will have an index of 0 and increment from there.
You have this array.
Array ( [0] => UK [1] => France [2] => USA )
you have key and value assigned to it.
if you make another array like
array('UK','France','USA')
It is an associative array. Its the same as above.
Read it for more info
If you want to do operation with the array you can use foreach and for or other array related functions. So, first of all say what are you trying to accomplish with this one.
$expression=array_values(array('0' => 'UK', '1' => 'France', '2' => 'USA'));
foreach($expression as $val) {
echo $val.",";
}
I am getting a Json respond with :
$response = curl_exec($rest);
$json = json_decode($response, true);
I manage to get its values(strings) with :
$foundUserId=$json['results'][0]['userId'];
$foundName=$json['results'][0]['name'];
$foundPhoneNum=$json['results'][0]['phoneNumber'];
But the last value- phoneNumber, is array of strings .
If i try then to loop over it i get nothing(although the array is there in the Json)
foreach ($foundPhoneNum as &$value)
{
print_r($value);
}
What am i doing wrong ?
EDIT :
The json:
Array ( [results] => Array ( [0] => Array ( [action] => message [createdAt] => 2015-11-21T09:36:33.620Z [deviceId] => E18DDFEC-C3C9 [name] => me [objectId] => klMchCkIDi [phoneNumber] => ["xx665542","xxx9446"] [state] => 1 [updatedAt] => 2015-11-22T08:24:46.948Z [userId] => 433011AC-228A-4931-8700-4D050FA18FC1 ) ) )
You might have json as a string inside json. That's why after json_decode() you still have json inside phoneNumber. You have 2 options:
Decode phoneNumber like
$foundPhoneNum=json_decode($json['results'][0]['phoneNumber']);
Build proper initial json. Instead of
{"phoneNumber": "[\"xx665542\",\"xxx9446\"]"}
should be
{"phoneNumber": ["xx665542","xxx9446"]}
There's a couple of ways to debug situations like this as mentioned in the comments; print_r() and var_dump().
var_dump(), although harder to read the first few times, is my favourite because it tells you the data types of each value in the array. This will confirm whether or not the expected string is indeed an array.
An example from the var_dump() documentation:
<?php
$a = array(1, 2, array("a", "b", "c"));
var_dump($a);
And the output is;
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
array(3) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
}
}
As you can see it shows array, int and string as the data types.
You might also like to install the Xdebug extension for PHP which dumps more useful error messages and tracebacks. Again harder to read the first few times, but well worth it!
foreach ($foundPhoneNum as $value)
{
print_r($value);
}
There was an extra & before $value. Try this.
I have a url parameter named data that contains a comma separated string with some enclosed in double quotes like this:
localhost/index.php?data=val1,val2,val3,"val4","val5",val6
I am trying to parse the string and put it into an array. Using str_getcsv($_GET['data'],',','"'); gives me the output like this:
Array
(
[0] => val1
[1] => val2
[2] => val3
[3] =>
)
I would like the array to look like this:
Array
(
[0] => val1
[1] => val2
[2] => val3
[3] => val4
[4] => val5
[5] => val6
)
Thanks in advance!
I would say urlencode the double quotes when generating that url. Because link will result in the url you go to only being localhost/index.php?data=val1,val2,val3,
So like:
echo 'link';
Have you tried using explode? It'll separate a string into an array using whatever separator you specify.
Using your example,
$_GET['data'] = 'val1,val2,val3,"val4","val5",val6';
$testarr = explode(",", $_GET['data']);
var_dump($testarr);
Outputs:
array(6) {
[0]=>
string(4) "val1"
[1]=>
string(4) "val2"
[2]=>
string(4) "val3"
[3]=>
string(6) ""val4""
[4]=>
string(6) ""val5""
[5]=>
string(4) "val6"
}
Looking at your question again, it seems you might want to remove the " from $_GET['data'] entirely?. If so, do this:
$testarr = explode(",", str_replace('"','',$_GET['data']));
Hello i have this string which is an serial of code/references for things:
<?PHP
$list="1010<1>;1020<?>;3010<?>"; the list of items code
$id="5060<?>"; //will add this
$list_unique=explode(";", $list);
print_r($list_unique);
?>
Now the output is: Array ( [0] => 1010<1> [1] => 1020 [2] => 3010 )
Why? it forgots the part why? it should be
Array ( [0] => 1010<1> [1] => 1020<?> [2] => 3010<?> )
You're probably viewing the output as rendered HTML. View source and you'll see it isn't missing.
Alternatively, escape your output when you inspect it.
echo htmlspecialchars( print_r($list_unique, 1 ) );
Always var_dump($yourvar); instead of print_r($yourvar); if you are unsure of your results.
var_dump($list_unique); gave me this
array(3) {
[0]=>
string(7) "1010<1>"
[1]=>
string(7) "1020<?>"
[2]=>
string(7) "3010<?>"
}