This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JSON Parsing with PHP
I'm hoping for a little assistance on this. I trying to take what's in my $data variable:
[{"Product":"Internal",
"Rank":"1",
"Number":"1234"},
{"Product":"External",
"Rank":"1",
"Number":"5678"}]
and turn it into an associative array that is something like this:
Product[0] => Internal,
Rank[0] => 1,
Number[0] => 1234,
Product[1] => External,
Rank[1] => 1,
Number[1] => 5678
The closest I've been able to get is using the following code:
$del='/[{: ,}]/';
$data = preg_split($del,$data);
print_r($data);
Which gives me something like this:
Array ( [0] => [ [1] => "Product" [2] => "1 [3] => 1" [4] => "Rank"
[5] => "1"
Any suggestions would be greatly appreciated. Thanks for your time!
If you know that $data is in JSON format, you could just do this:
$data = json_decode($data);
Then you want to iterate through the array of objects and transmogrify them:
$output = array();
foreach($data as $index=>$object) {
foreach($object as $name=>$value) {
$output[$name][$index] = $value;
}
}
Firstly, use json_decode($data,TRUE), this will make your life easier, giving you nice array to transform in what you want.
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
So I got this array with associative object in PHP and I couldnot figure out how to get specific element
here is an array:
extra_fields => [
{"id":"1","value":"1055"},
{"id":"2","value":"Link"},
{"id":"3","value":"Name"}
]
I tried like this but it doesn't work
extra_fields[0]["value"]) and extra_fields[0]->value
Please help.
UPDATE:
full output code:
stdClass Object
(
[id] => 723
[title] => XXXXXXX
[alias] => XXXXXXX
[catid] => 50
[published] => 1
[introtext] =>
[fulltext] =>
[video] =>
[gallery] =>
[extra_fields] => [
{"id":"1","value":"1055"},
{"id":"2","value":"Link"},
{"id":"3","value":"Name"}
]
)
this is an $item coming out of Joomla CMS K2 plugin when I use print_r() command
I can access normal stuff like this $item->title and get XXXXXXX for my value, but could not figure out how to get items from extra_fields
as others mentioned, this doesn't seem valid PHP data, however if you would convert array of objects to multidimensional array, then do it this way:-
<?php
// new var that holds the arrays
$multiArray = [];
foreach ($extra_fields as $field){
$multiArray[] = (array) $field;
}
// now this var has the exact data that you want...
print_r($mulitArray);
Hope this helps.
The solution to my problem was pretty simple, I used json_decode() function to convert it too array
$someArray = json_decode($item->extra_fields, true);
print_r($someArray[0]["value"]);
Thank you all for your help
new to the stackoverflow community, so do be forgiving if I've done anything incorrect. As I'm an amateur in coding in php, I'm currently facing a difficulty in multi dimensional arrays.
Currently, I have this array being returned to me.
Array(
[trialID] => 1
[trialMixedArray] => 1,2,3,4,5,6
[trialStatus] => active
[trialAddedDate] => 2017-11-13 09:56:03
)
How do I split the trialMixedArray and return it to the original array so that it becomes formatted to be like the following result:
Array(
[trialID] => 1
[trialMixedArray] => Array(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[trialStatus] => active
[trialAddedDate] => 2017-11-13 09:56:03
)
Thanks for the help in advance! Cheers! :)
Use explode function, which splits string to array. Manual.
$results['trialMixedArray'] = explode(',', $results['trialMixedArray']);
you can try something like this
$finalArray = [];
fetchItems($array_variable) {
foreach($array_variable as $item){
if(is_array($item) {
fetchItems($item);
} else {
$finalArray[] = $item;
}
}
}
fetchItems($originaArray);
Use explode function to split any string based on a key.
The syntax is explode('key',string);
I am quite new with PHP and I am trying to read something form an API.
At the moment I use
$homepage = file_get_contents('http://www.site.com');
echo $homepage
This returns something which looks like this:
Array
(
[0] => Array
(
[name] => myname
[user_id] => 31232
)
[1] => Array
(
[name] => anothername
[user_id] => 23534
)
)
So here is what I want: I only want to read the [name] => x and leave the rest, so I tried a for loop within str_replace, but all I got were errors.
I hope someone is able to help me
Edit:
I just saw I could set is as a json text too, it returns something like
[{"name":"myname","user_id":"31232"},{"name":"anothername","user_id":"23534"}]
Edit2: Thank you Tuga, that was exactly what I was searching for :) I can't upvote, since my reputaion is below 15, is there another way for me to show your answer helped?
$array = json_decode($homepage); will return an array, then you can loop the array containing objects and use 'name' attribute :
foreach ($array as $obj) echo $obj->name;
This question already has answers here:
PHP - Append elements to array
(3 answers)
Closed 8 years ago.
I'm trying to populate all the links in my database into an array with a key something like this:
$link = array(99999 => "http://link1.com", 111111 => "http://link2.com");
Which returns:
Array ( [99999] => http://link1.com [111111] => http://link2.com )
How can i get the array to look like that when going through a loop, Here is my code:
while($row = mysql_fetch_array($query)) {
$link[] = "$row[r_id] => $row[r_link]";
}
It returns:
Array ( [0] => 24004 => http://link1.com [1] => 30554 =>
http://link2.com
But i don't wan't it to be like this, How can i get it the same as the first example while going through a loop ?
while($row = mysql_fetch_array($query)) {
$link[$row['r_id']] = $row['r_link'];
}
$link[$row['r_id']] = $row['r_link'];
Ok so I have been struggling with this for hours now and I cannot seem to figure out what I'm trying to do. I have an array with many percent values placed inside and I'm printing out the first 5 of them. The $percent variables are acquired through similar_text
$array=array($percent12, $percent13, $percent14,
$percent15, $percent16, $percent17,
$percent18, $percent19, $percent110,
$percent111, $percent112, $percent113,
$percent114, $percent115, $percent116,
$percent117, $percent118, $percent119,
$percent120);
print_r(array_slice($array,0,5));
and it outputs like this:
Array ( [0] => 36.015505697169 [1] => 2.4181626187962 [2] => 2.4181626187962 [3] => 5.2153134902083 [4] => 100 )
So what i'm trying to figure out here is if it's possible to print the results of my array as they are listed above. example output would look like this:
Array ( [percent12] => 36.015505697169 [percent13] => 2.4181626187962 [percent14] => 2.4181626187962 [percent15] => 5.2153134902083 [percent16] => 100 )
i feel like this isn't possible, but if not, is there a way to assign the
[0]=> 36.015505697169 [1]=> 2.4181626187962
...etc to output something else say like:
[web0]=> 36.015505697169 [web1] => 2.4181626187962
Please help!! It's driving me crazy!!
You need to make it an associative array:
$array=array('percent12' => $percent12,
'percent13' => $percent13,
...);
I recommend using array_combine()
Basically you're just going to setup your new array with the keys, and pass in your current array for the values, thus creating a new array with the keys you want in the right place.
Try like
$myArr = array_slice($array,0,5);
$i = 0;
foreach($myArr as $key => $value) {
$newArr['web'.$i++] = $value;
}
print_r($newArr);