This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
I have result
{"success":true,"data":{"id":6583879,"listingId":"11745/3470/OMS"}}
I need to explode it on two arrays:
$id = 6583879;
and
$listid = 11745/3470/OMS
I'd like to avoid counting characters, this response may be another in future. I was thinking about taking:
everything between "id": and comma
everything between "listingId":" and "
PHP code demo
<?php
$json='{"success":true,"data":{"id":6583879,"listingId":"11745/3470/OMS"}}';
$array= json_decode($json,true);
extract($array["data"]);
$first=array("id"=>$id);
$second=array("listingId"=>$listingId);
print_r($first);
print_r($second);
Output:
Array
(
[id] => 6583879
)
Array
(
[listingId] => 11745/3470/OMS
)
just decode the json data
$data = '{"success":true,"data":[{"id":6583879,"listingId":"11745/3470/OMS"}]}';
$arr = json_decode($data);
$id = array();
$listing = array();
for($i=0; $i < count($arr->data); $i++){
$id[$i] = $arr->data[$i]->id;
$listing[$i] = $arr->data[$i]->listingId;
}
//loop array to view data
foreach($id as $value){
echo $value.'<br>';
}
?>
<?php
$json='{"success":true,"data":{"id":6583879,"listingId":"11745/3470/OMS"}}';
$decoded = json_decode($json);
$id = $decoded['data']['id'];
$listingId = $decoded['data']['listingId'];
Related
This question already has an answer here:
unserialize data from mysql
(1 answer)
Closed 7 months ago.
I want to break the line I pulled from the database and list the ip addresses
Sql Table export
a:3{i:0;s:13:"213.74.219.18";i:1;s:13:"321.32.321.32";i:2;s:14:"321.315.212.55";}
$set=mysqli_query($con,"SELECT * FROM simple_stats_options where option='ignored_ips'");
$value = mysqli_fetch_array($set,MYSQLI_ASSOC);
function arasinial($str,$birinci,$ikinci,$i) {
$bolum = explode ($birinci,$str);
$bolum = explode ($ikinci,$bolum[$i]);
return $bolum[0];
}
$array = explode('/', $var);
foreach ($array as $values)
{
}
$metin=$value["value"];
for ($x = 1; $x <= 10; $x++) {
echo arasinial($metin,':"','";',$x)."<br>";
}
Your data is serialized data, so you can just use unserialize():
<?php
$r = 'a:3:{i:0;s:13:"213.74.219.18";i:1;s:13:"321.32.321.32";i:2;s:14:"321.315.212.55";}';
print_r(unserialize($r));
will output
Array
(
[0] => 213.74.219.18
[1] => 321.32.321.32
[2] => 321.315.212.55
)
This question already has answers here:
PHP - Convert multidimensional array to 2D array with dot notation keys
(5 answers)
Closed 2 years ago.
I have a little problem. I want to transform this:
$array['page']['article']['header'] = "Header";
$array['page']['article']['body'] = "Body";
$array['page']['article']['footer'] = "Footer";
$array['page']['news']['header'] = "Header";
$array['page']['news']['body'] = "Body";
$array['page']['news']['footer'] = "Footer";
Into this:
$array['page.article.header'] = "Header";
$array['page.article.body'] = "Body";
$array['page.article.footer'] = "Footer";
$array['page.news.header'] = "Header";
$array['page.news.body'] = "Body";
$array['page.news.footer'] = "Footer";
The number of dimensions is variable and can be times 0 or 10. I don't know if I used the right search term, but Google could not help me so far.
So if someone has a solution for me.
Thanks
You can loop over the array at hand. Now, if the current value is an array, recursively call that array to the function call. On each function call, return an array with key value pairs. When you get the output from your sub-array, attach current key value to all keys of that sub-array returned output.
Snippet:
<?php
function rearrange($array){
$output = [];
foreach($array as $key => $val){
if(is_array($val)){
$out = rearrange($val);
foreach($out as $sub_key => $sub_val){
$output[$key . "." . $sub_key] = $sub_val;
}
}else{
$output[$key] = $val;
}
}
return $output;
}
print_r(rearrange($array));
Demo: https://3v4l.org/40hjK
This question already has answers here:
Finding the subsets of an array in PHP
(5 answers)
Closed 7 years ago.
I will do my best to explain this idea to you. I have an array of values, i would like to know if there is a way to create another array of combined values. So, for example:
If i have this code:
array('ec','mp','ba');
I would like to be able to output something like this:
'ec,mp', 'ec,ba', 'mp,ba', 'ec,mp,ba'
Is this possible? Ideally i don't want to have duplicate entries like 'ec,mp' and 'mp,ec' though, as they would be the same thing
You can take an arbitrary decision to always put the "lower" string first. Once you made this decision, it's just a straight-up nested loop:
$arr = array('ec','mp','ba');
$result = array();
foreach ($arr as $s1) {
foreach ($arr as $s2) {
if ($s1 < $s2) {
$result[] = array($s1, $s2);
}
}
}
You can do it as follows:
$arr = array('ec','mp','ba', 'ds', 'sd', 'ad');
$newArr = array();
foreach($arr as $key=>$val) {
if($key % 2 == 0) {
$newArr[] = $val;
} else {
$newArr[floor($key/2)] = $newArr[floor($key/2)] . ',' . $val;
}
}
print_r($newArr);
And the result is:
Array
(
[0] => ec,mp
[1] => ba,ds
[2] => sd,ad
)
Have you looked at the function implode
<?php
$array = array('ec','mp','ba');
$comma_separated = implode(",", $array);
echo $comma_separated; // ec,mp,ba
?>
You could use this as a base for your program and what you are trying to achieve.
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 9 months ago.
I have a multidimensional array, and I need to sort that array by a specific key in that array.
I add to the array like this in a for loop
$myArr[$i][0] = $row[1];
$myArr[$i][1] = $row[2];
$myArr[$i][2] = $row[3];
Now lets say that the value of $row[3] is DATE_ATOM.
How can i arrange the completed array by $myArr[$i][2]?
Thanks!
What you are probably looking for is array_multisort(), specifically this example usage (Sorting database results).
For example (based on your code above):
$i = 0;
$myArr = $col1 = $col2 = $col3 = array();
foreach ($rows as $row) {
$myArr[$i][0] = $col1[$i] = $row[1];
$myArr[$i][1] = $col2[$i] = $row[2];
$myArr[$i][2] = $col3[$i] = $row[3];
$i++;
}
array_multisort($col3, SORT_ASC, $myArr);
var_dump($myArr);
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php - get numeric index of associative array
$array = ('a'=>'a', 'b'=>'b');
foreach($array as $key => $value ){
//echo $position ( 1,2 )
}
Can I get the position in the array with a simple function ?
Try:
$i = 0;
$array = ('a'=>'a', 'b'=>'b');
foreach($array as $key => $value ){
$i++;
echo $i;
}
$array = array('a'=>'a', 'b'=>'b');
for ($x = 0; $x < count($array);$x++)
echo $x."<br >";