Wordpress Parsing Array in PHP - php

How can I parse this in PHP:
a:8:{s:3:"key";s:19:"field_501743d4baa78";s:5:"label";s:8:"Category";s:4:"name";s:8:"category";s:4:"type";s:8:"checkbox";s:12:"instructions";s:0:"";s:8:"required";s:1:"1";s:7:"choices";a:17:{s:4:"Arts";s:4:"Arts";s:8:"Business";s:8:"Business";s:14:"Communications";s:14:"Communications";s:16:"Criminal Justice";s:16:"Criminal Justice";s:13:"Culinary Arts";s:13:"Culinary Arts";s:9:"Education";s:9:"Education";s:11:"Engineering";s:11:"Engineering";s:11:"Health Care";s:11:"Health Care";s:22:"Information Technology";s:22:"Information Technology";s:13:"International";s:13:"International";s:5:"Legal";s:5:"Legal";s:17:"Political Science";s:17:"Political Science";s:10:"Psychology";s:10:"Psychology";s:8:"Religion";s:8:"Religion";s:7:"Science";s:7:"Science";s:9:"Technical";s:9:"Technical";s:10:"Veterinary";s:10:"Veterinary";}s:8:"order_no";s:1:"3";}
to get a list of all the "categorys" such as Arts, Business, Communications etc etc
Thanks for your help
UPDATE
After running unserialize this is what i get:
array(8) { ["key"]=> string(19) "field_501743d4baa78" ["label"]=> string(8) "Category" ["name"]=> string(8) "category" ["type"]=> string(8) "checkbox" ["instructions"]=> string(0) "" ["required"]=> string(1) "1" ["choices"]=> array(17) { ["Arts"]=> string(4) "Arts" ["Business"]=> string(8) "Business" ["Communications"]=> string(14) "Communications" ["Criminal Justice"]=> string(16) "Criminal Justice" ["Culinary Arts"]=> string(13) "Culinary Arts" ["Education"]=> string(9) "Education" ["Engineering"]=> string(11) "Engineering" ["Health Care"]=> string(11) "Health Care" ["Information Technology"]=> string(22) "Information Technology" ["International"]=> string(13) "International" ["Legal"]=> string(5) "Legal" ["Political Science"]=> string(17) "Political Science" ["Psychology"]=> string(10) "Psychology" ["Religion"]=> string(8) "Religion" ["Science"]=> string(7) "Science" ["Technical"]=> string(9) "Technical" ["Veterinary"]=> string(10) "Veterinary" } ["order_no"]=> string(1) "3" }
however i'm not sure how to loop through and just get the category names - sorry - i'm new to PHP - probably just doing something stupid - thanks for your help

That's just a serialized array. Just unserialize it and getting the values is easy:
$array = unserialize(a:8:{s:3:"key";s:19:"field_501743d4baa78";s:5:"label";s:8:"Category";s:4:"name";s:8:"category";s:4:"type";s:8:"checkbox";s:12:"instructions";s:0:"";s:8:"required";s:1:"1";s:7:"choices";a:17:{s:4:"Arts";s:4:"Arts";s:8:"Business";s:8:"Business";s:14:"Communications";s:14:"Communications";s:16:"Criminal Justice";s:16:"Criminal Justice";s:13:"Culinary Arts";s:13:"Culinary Arts";s:9:"Education";s:9:"Education";s:11:"Engineering";s:11:"Engineering";s:11:"Health Care";s:11:"Health Care";s:22:"Information Technology";s:22:"Information Technology";s:13:"International";s:13:"International";s:5:"Legal";s:5:"Legal";s:17:"Political Science";s:17:"Political Science";s:10:"Psychology";s:10:"Psychology";s:8:"Religion";s:8:"Religion";s:7:"Science";s:7:"Science";s:9:"Technical";s:9:"Technical";s:10:"Veterinary";s:10:"Veterinary";}s:8:"order_no";s:1:"3";});
var_dump($array);

$sample_arr = unserialize($array); //unserialize here
$sample_arr = $sample_arr['choices']; //get the array
then you have the category list saved to $sample_arr.
foreach($sample_arr as $temp) {
//do stuff with each element here
}
Examples include outputting this as a html list
echo '<ul>';
foreach($sample_arr as $temp) {
echo '<li>' . $temp . '</li>';
}
echo '</ul>';

Related

output an Array with same values

I have a SQL Query result (array): "title", "content" and "name"
Here is my var_dump:
array(28) {
[0]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(43) "https://www.wrike.com/open.htm?id=440908999"
["name"]=>
string(14) "Christian Wahl"
}
[1]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(5) "MySQL"
["name"]=>
string(14) "Christian Wahl"
}
[2]=>
array(3) {
["title"]=>
string(4) "Test"
["content"]=>
string(3) "PHP"
["name"]=>
string(14) "Christian Wahl"
}
[3]=>
array(3) {
["title"]=>
string(4) "Test"
["content"]=>
string(3) "PHP"
["name"]=>
string(14) "Christian Wahl"
}
[4]=>
array(3) {
["title"]=>
string(10) "Basis Task"
["content"]=>
string(7) "content"
["name"]=>
string(9) "Elena Ott"
}
(I cut off the end of the array to make it a little clearer to see)
These are Tasks who are assigned to a User.
Now i want to output the "Name" (panel-heading)
and the "title" and "content" (panel-body).
It should look smth like this but for each given name:
how it should look like
I tried to find a solution on my own, but without success :(
I hope u can help me?
thx a lot
-Taddl
just loop on your data and render it
$data = [];
foreach ($databaseResult as $row) {
$data[$row['name']][] = $row;
}
foreach($data as $name => $stuff) {
echo $name . '<br>';
foreach($stuff as $row) {
echo $row["title"] . ':' . $row["content"] . '<BR>';
}
}
You can use foreach loop. As example:
foreach($yourarrayvariable as $data)
{
echo "<tr>";
echo "<td class='heading'>".$data['name']."</td>";
echo "<div class='content'>";
echo "<td>".$data['title']."</td>";
echo "<td>".$data['content']."</td>";
echo "</div>";
echo "</tr>";
}
And create your desired template look classes as per your need inside foreach.

Print an array value only once - but still print rest of array? [duplicate]

This question already has answers here:
How can i list has same id data with while loop in PHP? [closed]
(1 answer)
php get unique sub array [duplicate]
(1 answer)
Closed 4 years ago.
been using most of the day to solve my problem - and i am about to give up.
I have an array looking like this:
array(2) {
[1]=>
array(5) {
[0]=>
array(6) {
["name"]=>
string(15) "Henning smaall"
["drivernumber"]=>
string(4) "8830"
["period_start"]=>
string(10) "2018-07-20"
["period_end"]=>
string(10) "2018-07-31"
["status"]=>
string(1) "0"
["note"]=>
string(9) "Operation"
}
[1]=>
array(6) {
["name"]=>
string(15) "Henning smaall"
["drivernumber"]=>
string(4) "8830"
["period_start"]=>
string(10) "2018-07-17"
["period_end"]=>
string(10) "2018-07-17"
["status"]=>
string(1) "0"
["note"]=>
string(0) ""
}
[2]=>
array(6) {
["name"]=>
string(15) "Henning smaall"
["drivernumber"]=>
string(4) "8830"
["period_start"]=>
string(10) "2018-07-16"
["period_end"]=>
string(10) "2018-07-16"
["status"]=>
string(1) "1"
["note"]=>
string(0) ""
}
[3]=>
array(6) {
["name"]=>
string(15) "Henning smaall"
["drivernumber"]=>
string(4) "8830"
["period_start"]=>
string(10) "2018-07-27"
["period_end"]=>
string(10) "2018-07-27"
["status"]=>
string(1) "0"
["note"]=>
string(0) ""
}
[4]=>
array(6) {
["name"]=>
string(15) "Henning smaall"
["drivernumber"]=>
string(4) "8830"
["period_start"]=>
string(10) "2018-07-31"
["period_end"]=>
string(10) "2018-07-31"
["status"]=>
string(1) "1"
["note"]=>
string(0) ""
}
}
[13]=>
array(2) {
[0]=>
array(6) {
["name"]=>
string(15) "Henrik Hjersing"
["drivernumber"]=>
string(4) "8850"
["period_start"]=>
string(10) "2018-07-10"
["period_end"]=>
string(10) "2018-07-24"
["status"]=>
string(1) "0"
["note"]=>
string(0) ""
}
[1]=>
array(6) {
["name"]=>
string(15) "Henrik Hjersing"
["drivernumber"]=>
string(4) "8850"
["period_start"]=>
string(10) "2018-07-18"
["period_end"]=>
string(10) "2018-08-01"
["status"]=>
string(1) "0"
["note"]=>
string(11) "asdasdasdad"
}
}
}
The output i am looking for should be something like
Henning smaall (8830)
2018-07-20 - 2018-07-31
2018-07-17 - 2018-07-17
2018-07-16 - 2018-07-16
2018-07-27 - 2018-07-27
2018-07-31 - 2018-07-31
Henrik Hjersing (8850)
2018-07-10 - 2018-07-24
2018-07-18 - 2018-08-01
For now i am using dummy data and easy php testcode - but i keep getting the name reccuring in the output.
Database function:
public function resultsetGroup(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC);
}
SQL function:
public function getVacations(){
//$this->db->query('SELECT * FROM vacation ORDER BY user_id, status, period_start');
$this->db->query('SELECT u.id, u.name, u.drivernumber, v.period_start, v.period_end, v.status, v.note FROM users u, vacation v where u.id = v.user_id');
$results = $this->db->resultsetGroup();
return $results;
}
This is the php code i've been trying to alter in 500 different ways, and still i cannot get the result i am after - if i do not pdo group by u.id but u.name - it works, but then again, 2 people can have the same name - and that will break my intention.
<?php var_dump($data['vacations']); ?>
<?php foreach($data['vacations'] as $key => $vac) : ?>
<?php echo $key; ?>
<?php foreach($vac as $va) : ?>
<?php echo key($vac);?>
<h1><?php echo $va['name'] ?></h1>
<?php endforeach ; ?>
<?php endforeach ; ?>
Can you pls help me out? i feel like im running around in cirkles.
enter code here
Try this:
$vacations = array();
foreach($data['vacations'] as $key => $value){
foreach($value as $index => $person){
$vacations[$person['drivernumber']] = array();
$vacations[$person['drivernumber']]['name'] = $person['name'];
$vacations[$person['drivernumber']]['drivenumber'] = $person['drivenumber'];
$vacations[$person['drivernumber']]['vacations'] = "";
}
}// With this, you will have the array $vacations with the drivernumbers as keys without duplicates and initialized the name, drivenumber and vacations string.
foreach($data['vacations'] as $key => $value){
foreach($value as $index => $person){
$vacations[$person[drivenumber]]['vacations'] += $person['period_start'] . "-" . $person['period_end'] . " ";
}
}//This will concatenate all the period_start and period_end of each person in their respective position in the array.
foreach($vacations as $key => $value){
echo $value['name'] . " " . $value['drivernumber'] . " " . $value['vacations'];
}//Finally.. print in the format that you are requesting.
Please try.

How to loop through the following php array?

I want to loop through the following array.
array(2) {
[0]=> array(6) {
[0]=> string(10) "excel_id "
[1]=> string(12) "excel_name "
[2]=> string(11) "excel_email"
[3]=> string(13) "excel_address"
[4]=> string(12) "excel_giveby"
[5]=> string(16) "excel_accesories"
}
[1]=> array(6) {
[0]=> float(111)
[1]=> string(10) "sh1exname1"
[2]=> string(11) "sh1exemail1"
[3]=> string(12) "sh1exaddress"
[4]=> string(12) "sh1exgivenby"
[5]=> string(16) "sh1exaccesorries"
}
}
can anyone help me?
If you tried anything at all you should have found foreach.
this will loop through all of your array values.
You can simply loop through a multi-dimensional array by using a multi-dimensional foreach like this:
foreach($array as $parentValue)
{
foreach($parentValue as $childValue)
{
// your code
}
}
try this:
foreach ($main_array as $outer_entry){
foreach($outer_entry as $entity){
print_r($entity);
}
}

JSON data parsing cannot understand

array(2) {
["success"]=>
string(1) "1"
["return"]=>
array(125) {
[0]=>
array(11) {
["marketid"]=>
string(3) "141"
["label"]=>
string(6) "42/BTC"
["primary_currency_code"]=>
string(2) "42"
["primary_currency_name"]=>
string(6) "42Coin"
["secondary_currency_code"]=>
string(3) "BTC"
["secondary_currency_name"]=>
string(7) "BitCoin"
["current_volume"]=>
string(10) "0.11628537"
["last_trade"]=>
string(12) "223.00000000"
["high_trade"]=>
string(12) "256.88999999"
["low_trade"]=>
string(12) "205.00000000"
["created"]=>
string(19) "2014-01-12 19:35:49"
}
}
I was trying to process this in PHP
$result = api_query("getmarkets");
$json = json_decode($result);
var_dump($result);
I tried many times to start processing this data but how would I start this
I was thinking something like $json['return'][0]['marketid'] would grab the market id.
Looks like it is already php object, so no need to parse as json.
try...
$result['return'][0]['marketid']
... and delete ...
$json = json_decode($result);

Converting Json String into PHP array then using php array

Im new to json & php and I'm having some issues with json into php string
My json string looks like this
{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}
So Far i have created my string
$json_raw = '{"status":"OK","cards": [{"id":100001,"name": .....
Decoded the json
$arr = json_decode($json_raw, TRUE);
I var_dump($arr);
then it returns
array(2) { ["status"]=> string(2) "OK" ["cards"]=> array(4) { [0]=> array(8) { ["id"]=> int(100001) ["name"]=> string(6) "batman" ["image"]=> int(11111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T11:37:07Z" } [1]=> array(8) { ["id"]=> int(100002) ["name"]=> string(8) "superman" ["image"]=> int(111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:30:09Z" } [2]=> array(8) { ["id"]=> int(100003) ["name"]=> string(8) "catwoman" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-08-15T12:39:42Z" } [3]=> array(8) { ["id"]=> int(100004) ["name"]=> string(4) "bane" ["image"]=> int(1111111) ["size"]=> int(75) ["region_id"]=> int(1) ["locked"]=> bool(false) ["status"]=> string(6) "active" ["created_at"]=> string(20) "2013-09-08T12:56:04Z" } } }
Now all I want to do is be able to use this data
e.g if name = batman then
I know this is a stupid question but I am struggling :(
Thank in Advance
json_decode() with TRUE as second parameter gives you an associative array. You need to access the correct index to do what you want.
To list the complete associative array with nice formatting, you can do:
echo '<pre>', print_r($arr), '</pre>';
Now, to access the name in your array:
$man = $arr['cards'][0]['name'];
To check if it's Batman (yay!):
if( isset($man) && $man == 'batman' ) {
# code ...
}
For getting the name of all similar names:
$man = $json['cards']['0']['name'];
for ($i=0; $i < count($json['cards']); $i++) {
echo $json['cards'][$i]['name']."\n";
}
See it live!
when you got the array
$arr = json_decode($json_raw, TRUE);
then check if cards key exist
if(array_key_exists('cards', $arr)){
foreach($arr['cards'] as $key=>$val){
echo $key; ///name, id..
echo $val; /// batman,...
if($key == 'name' && $val =='batman'){
//-------do your stuff
}
}
}
Try with:
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
EDIT:
Ok, so this worked for me using code above, try it yourself if you want:
<?php
$json_raw = '{"status":"OK","cards":
[{"id":100001,"name":"batman","image":11111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T11:37:07Z"},
{"id":100002,"name":"superman","image":111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:30:09Z"},
{"id":100003,"name":"catwoman","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-08-15T12:39:42Z"},
{"id":100004,"name":"bane","image":1111111,"size":75,"region_id":1,"locked":false,"status":"active","created_at":"2013-09-08T12:56:04Z"}
]}';
$arr = json_decode($json_raw, TRUE);
$cards = $arr['cards'];
foreach($cards as $card) {
if($card['name'] == 'batman') echo 'Hello batman!';
}
?>

Categories