I got follow array ($json_output):
array(3) {
["ProductsSummary"]=>
array(4) {
["Records"]=>
int(500)
["TotalRecords"]=>
int(5720)
["TotalPages"]=>
int(12)
["CurrentPage"]=>
int(2)
}
["Products"]=>
array(500) {
[0]=>
array(10) {
["ProductId"]=>
int(1323819499)
["ShopId"]=>
int(1856)
["ProductName"]=>
string(21) "Fossil Creole JF84757"
["Deeplink2"]=>
string(0) ""
["Brand"]=>
NULL
["Manufacturer"]=>
string(6) "Fossil"
["Distributor"]=>
NULL
["EAN"]=>
string(13) "4048803717479"
["Keywords"]=>
NULL
["Properties"]=>
array(3) {
[0]=>
array(2) {
["PropertyName"]=>
string(12) "DeliveryTime"
["PropertyValue"]=>
string(1) "5"
}
[1]=>
array(2) {
["PropertyName"]=>
string(17) "MerchantArtNumber"
["PropertyValue"]=>
string(8) "85145452"
}
[2]=>
array(2) {
["PropertyName"]=>
string(6) "gender"
["PropertyValue"]=>
string(5) "Damen"
}
}
}
[1]=>
array(10) {
["ProductId"]=>
int(1323819505)
["ShopId"]=>
int(1856)
["ProductName"]=>
string(16) "SANSIBAR Armband"
["Deeplink2"]=>
string(0) ""
["Brand"]=>
NULL
["Manufacturer"]=>
string(8) "Sansibar"
["Distributor"]=>
NULL
["EAN"]=>
NULL
["Keywords"]=>
NULL
["Properties"]=>
array(3) {
[0]=>
array(2) {
["PropertyName"]=>
string(12) "DeliveryTime"
["PropertyValue"]=>
string(1) "5"
}
[1]=>
array(2) {
["PropertyName"]=>
string(17) "MerchantArtNumber"
["PropertyValue"]=>
string(8) "85189719"
}
[2]=>
array(2) {
["PropertyName"]=>
string(6) "gender"
["PropertyValue"]=>
string(5) "Herren"
}
}
}
I need to unset all Products which contains 'Herren' in Properties, so I tried:
<?php
foreach($json_output["Products"] as & $bla)
$check = $bla["Properties"][0]["PropertyValue"] . $bla["Properties"][1]["PropertyValue"] . $bla["Properties"][2]["PropertyValue"];
if (preg_match('/Herren/',$check))
{
unset($bla);
}
?>
But it's not working.
array_filter iterates for you and returns a filtered set of elements.
The callback function returns true if the element is to remain, and false if it is to be removed. json_encode converts the whole array into a string, strpos looks for the string Herren anywhere in that string. Since you require no regular expression, there's no need to use preg_match which is slower than strpos.
$array['Products']=array_filter($array['Products'], 'removeHerren');
function removeHerren($array){
return strpos(json_encode($array), 'Herren')===false;
}
Related
I'm decoding a json that I get from Notion's API in an associative array. When I var_dump it, it outputs as follows (with some information removed for security):
array(4) {
["object"]=>
string(4) "list"
["results"]=>
array(1) {
[0]=>
array(10) {
["object"]=>
string(4) "page"
["id"]=>
string(36) "dbid"
["created_time"]=>
string(24) "2021-11-17T10:06:00.000Z"
["last_edited_time"]=>
string(24) "2021-11-17T15:58:00.000Z"
["cover"]=>
NULL
["icon"]=>
NULL
["parent"]=>
array(2) {
["type"]=>
string(11) "database_id"
["database_id"]=>
string(36) "dbid"
}
["archived"]=>
bool(false)
["properties"]=>
array(2) {
["Meaning"]=>
array(3) {
["id"]=>
string(8) "%5Eny%3A"
["type"]=>
string(9) "rich_text"
["rich_text"]=>
array(1) {
[0]=>
array(5) {
["type"]=>
string(4) "text"
["text"]=>
array(2) {
["content"]=>
string(26) "This is a really big house"
["link"]=>
NULL
}
["annotations"]=>
array(6) {
["bold"]=>
bool(false)
["italic"]=>
bool(false)
["strikethrough"]=>
bool(false)
["underline"]=>
bool(false)
["code"]=>
bool(false)
["color"]=>
string(7) "default"
}
["plain_text"]=>
string(26) "This is a really big house"
["href"]=>
NULL
}
}
}
["DataElement"]=>
array(3) {
["id"]=>
string(5) "title"
["type"]=>
string(5) "title"
["title"]=>
array(1) {
[0]=>
array(5) {
["type"]=>
string(4) "text"
["text"]=>
array(2) {
["content"]=>
string(8) "bigHouse"
["link"]=>
NULL
}
["annotations"]=>
array(6) {
["bold"]=>
bool(false)
["italic"]=>
bool(false)
["strikethrough"]=>
bool(false)
["underline"]=>
bool(false)
["code"]=>
bool(false)
["color"]=>
string(7) "default"
}
["plain_text"]=>
string(8) "bigHouse"
["href"]=>
NULL
}
}
}
}
["url"]=>
string(63) "https://www.notion.so/bigHouse-connec"
}
}
["next_cursor"]=>
NULL
["has_more"]=>
bool(false)
}
I'm trying to get "This is a really big house" from the above associative array. I send the payload to notion that filters by the "DataElement" value "bigHouse". That works ok, but what I can't figure out is how to output "This is a really big house" from the associative array using PHP.
I have tried the following:
foreach($decoded as $key=>$val){
echo $key." "."| Value: ".$val."<br>";
}
This outputs:
object | Value: list
results | Value: Array
next_cursor | Value:
has_more | Value:
I'm not sure how to proceed from here. Any help would be appreciated. The name of the colum where "This is a really big house" is located on notion is: "Meaning".
As #berend stated in the comments:
$decoded['results'][0]['properties']['Meaning']['rich_text'][0]['text']['content'];
I'm grouping one multidimensional array by age.
This is my code:
$mEmployees = array (
array("name"=>"Pedro", "age"=>20, "ID"=>1111),
array("name"=>"Carlos", "age"=>15, "ID"=>2222),
array("name"=>"Susana", "age"=>20, "ID"=>3333),
array("name"=>"Carmen", "age"=>19, "ID"=>4444)
);
$byAge=array();
foreach ($mEmployees as $k => $oneItem) {
$byAge[$oneItem['age']][$k] = $oneItem;
}
var_dump($byAge);
That works fine as you can see below:
output:
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["age"]=>
int(20)
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["age"]=>
int(20)
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["age"]=>
int(15)
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["age"]=>
int(19)
["ID"]=>
int(4444)
}
}
}
But in the results, the age key is redundant. I want to remove this key in the $byAge array.
I tried with array_slice, but it's not possible to indicate one irregular offset (the key age is in middle).
How I can achieve this easily for this result?
array(3) {
[20]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(5) "Pedro"
["ID"]=>
int(1111)
}
[2]=>
array(3) {
["name"]=>
string(6) "Susana"
["ID"]=>
int(3333)
}
}
[15]=>
array(1) {
[1]=>
array(3) {
["name"]=>
string(6) "Carlos"
["ID"]=>
int(2222)
}
}
[19]=>
array(1) {
[3]=>
array(3) {
["name"]=>
string(6) "Carmen"
["ID"]=>
int(4444)
}
}
}
Cache the age value in a variable and unset from $oneItem.
foreach ($mEmployees as $k => $oneItem) {
$age = $oneItem['age'];
unset($oneItem['age']);
$byAge[$age][$k] = $oneItem;
}
Demo: https://3v4l.org/pDDn5
I have an object I need to parse out and insert into SQL but I am having a hard time figuring out what I need to do as far as the order of fields in the syntax listed below.
Here is the object vardump:
object(EmailCheck\Object\ResponseObject)#25 (1) {
["response":protected]=> array(6) {
["status"]=> string(7) "success"
["total_results"]=> int(6)
["total_pages"]=> int(1)
["query"]=> array(8) {
["job_id"]=> int(2562625)
["valids"]=> int(1)
["invalids"]=> int(1)
["disposables"]=> int(1)
["catchalls"]=> int(1)
["unknowns"]=> int(1)
["page"]=> int(0)
["items_per_page"]=> int(10)
}
["results"]=> array(6) {
[0]=> array(2) {
["data"]=> array(4) {
["email"]=> string(20) "chris#example.com"
[0]=> string(1) "1"
["ID"]=> string(1) "1"
["EMAIL"]=> string(20) "chris#example.com"
}
["verification"]=> object(EmailCheck\Object\VerificationObject)#7 (1) {
["response":protected]=> array(7) {
["result"]=> string(5) "valid"
["flags"]=> array(3) {
[0]=> string(7) "has_dns"
[1]=> string(10) "has_dns_mx"
[2]=> string(16) "smtp_connectable"
}
["suggested_correction"]=> string(0) ""
["address_info"]=> object(EmailCheck\Object\ResponseObject)#9 (1) {
["response":protected]=> array(9) {
["original_email"]=> string(20) "chris#example.com"
["normalized_email"]=> string(20) "chris#example.com"
["addr"]=> string(6) "chris"
["alias"]=> string(0) "" ["host"]=> string(13) "example.com"
["fqdn"]=> string(13) "example.com"
["domain"]=> string(9) "example"
["subdomain"]=> string(0) ""
["tld"]=> string(3) "com"
}
}
["email"]=> string(20) "chris#example.com
["result_integer"]=> int(0)
["credits_info"]=> object(EmailCheck\Object\ResponseObject)#8 (1) {
["response":protected]=> array(0) {
}
}
}
}
}
I know the syntax is something like this but it is not returning anything:
echo $object->data->email;
I need to extract the following fields: result, normalized_email, email
This is what ended up working. I did not need to reference the "response" portion of the object.
$object->results[0]['data']['email']
I'm using the Paypal API PHP REST SDK. I got a response that looks like in JSON format but i'm unable to access the properties inside the object and array.
How do I access the "state" properties from this JSON response via PHP? The response is being wrapped by the object of Paypal\Api\Payment type. foreach looping returns NULL
var_dump($response) looks like below:
object(PayPal\Api\Payment)#8 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(8) {
["id"]=>
string(28) "PAY-8JC052XXXXKKZMQNQ"
["create_time"]=>
string(20) "2013-12-19T10:19:34Z"
["update_time"]=>
string(20) "2013-12-19T10:20:38Z"
["state"]=>
string(8) "approved"
["intent"]=>
string(4) "sale"
["payer"]=>
object(PayPal\Api\Payer)#33 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(2) {
["payment_method"]=>
string(6) "paypal"
["payer_info"]=>
object(PayPal\Api\PayerInfo)#30 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(5) {
["email"]=>
string(11) "some#email.com"
["first_name"]=>
string(6) "fname"
["last_name"]=>
string(5) "lname"
["payer_id"]=>
string(13) "UAGGF3392CUTG"
["shipping_address"]=>
object(PayPal\Api\Address)#31 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(5) {
["line1"]=>
string(26) "Address"
["city"]=>
string(13) "City"
["state"]=>
string(8) "State"
["postal_code"]=>
string(5) "000000"
["country_code"]=>
string(2) "US"
}
}
}
}
}
}
["transactions"]=>
array(1) {
[0]=>
object(PayPal\Api\Transaction)#34 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(4) {
["amount"]=>
object(PayPal\Api\Amount)#35 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["total"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
["details"]=>
object(PayPal\Api\Details)#36 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["subtotal"]=>
string(4) "1.00"
}
}
}
}
["description"]=>
string(33) "Item name: 1"
["item_list"]=>
object(PayPal\Api\ItemList)#37 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["items"]=>
array(1) {
[0]=>
object(PayPal\Api\Item)#38 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(4) {
["name"]=>
string(20) "Item name"
["price"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
["quantity"]=>
string(1) "1"
}
}
}
}
}
["related_resources"]=>
array(1) {
[0]=>
object(PayPal\Api\RelatedResources)#40 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(1) {
["sale"]=>
object(PayPal\Api\Sale)#42 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(7) {
["id"]=>
string(17) "5DH04XXX63X"
["create_time"]=>
string(20) "2013-12-19T10:19:34Z"
["update_time"]=>
string(20) "2013-12-19T10:20:38Z"
["state"]=>
string(9) "completed"
["amount"]=>
object(PayPal\Api\Amount)#44 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(2) {
["total"]=>
string(4) "1.00"
["currency"]=>
string(3) "USD"
}
}
["parent_payment"]=>
string(28) "PAY-8JC05XXXXKZMQNQ"
["links"]=>
array(3) {
[0]=>
object(PayPal\Api\Links)#46 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(65) "https://api.sandbox.paypal.com/v1/payments/sale/5DHXX91763X"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
[1]=>
object(PayPal\Api\Links)#47 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(72) "https://api.sandbox.paypal.com/v1/payments/sale/5DHXXA691763X/refund"
["rel"]=>
string(6) "refund"
["method"]=>
string(4) "POST"
}
}
[2]=>
object(PayPal\Api\Links)#48 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JC052914XX1034SKKZMQNQ"
["rel"]=>
string(14) "parent_payment"
["method"]=>
string(3) "GET"
}
}
}
}
}
}
}
}
}
}
}
["links"]=>
array(1) {
[0]=>
object(PayPal\Api\Links)#49 (1) {
["_propMap":"PayPal\Common\PPModel":private]=>
array(3) {
["href"]=>
string(79) "https://api.sandbox.paypal.com/v1/payments/payment/PAY-8JC0XX914D601034SKKZMQNQ"
["rel"]=>
string(4) "self"
["method"]=>
string(3) "GET"
}
}
}
}
}
I tried json_decode($response) but it returned NULL so i assumed that this is already the correct JSON format.
I tried echo $response->id and it returns blank
I've also tried multiple variations of foreach ($response->id as $value) { var_dump($value); } which also returns nothing
Help!
If you use only json_decode($result) it will not convert entire objects into an array.
So simply use
$result=json_decode($result, true, 512);
It will convert all the objects into associative array recursively.
Try it. It works for me.
It turns out that this is not a standard JSON format. For some reason the Paypal API SDK return it in their own "json" format through this line
$ret->fromJson($json);
return $ret;
I just skipped that and return $json instead and it gives me the format that i can put into json_decode for further processing.
return $json;
That took me 1 full freaking day! Pff...
You can also use
$response->toJSON();
then you can use
$result = json_decode($response);
echo ($result->state);
Convert json to a string then store the content in an array (decode it with json_decode).
Given arrays 'a1' and 'b1' how may they be combined them to produce the final array? Basically replacing the value within 'a1' with the array data for the matching value within 'b1'. I guess the question would be if there is a function that can do this that I'm not seeing.
$a1 = array('id1'=>array('a'=>'444-444',
'b'=>'222-222',
'c'=>'111-111'),
'id2'=>array('a'=>'888-888',
'b'=>'666-666',
'c'=>'555-555')
);
$b1 = array('222-222'=>array('first'=>array('9999',
'dddd',
'yyyy'),
'second'=>'mmgghh'
),
'666-666'=>array('first'=>array('bbbb',
'cccc',
'7777'),
'second'=>'ffffgggg'
)
);
Desired combination:
array(2) {
["id1"]=>
array(3) {
["a"]=>
string(7) "444-444"
["b"]=>
array(1) {
["222-222"]=>
array(2) {
["first"]=>
array(3) {
[0]=>
string(4) "9999"
[1]=>
string(4) "dddd"
[2]=>
string(4) "yyyy"
}
["second"]=>
string(6) "mmgghh"
}
}
["c"]=>
string(7) "111-111"
}
["id2"]=>
array(3) {
["a"]=>
string(7) "888-888"
["b"]=>
array(1) {
["666-666"]=>
array(2) {
["first"]=>
array(3) {
[0]=>
string(4) "bbbb"
[1]=>
string(4) "cccc"
[2]=>
string(4) "7777"
}
["second"]=>
string(6) "ffffgggg"
}
}
["c"]=>
string(7) "555-555"
}
}
array_walk_recursive($a1,function(&$value,$key,$addin){
if(is_scalar($value) && isset($addin[$value])){
$value = array($value=>$addin[$value]);
}
},$b1);